DEV Community

rakeshvngowda
rakeshvngowda

Posted on

Data Types in Java

Every variable in Java has a data type.Data types specify the size and type of values that can be stored in an identifier.

In Java Data types are classified into two categories.

  1. Primitive Data type(built-in data type).
  2. Non-Primitive Data type(reference data type).

Primitive Data type.

Java has 8 primitive data types

Integer types

  1. byte - 1byte(8-bits) value range from -128 to 127
  2. short - 2byte(16-bits) value -32768 to 32768
  3. int - 4bytes(32-bits) value from -2147483648 to 2147483647
  4. long - 8bytes(64-bits) value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
public class App 
{
   public static void main(String[] args) {
      // byte type
      byte b = 20;
      System.out.println("byte= "+b);

      // short type
      short s =20;
      System.out.println("short= "+s);

      //int type
      int i =20;
      System.out.println("int= "+i);

      // long type
      long l = 20;
      System.out.println("long= "+l);
   }

}
Enter fullscreen mode Exit fullscreen mode

Floting types

  1. float - 4byte(32-bits) float data type. ex: 0.3f
  2. double - 8byte (64-bits) float data type. ex: 11.123
public class App 
{
   public static void main(String[] args) {
      // float type
      float f = 20.25f;
      System.out.println("float= "+f);

      // double type
      double d = 20.25;
      System.out.println("double= "+d);
   }

}
Enter fullscreen mode Exit fullscreen mode

Character Type

  1. char - 2bytes(16bits) range from 0 to 65,535.
  2. boolean - true or false.
public class App 
{
   public static void main(String[] args) {
      char ch = 'S';
      System.out.println(ch);

      char ch2 = '&';
      System.out.println(ch2);

      char ch3 = '$';
      System.out.println(ch3);
   }

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)