Data Types-Java

There are 8 primitive data types in Java. All numeric data types are signed data types, because Java does not supports unsigned data types. Other these 8 there are 2 library define data types.

S.No.Data TypeSize in Byte(s)Range
1byte1 byte -128 to +127
2short2 bytes -32768 to +32767
3int4 bytes -2147483648 to +2147483647
4long8 bytes -9223372036854775808 to +9223372036854775807
5float4 bytes 1.4E-45 to +3.4028235E38
6double8 bytes 4.9E-324 to +1.7976931348623157E308
7char2 bytes Unicode Character
8booleanx true of false
Library Define Data Types
1Stringx To Store string data e.g. "ABC"
2Objectx We will discuss later
Please check the range of long and float, size of the long is greater than the float, but range of float is higher, because the internal data formats of both the data types are different.Read this
There are 50 keywords in Java. All primitive data types are also keywords, but last two data types (String/Object) are not the keywords, because these are not primitive data types.
Java also fixed the data types of Literals.
  • 673 any value without decimal point will be consider as 'int'
  • 85.25 any value with decimal point will be consider as 'double' not float
  • 'A' characters are in single quotes
  • "ABCD" strings are in double quotes
  • 45L or 45l will be consider as long (check suffix 'L' or 'l')
  • 498.5F or 42.5f will be consider as float (check suffix 'F' or 'f')
  • Also note that 1 and 0 are Integers in Java not 'true' or 'false' as in 'C'

You can't assign larger data type to smaller one. Check following cases.

float x=67.23 Error, 67.23 is double
float x=67.23F Valid
int x=23L Error, 23L is long
In some case we wish to assign large data type to smaller one, then you can use typecasting.
float x=23.87; Error
float x=23.87F; Valid
double y=x; Valid- Implicit type casting
x=y; Error
x=(float)y; Valid- Explicit Type Casting
If you typecast some very large value into smaller one, then value may be changed.
Note: Result of any type of expression will be 'int' or greater than 'int' (if expression contains some data type greater than the 'int'):
byte+byte=int (Min. result is 'int')
byte+short=int (Min. result is 'int')
short+short=int (Min. result is 'int')
int+int=int (Min. result is 'int')
int+long=long (long is largest data type in the expression)
float+long=float (float is largest data type in the expression)
int+double+float=double (double is largest data type in the expression)