There are basically eight primitive data types in Java which are can be looked as four categories:
The literals of integral type can be expressed using decimal, octal and hexadecimal forms.
Some of the legal integer literals are:
223
-3460
For octal (base 8), it is expressed with a leading zero (0) and it cannot include the digits 8 and 9.
Some of them are:
0377
077
int octalNuumber1=0377; int octalNumber2=077;
For hexadecimal, it is expressed with a 0x or 0X, using letters A to F s the additional digits required for base-16 numbers.
Some are:
0XCAFE
0xABB
int hexNumber1=0XCAFE; int hexNumber2=0xABB;
Java does not allow integer literals to be expressed in binary (base-2) notation. They are 32 bit int values unless they end with character L or l in which case they are 64 bit long.
int intNumber=1454; long longNumber=1454L; long longOctal=047L; long longHex=0xefL;
Float is 32 bit, single precision floating point value and double is 64 bit long, double precision.
Some of which are:
3.14
123.56
0.01
It can use exponential or scientific notation which is a number followed by e or E and another number. The second number represents the power of ten by which the first number is multiplied. Some are:
1.346E03 // 1.346 * 10^3 1e-7 //1 * 10^-7
Floating point literals are double by default. To include float, we have to follow the number by the character f or F.
float number=2.465F //float should be declared with suffix f or F
The boolean type represents a truth value. So there can be only two possible states- on or off/ yes or no/ true or false. boolean values can never be converted to or from other data types, especially integer.
boolean name= true; //declares name as boolean //and the value to be true.
There are two types- char and string. The char type represents Unicode characters and is 16 bit long. To include a character literal, just place it between apostrophes (single quotes).
char c='B';
Escape Sequence | Character |
\b | backspace |
\n | New line |
\t | Vertical tab |
\f | Form Feed |
\r | Carriage Run |
\" | Double Quotes |
\\ | Backslash |
\' | Single Quotes |
Strings are not a primitive data type but is a class. A string literal consists of arbitrary text within double quotes.
String program= "hello world";
Type | Contains | Default Value | Size(bits) | Range |
byte | signed integer | 0 | 8 | -127 to 128 |
short | signed integer | 0 | 16 | -32678 to 32676 |
int | signed integer | 0 | 32 | -2147483648 to 2147483647 |
long | signed integer | 0 | 64 | -9223372036854775808 to 9223372036854775807 |
float | floating point | 0.0F | 32 | +/- 1.4E-45 to +/- 3.4028235E+38 |
double | floating point | 0.0 | 64 | +/-4.9E-324 to +/-1.7976931348623157E+308 |
char | Unicode character | '\u0000' | 16 | \u0000 to \uFFFF |
boolean | true or false | false | 1 | NA |
Post your comment
Says:User