WeCodeAcademy

Java Data Types

Java has primitive types, built into the language, and reference types such as String and arrays.

ExampleJava
public class Main {
  public static void main(String[] args) {
    byte b = 100;          // 8 bits
    short s = 30000;       // 16 bits
    int i = 2_000_000_000; // 32 bits
    long l = 9_000_000_000L; // 64 bits, note the L
    float f = 3.14f;       // 32-bit decimal, note the f
    double d = 3.14159265358979; // 64-bit decimal
    char c = 'J';
    boolean ok = true;
    System.out.println(b + " " + s + " " + i + " " + l);
    System.out.println(f + " " + d);
    System.out.println(c + " " + ok);

    int big = Integer.MAX_VALUE;
    System.out.println(big);
    System.out.println(big + 1); // overflow wraps around
  }
}
Output
100 30000 2000000000 9000000000
3.14 3.14159265358979
J true
2147483647
-2147483648

Use int for whole numbers and double for decimals unless you have a reason not to. Notice that adding 1 to the largest int wraps around to a negative number: this is overflow.

Quick check

What does adding 1 to Integer.MAX_VALUE give?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.