CodeBlanks Logo Icon CodeBlanks

Primitive types

Learn primitives byte, short, int, long, float, double, char, boolean.

What are primitive types?

Java has eight primitive types. They are the most basic data types and store values directly in memory.

  • Integer: byte (8 bit), short (16 bit), int (32 bit), long (64 bit)
  • Decimal: float (32 bit), double (64 bit)
  • Character: char (16 bit Unicode)
  • Logical: boolean (true/false)

The most common are int and double. long is for very large numbers, char for single characters.

char holds a single character

char holds a single Unicode character. It is written in single quotes:

char letter = 'A';
char digit = '7';
char sign = '$';

A char is actually a 16-bit number, so it can take part in arithmetic. 'A' + 1 yields the numeric value of 'B'.

Double quotes "A" make a String, single quotes 'A' make a char. The distinction matters.

Practice Define primitives and print them

Write a Main class with a main method. Assign 100 to int code and 'A' to char initial. Print both with println.

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        int code = 100;
        char initial = 'A';
        System.out.println(code);
        System.out.println(initial);
    }
}

Expected output

100
A

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is a primitive type?

  • String
  • char Correct answer
  • Scanner
  • Main

Explanation: char is a primitive; String is a reference type.

2 Assign a value to a long variable

Question
long population = ____;
Answer
long population = 8000000000L;

Explanation: A long literal ends with the L suffix.

3 Match the primitive types

Term Meaning
byte 8 bit
short 16 bit
int 32 bit
long 64 bit

All lessons in Beginner