Data Types in Java


Data Type -

Every variable has a type associated with it, that describes the size and type of value that variable can contain. This type is known as Data Type. Java supports the following types of Data Types-
java data types

Types of Java Data type -

Data types can be categorized into 2 categories -
1. Primitive Data types
2. Non Primitive Data types

1. Primitive Data types -

Java defines 8 primitive data types - byte, short, int, long, float, double, char and boolean.

Integer Types -

Java support 4 integers types- byte, short, int and long all are signed integers i.e. can store both positive and negative values. Java does not support unsigned integers.
Type Size Value Range
byte 1 byte -128 to 127
short 2 byte -32,768 to 32,767
int 4 byte -2,147,483,648 to 2,147,483,647
long 8 byte -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Example -


public class Demo2
 {
   public static void main(String[] args) 
    {
      byte a = 10;
      short b = 20;
      int c= 30;
      long d = 40l;        
      
      //Note we use lower case 'l' to denote a long value 
      
      System.out.println("value of a:"+a);   
  
      /*  '+' is used to concatenate something with any string
      and make a new string  */
      
      System.out.println ("value of b:"+b);    
      System.out.println ("value of c:"+c);
      System.out.println ("value of d:"+d);
    }    
  }

 

Output -

value of a:10
value of b:20
value of c:30
value of d:40
 

Floating Point Types -

Java supports 2 floating point data types - float and double. For number containing fractional part we use these data types.
Type Size Value Range
float 4 byte 3.4e+038 (positive or negative)
double 8 byte 1.7e+308 (positive or negative)

Example -


public class Demo3
 {
  public static void main(String[] args) 
  {
   float a = 10.123f; 
   
   //Note we use lower case 'f' to denote a float value 
   
   double b = 20.123;
   
   //By default floating point types are double, so no need to add any postfix for double data type.
   
   System.out.println("value of a:"+a);    
   System.out.println ("value of b:"+b);
   }    
 }

 

Output -

value of a:10.123
value of b:20.123
 

Note -

Larger the size of data type, larger the time required for its manipulation. So don't use larger size data type unnecessarily i.e. if you need to store a value 12 for example then use byte data type in place of int or long etc to improve the execution speed.

Characters Type -

To store character java support char data type. In java char is 2 byte Unicode character that defines fully international character set that can represent all of the characters found in all human languages.
We can also perform arithmetic operations on characters, char can be thought of integer type.

Example -


public class Demo4
{
 public static void main(String[] args) 
 {
  char ch1, ch2, ch3, ch4;
  ch1 = 'A';
  ch2 = 'B';
  ch3 = 65;       // ASCII value of A
  System.out.println("values of ch1, ch2 and ch3 are:"+ch1+"  "+ch2+"  "+ch3);
  ch1++;
  ch4 = ch1;
  int a = ch1+ch2;
  System.out.println("values of ch4 and a are:"+ch4+"  "+a);
 }    
}


     

Output -

values of ch1, ch2 and ch3 are:A  B  A
values of ch4 and a are:B  132

Description -

In the program ch1 and ch2 are assigned the values 'A' and 'B'. Ch3 is assigned a value 65 which is ASCII value of 'A'. So if we print the value of ch3 that will be 'A'.
We can also increment/decrement value of characters so ch1++ will increment the value of ch1 by one character so new value of ch1 will be 'B'.
When we perform any arithmetic operation on characters they will be automatically promoted to int data type that's why when we add ch1 and ch2 result is stored in integer 'a' and the value of 'a' is addition of ASCII equivalent of 'A' and 'B'.

Boolean Type -

To support Boolean values (true or false) java provides 'boolean' data type. It require only 1 bit to store boolean value in memory.
Note :- we cannot use 0 for false and non zero value as true in java, as in C or C++.

Example -


public class Demo5
{
 public static void main(String[] args) 
 {
  boolean a,b;
  a = true;
  b=false;
  System.out.println("values of a and b are:"+a+"  "+b);
 }    
}


         

Output -

values of a and b are:true  false

2. Non Primitive Data types -

There are several non primitive data types supported by java like class, interface etc. These will be covered next.