Data types in Java



Primitive data types of java can be divided into three categories.

  • Integers type It represent signed numbers (byte, short, int, long) and unsigned char value.
  • Floating type It represent fractional values(float, double).
  • boolean type It represent logical values i.e. true or false.

Primitive data types are not object. Each primitive data type has a wrapper class which is used to represent primitive data type as object.

Data Type Size(bits) MAX_VALUE MIN_VALUE
byte 8-27(-128)27-1(127)
short 16-215(-32768)215-1(32767)
int 32-231(-2147483628)231-1(2147483627)
long 64-263(-9223372036854775808L)263-1(9223372036854775807L)
float 321.401298464324817E-45f3.402823476638528860e+38f
double 644.94065645841246544e-3241.79769313486231570e+308
char 160x0(\u0000)0xffff(\uffff)
boolean 1NANA


Signed integer values are represented using 2's Complement. In java character are represented using unsigned integer (char data type). It can represent 216 characters.

Floating point numbers confirm to IEEE754 standard.

In java every real number by default considers as double. In order to assign a real value to float, that value has to end with 'f' character.

// Program : Error during assigning real value
class types
 {
  public static void main(String arg[])
   {
   // Error: 1.23 is considered as double.
  float f=1.23;    
	}
 }

Example

// Program : Variable declaration of different-2 data types
class types
 {
  public static void main(String arg[])
   {
     int a=12;
	 float b=3.4f;
	 char c='A';
	 boolean f=true;
   }
 }



Assignment of beyond range value

Primitive data types has fixed data size which specify their range. Following program will show conversion of beyond range value into in-range value.

// Program : Assignment of beyond range value
class types
 {
  public static void main(String arg[])
   {
   // range of byte is -128...127 
     byte b=(int)130;
     System.out.println("Value of b:"+b);
   }
 }
/* Output
Value of b:-126
*/