Java Data Types

Each variable in Java has a java data type. The java data type controls what kind of value can be saved in the variable like integers decimal, character etc. The operating system allocates space for the variable based on its data type. Java data types can be divided in two groups:

  • Primitive Data Types
  • Non Primitive Data Types
Java Data type
Java Data Types

Primitive Java Data Types

The primitive data types are the basic pre defined data types in java. These java data types have a keyword. All primitive data types start with a lower case letter. There are eight primitive data types as given below:

  1. int
  2. double
  3. float
  4. short
  5. long
  6. char
  7. byte
  8. boolean

1. int

int data type stores positive and negative integer values. It is a 32 bit signed two’s compliment integer. The maximum size range of integers is 4 bytes (32 bits). int data type store wholes numbers from -2147483648 to 2147483647. By default, the value of int variable is 0. The syntax for declaring an int variable is:

For example, the below code shows to declare, initialize and print int type variables. Two or more variables can be declared and initialized in the same line as show below.

public class MyProgram {
  public static void main(String[] args) {
    int num = -4, var = 123456;
    System.out.println(num);
    System.out.println(var);  
  }
}

The output of the above code after compilation is:

2. double

The double data type stores floating point numbers. It stores fractions from  from 1.7e−308 to 1.7e+308. The double type fractional numbers end with the literal d. The size of double variable is 8 bytes (64 bits) that can save decimal number upto 16 decimal places. The default value of double variable is 0.0.The syntax for declaring double type variable is:

The below example creates and displays the value stored in a double type variable.

public class MyProgram {
  public static void main(String[] args) {
    double num = 7.86d;
    double var = 3.142;
    System.out.println(num);  
    System.out.println(var); 
  }
}

The output of the above code after compilation is:

3. float

The float data type stores decimal values. It stores fractional numbers from 3.4e−038 to 3.4e+038. The float type fractional numbers end with the literal f. The size of float variables is 4 bytes (32 bits) that can save decimal number upto 7 decimal places. By default the value of float variable is 0.0. The syntax for declaring float variable is:

The below example creates and displays the value in a float type variable.

public class MyProgram {
  public static void main(String[] args) {
    float num = 6.75f;
    float var = 3.14235623961f;
    System.out.println(num); 
    System.out.println(var);
  }
}

The output of the above code after compilation is:

Note that the decimal places greater than 7 are dropped by the compiler.

4. short

The short data type is 16-bit signed two’s complement integer. Its size is 2 bytes (16 bits) and stores values from -32768 to 32767. It is used to save data in large arrays. Its default value is 0. The syntax for declaring short variable is:

The below example creates and displays the value in a short type variable.

public class MyProgram {
  public static void main(String[] args) {
    short num = 2020;
    System.out.println(num);  
  }
}

The output of the above code after compilation is:

5. long

The long data type is a 64-bit two’s complement integer. It store whole numbers from -9223372036854775808 to 9223372036854775807. It is used to store large numbers and its size is 8 bytes (64 bits). The float type fractional numbers end with the literal L. The syntax for declaring long type variable is:

The below example shows how to create and display the value stored in a short type variable.

public class MyProgram {
  public static void main(String[] args) {
    long num = 75000000000L;
    System.out.println(num);  
  }
}

The output of the above code after compilation, on the console will be:

6. char

The char data type stores single characters. These characters are stored inside single quotes. It is a single 16-bit Unicode character. Its size is 2 bytes (16 bits) and stores numbers from ‘\u0000’ (0) to ‘\uffff’. The syntax for declaring char type variable is:

The below example shows how to create and display char type variable.

public class MyProgram {
  public static void main(String[] args) {
    char var = 'A';
    System.out.println(var);
  }
}

The output of the above code after compilation will be:

7. byte

The byte data type is an 8-bit signed two’s complement integer. Its size is 1 byte (8 bits) and stores values from -128 to 127. It is used to save data in large arrays. Its default value is 0. The syntax for declaring a byte type variable is:

The below example shows how to create and display byte type variable.

public class MyProgram {
  public static void main(String[] args) {
    byte var = 10;
    System.out.println(var);  
  }
}

The output of the above code after compilation is:

8. boolean

The boolean data type stores only 1 bit a value that is either true or false. By default, its value is false. It works as flags. The syntax for declaring boolean type variable is:

The below example shows how to create a boolean variable:

public class MyProgram {
  public static void main(String[] args) {
    boolean var = true;
    boolean val = false;    
    System.out.println(var);
    System.out.println(val);
  }
}

The output of the above code after compilation is:

Non Primitive Java Data Types

Non Primitive data types are also called as Reference Data Types. The non primitive data types are classes, interfaces and arrays. Reference data types refer to the objects of a class. User can create non primitive data types by using constructors of a class. By default, the value of reference variable is NULL. String is an example of non primitive data type.

String

String java data type is used to store a sequence of characters like sentences. Strings are arrays of characters that are stored in double quotes (“”). The syntax for declaring string variable is:

Note that all non primitive data types start with an uppercase letter. The below example shows how to create and print string variables:

public class MyProgram {
  public static void main(String[] args) {
    String var = "Hello World!";
    System.out.println(var);
  }
}

The output of the above code after compilation is: