The Primitive Type:
There are eight primitive types of data : byte, short, int, long, char, float, double and boolean. These can be put in four categories as:
- Integers : This group includes bytes, short, int and long , all of these are used for whole valued signed integer.
- Floating point numbers : This group includes float and double which represent numbers with fractional numbers.
- Characters : This group includes char, which represents data in a character set (‘ ‘) like letters and numbers.
- Boolean : This includes only boolean, it stores only true or false values.
Integers :
There are four integer types as byte, short, int and long. all of these are signed integers (positive and negative values). java doesn’t support unsigned integers.
-
byte :
The smaller integer type is byte. width of the byte is 8 bit so we can calculate its length as 2 power(width).
2 power 8= 256 so its range is -128 to 127. byte variable are declared by using the byte keyword.
For example : byte a, b;
-
short:
Its width is 16 bit. it range from -32,768 to 32,767. short variable are declared by using the short keyword.
For example: short a;
-
int :
This is most commonly used integer type. its width is 32 bit that has range from -2,147,483,648 to 2,147,683,647. int variable are declared using the int keyword.
For example: int a;
-
long :
Width of long integer type is 64 bit and is useful when int type is not sufficient to hold the desired value. The range of a long is quite large.
For example: long light speed;
Floating point type:
Floating point numbers are also known as real numbers. it is used when evaluating expression requires fractional precision to hold. There are two kinds of floating point types as “float” and “double“, which represent single precision and double precision numbers respectively.
-
float :
The float type specifies a single precision value that uses 32 bit storage.for declaring the float variable float keyword is used. For example : float a; float variable is useful when representing the dollar and cents.
-
double :
Double precision are declared by using the double keyword. it uses 64 bit to store a value. when we need to maintain accuracy over many iterative calculations or are manipulating large valued numbers, double is the best choice. For Example: double circle;
Characters:
In Java, the data type that is used to store character is char. char uses 16 bit to store a character value in Java. The range of char is 0 to 65535. there is no negative chars.
For Example : char a;
class chars{
public static void main(String ar[]){
char a=’r’;
System.out.println(a);
char b=65;
System.out.println(b);
}
}
output : r
A // it is the ASCII value of the 65 that it prints. when a char finds a integer value without the single quotes it convert the integer value in char using the ASCII table.
Boolean:
The boolean primitive type is used for logical values. it can have one of two values, true or false. this is the type i.e. returned by all relational operators.
For example:
class BooleanTest{
public static void main(String ar[]){
boolean a;
a=false;
System.out.println(a);
a=true;
System.out.println(a);
}
}