HuntExams Academy logo
JavaBeginner#fundamentals

What are Java's primitive data types?

Answer

Eight built-in types: byte, short, int, long, float, double, char, and boolean. They store values directly on the stack rather than as objects, and each has a fixed size and default value.

Example

int i = 10;
double d = 3.14;
boolean flag = true;
char c = 'A';

Detailed explanation

Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean. They represent basic values rather than references to objects. Four types hold signed integers: byte uses 8 bits, short 16 bits, int 32 bits, and long 64 bits. Their ranges run from −2^(n−1) to 2^(n−1)−1, where n is the bit width; for example, byte ranges from −128 to 127. int is the usual choice for whole numbers, while long is useful when values can exceed int’s range.

float and double store floating-point numbers using the IEEE 754 binary32 and binary64 formats, respectively. float uses 32 bits and provides roughly 6–7 significant decimal digits; double uses 64 bits and provides roughly 15–16. Decimal literals such as 3.14 are double by default, so write 3.14f for a float. Use an L suffix for long literals when needed, such as 3000000000L. char is an unsigned 16-bit UTF-16 code unit, ranging from 0 to 65535; 'A' is a valid char literal. A single char cannot represent every Unicode character. boolean holds only true or false, and Java does not specify a universal storage size for it.

Default values depend on where variables are declared. Instance fields, static fields, and array elements receive defaults: zero for integer types, positive zero for float and double, '\u0000' for char, and false for boolean. Local variables do not receive usable automatic defaults; the compiler requires definite assignment before they are read. For example, int i = 10; double d = 3.14; boolean flag = true; and char c = 'A'; explicitly initialise four primitive variables.

Avoid saying primitives always live on the stack. A primitive can be a local variable, an object field, or an array element, and its physical storage depends on context and JVM implementation. Primitive types have defined value representations, but that does not imply identical physical memory usage everywhere. Interviewers usually expect the eight names, widths and purposes, default-initialisation rules, and awareness that wrapper classes such as Integer and Boolean are reference types, not primitives.

Key points to remember

  • The eight primitives are byte, short, int, long, float, double, char, and boolean.
  • Integer widths are 8, 16, 32, and 64 bits; all four integer types are signed.
  • float is 32-bit and double is 64-bit; binary floating-point cannot represent every decimal fraction exactly.
  • char is an unsigned 16-bit UTF-16 code unit; boolean has two values but no universally specified storage size.
  • Fields and array elements receive default values; local variables must be definitely assigned before use.

Common mistakes candidates make

  • Claiming that primitives always live on the stack or that boolean always occupies one bit.
  • Assuming local primitive variables automatically receive zero or false.
  • Calling String a primitive or confusing primitive types with wrapper classes such as Integer.

Likely follow-up questions

What is the difference between int and Integer?

int is a primitive holding a signed 32-bit integer value; Integer is its wrapper reference type and can also be null. Collections such as ArrayList<Integer> use wrappers because Java generics do not accept primitive type arguments.

Can an int be assigned to a long without casting?

Yes, this is a widening primitive conversion, and every int value fits exactly in a long. Converting a long to an int generally requires an explicit cast and can discard high-order bits, changing the value.

Why should float and double usually be avoided for exact monetary calculations?

Many decimal fractions, including 0.1, cannot be represented exactly in binary floating-point, so calculations can introduce rounding errors. Use BigDecimal with appropriate decimal inputs and rounding rules, or integer minor units when the application permits.

Interview tip: List the eight types by category, give a short declaration example, and clarify the default-value and stack-storage exceptions.

Related Interview Questions

1
JavaIntermediate#fundamentals

What is the difference between pass-by-value and pass-by-reference in Java?

Open
2
JavaBeginner#fundamentals

What is the difference between static and instance methods?

Open
3
JavaBeginner#oop

What is a constructor and how does constructor overloading work?

Open