SteelReed

Search

Search posts, questions…

Java CoreQuestion 4 / 43

Junior

What primitive types does Java have? What are the default values of variables and array elements?

Short answer

Java has 8 primitive types (types that store the value directly): byte, short, int, long, float, double, char, boolean. Fields and arrays get default values, local variables do not.

JVM zeroes memorydefinite assignmentUnassigned variableField or array elementLocal variable0, false, nullCompile errorif read before set

3 points to say:

  1. String is not a primitive, it is a class.
  2. Fields default to 0, false, null; locals must be assigned before reading.
  3. IDs and timestamps use long, because int overflows around 2.1 billion.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.