Data types

The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127:

Example

public class Main {

  public static void main(String[] args) {

    byte myNum = 126;

    System.out.println(myNum); 

  }

}

Short

The short data type can store whole numbers from -32768 to 32767:

Example

public class Main {

  public static void main(String[] args) {

    short myNum = 31000;

    System.out.println(myNum); 

  }

}

Int

The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value.

public class Main {

  public static void main(String[] args) {

    int myNum = 100000;

    System.out.println(myNum); 

  }

}

Float

The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an “f”:

public class Main {

  public static void main(String[] args) {

    float myNum = 5.75f;

    System.out.println(myNum); 

  }

}

Double

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a “d”:

public class Main {

  public static void main(String[] args) {

    double myNum = 19.99d;

    System.out.println(myNum); 

  }

}

Characters

The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’:

public class Main {

  public static void main(String[] args) {

    char myGrade = ‘B’;

    System.out.println(myGrade);

  }

}

Copyright@ 2021. All rights reserved. -NVQ Project-

Don`t copy text!