Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Data Types

Every variable in Java has a type. The type decides which values the variable can hold, how much memory it uses, and which operations make sense. Java has two groups of types: eight primitive types for simple values, and reference types for everything else, such as String, arrays and the classes you will write yourself.

The eight primitive types

TypeSizeHoldsExample
byte1 bytewhole numbers from -128 to 127byte b = 100;
short2 byteswhole numbers from -32,768 to 32,767short s = 3000;
int4 byteswhole numbers up to about 2.1 billionint n = 250000;
long8 bytesvery large whole numberslong l = 9000000000L;
float4 bytesdecimal numbers, about 7 digitsfloat f = 3.5f;
double8 bytesdecimal numbers, about 15 digitsdouble d = 3.14159;
boolean1 bit of informationtrue or falseboolean ok = true;
char2 bytesone characterchar c = 'A';

In everyday programs you mostly use int for whole numbers, double for decimals, boolean for yes-or-no answers and char for single characters.

public class Main {
    public static void main(String[] args) {
        int students = 32;
        long worldPopulation = 8_100_000_000L;
        double price = 49.99;
        boolean open = true;
        char grade = 'B';
        System.out.println(students);
        System.out.println(worldPopulation);
        System.out.println(price);
        System.out.println(open);
        System.out.println(grade);
    }
}
Output
32
8100000000
49.99
true
B

Two details: a long value that does not fit in an int needs an L at the end, and a float value needs an f. A char is written in single quotes, while text (a String) uses double quotes.

Limits of the number types

Each whole-number type has a smallest and a largest value. Java keeps them in constants such as Integer.MAX_VALUE. If a calculation goes past the largest value, it wraps around to the negative side without any error, which is a classic bug:

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Long.MAX_VALUE);
        int big = Integer.MAX_VALUE;
        big = big + 1;
        System.out.println(big);
    }
}
Output
2147483647
9223372036854775807
-2147483648

When numbers can become very large, use long. Decimal numbers have a different limit: they are stored in binary, so some values like 0.1 cannot be stored exactly. You will see this in the Operators topic.

Reference types

String is not a primitive type. It is a class, and a String variable holds a reference to a text object. Reference types have methods you can call with a dot:

public class Main {
    public static void main(String[] args) {
        String word = "Java";
        System.out.println(word.length());
        System.out.println(word.toUpperCase());
    }
}
Output
4
JAVA

Primitive values have no methods. A reference variable can also hold the special value null, which means "no object"; a primitive variable never can. The Strings topic covers text in detail, and the Wrapper Classes topic shows how each primitive type also has a class version.

Practice exercises

  1. Make one variable of each primitive type and print them all.
  2. Print Short.MAX_VALUE and Byte.MIN_VALUE.
  3. Store the number of seconds in 100 years in a long and print it. (Use 100L * 365 * 24 * 60 * 60.)