Data Types in Java — Java Technical Interview Questions & Answers

Data Types in Java

Data Types in Java

  • A data type tells Java what kind of value a box can hold. There are primitives (raw values) and non-primitives (objects like String or your own class).
  • Eight primitives: byte, short, int, long, float, double, char, boolean. Day to day, use int for whole numbers and double for decimals. boolean is only true or false — not 0 and 1 like C.
  • String, arrays, and your classes are non-primitive. Bigger types can hold bigger numbers, but don’t pick long or float without a reason. In an exam, name the 8 primitives, then say String is a class.
  • Let's take this on the board with one tiny Main class — no extra files. Data Types in Java — output: 10 then 99.5 then true. int, double, boolean — three real primitives. Start from main, go line by line, and stop at each print. That output is the proof for Data Types.

Technical Interview Tip: Name the 8 primitives. Then say String is a class, not a primitive.

Code Examples for Data Types in Java

Example

public class Main {
  public static void main(String[] args) {
    int count = 10;
    double price = 99.5;
    boolean ok = true;
    System.out.println(count);
    System.out.println(price);
    System.out.println(ok);
  }
}

Data Types in Java — output: 10 then 99.5 then true. int, double, boolean — three real primitives.

How to Explain Data Types in Java to an Interviewer

How many primitive types?

8

Name them.

byte short int long float double char boolean

Is String a primitive?

No It is a class