AI Hints
Home›Java Tutorial›Variables, Primitive Types, int and long
Java · Lesson 11 of 120

Primitive Types

The eight simple types.

⏱ 2 min read▶ Video at 14:12🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

Java has eight simple types built in, called primitive types. They hold numbers, single characters, and true or false.

  • Eight built-in types
  • Numbers, characters
  • true or false

The eight primitives

Four types hold whole numbers, of different sizes. Byte is tiny, short is small, int is the normal one, and long is huge. Two types hold decimals, float and double. Char holds one character. And boolean holds true, or false.

TypeHoldsExample
byte, shortsmall whole numbers100, 30000
intwhole numbers2000000000
longhuge whole numbers9000000000L
float, doubledecimals4.99f, 4.99
charone character'A'
booleantrue or falsetrue

All eight in one program

Here's one variable of each type. A long number needs a capital L on the end. A float needs a small f. A char uses single quotes, not double. And a boolean is just true, or false. Run it. Every value prints, as you'd expect.

Main.javajava
public class Main {
    public static void main(String[] args) {
        byte small = 100;
        short medium = 30000;
        int whole = 2000000000;
        long big = 9000000000L;
        double price = 4.99;
        float rough = 4.99f;
        char grade = 'A';
        boolean open = true;
        System.out.println(small + " " + medium + " " + whole + " " + big);
        System.out.println(price + " " + rough + " " + grade + " " + open);
    }
}
Output
100 30000 2000000000 9000000000
4.99 4.99 A true

Which ones you really use

In practice, you'll mostly use four. Int, for whole numbers. Double, for decimals. Boolean, for yes or no. And char, now and then. String isn't a primitive. It's a class, and we'll meet it soon.

keyvalue
intwhole numbers
doubledecimals
booleanyes or no
Stringtext, but not a primitive

Summary

Java has eight primitive types. Mostly, you'll use int, double, boolean, and char. Next, a closer look at int, and long.

Key takeaways
  • 8 primitives
  • int, double, boolean, char
  • String is a class
Next up: int and long — whole numbers, big and huge