Java Data Types

Java has two groups of data types: primitive types (simple values) and reference types (objects like String, arrays and classes).

The 8 primitive types

TypeSizeWhat it keepsExample
byte8 bitVery small whole number (-128 to 127)byte b = 100;
short16 bitSmall whole numbershort s = 3000;
int32 bitWhole number. The default for numbersint n = 42;
long64 bitBig whole numberlong big = 5000000000L;
float32 bitDecimal number (about 7 digits)float f = 3.14f;
double64 bitDecimal number (about 15 digits). The default for decimalsdouble d = 3.14159;
char16 bitOne characterchar c = 'A';
boolean1 bit of informationtrue or falseboolean ok = true;

The real limits

You do not need to remember the numbers. Java gives them to you:

public class Main {
    public static void main(String[] args) {
        System.out.println("byte:  " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
        System.out.println("short: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
        System.out.println("int:   " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
        System.out.println("long:  " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
        System.out.println("float max:  " + Float.MAX_VALUE);
        System.out.println("double max: " + Double.MAX_VALUE);
    }
}
Output

byte: -128 to 127
short: -32768 to 32767
int: -2147483648 to 2147483647
long: -9223372036854775808 to 9223372036854775807
float max: 3.4028235E38
double max: 1.7976931348623157E308

Examples of each type

public class Types {
    public static void main(String[] args) {
        byte b = 100;
        short s = 3000;
        int n = 42;
        long big = 5_000_000_000L;   // L at the end, and _ makes it easy to read
        float f = 3.14f;             // f at the end
        double d = 3.14159;
        char c = 'A';
        boolean ok = true;

        System.out.println(b + " " + s + " " + n + " " + big);
        System.out.println(f + " " + d + " " + c + " " + ok);
    }
}
Output

100 3000 42 5000000000
3.14 3.14159 A true

Overflow

When a value is too big for its type, it does not give an error. It wraps around to the other side:

public class Overflow {
    public static void main(String[] args) {
        int max = Integer.MAX_VALUE;
        System.out.println(max);
        System.out.println(max + 1);        // wraps to the smallest int
    }
}
Output

2147483647
-2147483648

The char type

A char keeps one character in single quotes, and it is really a number (the Unicode code):

public class Chars {
    public static void main(String[] args) {
        char c = 'A';
        System.out.println(c);
        System.out.println((int) c);        // 65
        System.out.println((char) 66);      // B
        System.out.println(c + 1);          // number: 66
    }
}
Output

A
65
B
66

Decimal numbers are not exact

public class Decimals {
    public static void main(String[] args) {
        System.out.println(0.1 + 0.2);
        System.out.println(0.1f + 0.2f);
    }
}
Output

0.30000000000000004
0.3

Computers keep decimals in binary, so some values are close but not exact. For money use BigDecimal or keep the amount in paise or cents as a long.

Reference types

Everything else is a reference type: String, arrays, and all classes. A reference variable keeps the address of an object, and it can be null (no object). Each primitive type has a wrapper class too (Integer, Double, Boolean and so on), which we use with collections.

Note: Use int for whole numbers and double for decimals, unless you have a reason for the others. Next: Java Type Casting.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.