Data types for Java


Data Types

In Java, any type of data type or variable can be stored. It can be string, integer, float, double or anything else. This tutorial covers all types of Java data types:


Types of data types

String Data Type

In Java, string data type refers to more than one character. It is written in double quotes ("").

Program
public class Main
{
  public static void main(String[] args{
     String msg = "Hello World";
     System.out.println(msg);
  }
}
Run

In the example above, the value String msg = "Hello World" is stored and then the stored value is printed.


Int Data Type

To get an integer in Java, you have to type int data. It takes up 4 bytes of memory. It can store all values between -2,147,483,648 to 2,147,483,647.

Program
public class Main
{
 public static void main(String[] args) {
   int msg = 100;
   System.out.println(msg);
 }
}
Run

In the example above int msg = 100; Say 100 interger value has been stored in msg variable.


Double Data Type

To store double value in Java you need to use "double" data type. It takes up 8 bytes of memory. It takes the fractional number as the value. And it can take 15 decimal digits value.

Program
public class Main
{
  public static void main(String[] args) {
   double msg = 123456.21;
   System.out.println(msg);
  }
}
Run

In the example above double msg = 123456.21; Say 123456.21 fractional value has been stored in msg variable.


Float Data Type

To store float value in Java you need to use "float" data type. It takes up 4 bytes of memory. It takes the fractional number as the value. And it can take 7 decimal digits value. The 'F' character must be added at the end of the value.

Program
public class Main
{
  public static void main(String[] args) {
   float msg = 123456.21F;
   System.out.println(msg);
  }
}
Run

In the example above float msg = 123456.21; Say 123456.21 fractional value has been stored in msg variable.


Byte Data Type

In Java, the "byte" data type is used to store Byte data. Byte value refers to all the values from -127 to 128. And byte takes 1 byte space in data type memory

Program
public class Main
{
   public static void main(String[] args) {
    byte msg = 127;
    System.out.println(msg);
   }
}
Run

In the example above byte msg = 127; Say 127 integer value has been stored in msg variable.


Short Data Type

In Java, if you have to take a value higher than byte but lower than int, then "short" data type is a good option. It takes up less space than the integer, only takes 2 bytes of memory. And it can take all numbers from -32,768 to 32,767 as input.

Program
public class Main
{
  public static void main(String[] args) {
   short msg = 32767;
   System.out.println(msg);
  }
}
Run

In the example above short msg = 32767; Say 32767 integer value has been stored in msg variable.


Long Data Type

In Java, if you want to get more value than int data type, you have to use "Long" data type. It takes up more than 8 bytes of memory. Its range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Program
public class Main
{
   public static void main(String[] args) {
    long msg = 32767454;
    System.out.println(msg);
   }
}
Run

In the example above long msg = 32767454; Say 32767454 integer value has been stored in msg variable.


Boolean Data Type

In Java, if the boolean value is to be taken as input, the "boolean" data type must be used. It takes up 1 byte of memory. It only accepts true or false values.

Program
public class Main
{
   public static void main(String[] args) {
    boolean msg = false;
    System.out.println(msg);
   }
}
Run

In the example above boolean msg = false; Say false boolean value has been stored in msg variable.


Char Data Type

If you want to take single character or ASCII value in Java, then "char" data type can be used. It takes up 2 bytes of memory. And in it the character is placed inside a single quote (' ').

Program
public class Main
{
   public static void main(String[] args) {
   char msg = 'M';
   System.out.println(msg);
   }
}
Run

In the example above char msg = 'M'; Say 'M' character value has been stored in msg variable.