Java Primitive Data Types
Primitive
means basic, so primitive data types in Java means the basic data types that
are present in the language. Java provides us different primitive data types
that we can use in different situations. These are total 8 and can be categorise into
1. Integers
2. Floating numbers
3. Characters
4. Boolean
Integers
are further dived into
1. Byte
a. The size of the
byte data type is 1 byte and it can store positive and negative values
b. It is used to
store smaller values and range is -128 to 127
c. Example
i. Byte
variableName=50;
2. Short
a. The size of the
short data type is 2 bytes and it can also store positive and negative values
b. Example
i. Short
variableName=2000;
3. Int
a. It has 4 bytes
and can store bigger positive and negative values
b. Example
i. Int
variablename=50000;
4. Long
a. It has 8 bytes
and can store very large numbers (Positive, Negative). It ends with “L”
b. Example
i. Long
variablename=100000000L;
Floating
numbers are further divided into
1. Float type
a. Float data type
contains 4 bytes and it is used to store fractional values. It supports 6 0r 7
decimal numbers. It ends with “f”
b. Example
i. Float
variableName=2.2f;
2. Double type
a. Also used to
store fractional values up to 15 decimal numbers
b. It ends with “d”
c. Example
i. Double
variableName=20.22d;
Characters
include alphabets or letters and it also involves numbers
It has 2 bytes and can be used to
store characters and also ASCII values
Character value should be placed in
single quotation mark
Example
Char variableName=’A’;
Boolean includes two values
1. True
2. False
Boolean
data type is used when we want to test some condition. Can be used in if-else,
switch etc
String is sometimes also considered as data
type in Java, but its actually not, because it has different methods and
primitive data types does not contains methods
Example
String name=”aslam”;

No comments:
Post a Comment