skip to content

Data Types

The eight primitives with their sizes and defaults, plus overflow behavior, the special floating-point values and literal syntax. Interviewers use this layer for the classic puzzles about wraparound, NaN and boxing.

part ofJavaoverview, primer and where to startread it →
on this pageshow

explore

questions

26

When you declare an instance field in a Java class without an initializer, what value does it hold, and how does that depend on its type?

level: juniorimportance: must knowfreq 70%

answer

  1. 0 / 0.0 / false / '' / null
  2. fields default; locals do NOT
  3. locals need definite assignment (compile error)
  4. Integer field → null, int field → 0
  5. JVM zeroes memory at allocation

basics

~10 s

Uninitialized fields get a default automatically: 0 for number types, false for boolean, '' (the null character) for char, and null for any object/reference type. You don't have to set them yourself.

solid answer

~40 s

Instance and static fields that you declare without an initializer are automatically zero-initialized by the JVM. The exact value depends on the type: numeric primitives (byte, short, int, long) default to 0, floating-point types (float, double) to 0.0, char to '' (code point 0, the null character), and boolean to false. Every reference type (objects, arrays, String) defaults to null. This happens during object construction, before your constructor body runs, so a field is never in an undefined state. The same defaulting applies to array elements after the array is created. Local variables are the exception: they get no default and must be definitely assigned before use, or the code won't compile.

go deeper

for a junior

Recall the table: 0 for numbers, false for boolean, '' for char, null for references; and that fields get these automatically while locals don't.

for a middle

Explain that the JVM zeroes memory and that defaults apply to instance fields, static fields, and array elements; distinguish primitive vs reference defaults including the wrapper-null trap.

for a senior

Tie it to definite-assignment analysis for locals, the object initialization order (defaults → initializers → constructor), and why the field-vs-local distinction is a deliberate safety design.

for a principal

Discuss implications for API design and safe object construction (avoid leaking 'this' during construction so others don't observe default-state fields), and the spec guarantees the JVM relies on.

## What a 'field' is In Java a **field** (also called a member variable) is a variable declared directly inside a class but outside any method, like `int count;` or `String name;`. There are two kinds: **instance fields** (one copy per object) and **static fields** (one copy shared by the whole class). This topic is about what value a field holds when you declare it *without* giving it a starting value (no `= something`). ## The rule: fields are default-initialized Unlike some languages (C/C++) where an uninitialized variable contains whatever garbage was previously in that memory, Java **guarantees** every field starts with a well-defined default value. The Java Virtual Machine (JVM) zeroes out the memory for an object when it is allocated. So the moment an object exists, all its fields already have predictable values, even before your constructor runs. ## The defaults, by type A **primitive type** is one of Java's built-in value types (not an object). Their defaults are all 'the zero value' of that type: - `byte`, `short`, `int`, `long` → `0` - `float` → `0.0f`, `double` → `0.0d` - `char` → `''` — this is the Unicode character with code point 0, the 'null character'. It is NOT the digit '0' (which is `'0'`), and it is NOT `null` (a char can never be null). If you print it you usually see nothing or a blank, and `(int) myChar` is `0`. - `boolean` → `false` A **reference type** is anything that is not primitive: every object, every array, `String`, your own classes, wrapper types like `Integer`. Their default is always **`null`**, meaning 'points to no object'. Note this catch: a field of type `Integer` (the wrapper class) defaults to `null`, but a field of type `int` (the primitive) defaults to `0` — mixing these up causes `NullPointerException` when the `null` wrapper is auto-unboxed. ## Where these defaults apply 1. **Instance fields** — set when the object is created. 2. **Static fields** — set when the class is first loaded/initialized. 3. **Array elements** — when you create an array with `new int[3]`, all three slots are `0`; `new String[3]` gives three `null` elements. The defaulting is by the array's element type. ## The big exception: local variables A **local variable** is one declared inside a method, constructor, or block. Local variables get **no** default value. The Java compiler enforces a rule called **definite assignment**: you must assign a value to a local variable along every path that reaches a use of it, otherwise you get a compile error: 'variable X might not have been initialized'. This is a deliberate safety feature — accidentally reading an unset local is almost always a bug, so the language catches it at compile time instead of silently handing you a default. ## Why the difference exists Fields belong to objects that may be constructed in many ways and live a long time, so a guaranteed safe initial state avoids undefined behavior. Locals are short-lived and the compiler can prove whether they were set, so it demands you do so explicitly, which prevents subtle bugs. ## Order of initialization for fields When an object is built: (1) memory is zeroed to defaults, (2) field initializers and instance-initializer blocks run in source order, (3) the constructor body runs. So a default is only ever visible if you haven't given the field an explicit value yet.

  • What value does the boolean default to, and the char?
    boolean defaults to false; char defaults to '' (the null character, code point 0), not the digit '0'.
  • If a field is Integer (wrapper) rather than int, what's its default and what risk does that create?
    It defaults to null. Using it in arithmetic auto-unboxes it, throwing NullPointerException.

saying these in an interview costs you the question

  • Saying char defaults to '0' the digit — it's '', the null character (code point 0)
  • Claiming local variables also get default values — they require explicit assignment
  • Saying a char can be null — char is a primitive and is never null
  • Thinking an Integer/wrapper field defaults to 0 — it defaults to null

context

open as a page

Why does this not compile: `int x; System.out.println(x);` inside a method, even though an int field would print 0?

level: juniorimportance: must knowfreq 65%

basics

~20 s

Local variables don't get default values. The compiler requires you to assign a value before reading one, so it rejects the code with 'variable x might not have been initialized'. A field would instead default to 0.

open as a page

What is a literal in Java, and what are the main categories of literals the language supports?

level: juniorimportance: must knowfreq 55%

basics

~10 s

A literal is a fixed value written directly in source code, like 42, 3.14, 'A', "hello", true, or null. Java has integer, floating-point, character, string, boolean, and the null literal.

open as a page

What happens when an int computation exceeds Integer.MAX_VALUE in Java, and why?

level: juniorimportance: must knowfreq 70%

basics

~10 s

Java does not throw an error. The value silently wraps around: adding 1 to the largest int gives the smallest (most negative) int. No warning, no exception.

open as a page

What are the default values of primitive fields, and when do defaults apply versus when must you initialize?

level: juniorimportance: must knowfreq 55%

basics

~10 s

Uninitialized primitive fields get defaults: 0 for numbers, 0.0 for float/double, '' for char, false for boolean. Local variables get NO default — you must assign before use or the code won't compile.

open as a page

What are the eight primitive types in Java, and what is the size of each?

level: juniorimportance: must knowfreq 85%

basics

~10 s

Java has eight primitives: byte (8 bits), short (16), int (32), long (64), float (32), double (64), char (16), and boolean. They hold simple values directly, not objects.

open as a page

What is NaN in Java, and why does NaN == NaN evaluate to false? How do you correctly test whether a value is NaN?

level: juniorimportance: must knowfreq 70%

basics

~10 s

NaN means 'Not a Number' and comes from undefined operations like 0.0/0.0. It is never equal to anything, even itself, so NaN == NaN is false. Use Double.isNaN(x) to check for it.

open as a page

What do the L, f, and d suffixes do on numeric literals, and why can the L suffix actually change program behavior?

level: middleimportance: must knowfreq 60%

basics

~20 s

L makes an integer literal a long, f makes a number a float, and d makes it a double. Without L, big literals stay int and can overflow during arithmetic; adding L forces 64-bit math.

open as a page

Why does 0.1 + 0.2 not equal 0.3 with float/double, and what should you use for money?

level: middleimportance: must knowfreq 70%

basics

~10 s

float and double store numbers in binary (base 2), and many decimals like 0.1 can't be represented exactly, so tiny rounding errors creep in. For money, use BigDecimal or integer cents, not double.

open as a page

Why does 0.1 + 0.2 not equal 0.3 in Java, and how should you handle floating-point precision and rounding errors?

level: middleimportance: must knowfreq 75%

basics

~20 s

Doubles store numbers in binary, and 0.1, 0.2, 0.3 can't be represented exactly, so tiny rounding errors creep in: 0.1 + 0.2 is 0.30000000000000004. Never compare with == — compare within a tolerance, or use BigDecimal for money.

open as a page

How is char different from the other primitive types in Java?

level: juniorimportance: should knowfreq 45%

basics

~10 s

char is a 16-bit unsigned number holding one UTF-16 character code (0 to 65535). It is the only unsigned primitive, and you can do arithmetic on it because it is numeric under the hood.

open as a page

After `new int[3]` and `new String[3]`, what are the element values, and why don't you have to fill the array first?

level: middleimportance: should knowfreq 55%

basics

~20 s

Creating an array fills every slot with the type's default: new int[3] gives {0,0,0} and new String[3] gives {null,null,null}. The array object's memory is zeroed when allocated, so the elements are already safe to read.

open as a page

How do you write integer literals in hexadecimal, octal, and binary, and what common bug does the octal form cause?

level: middleimportance: should knowfreq 48%

basics

~10 s

Hex uses prefix 0x (0x1F), binary uses 0b (0b1010), octal uses a leading zero (017). The trap: a leading zero makes the number octal, so 010 is 8, not 10.

open as a page

What are string literals and the string constant pool, and how do underscores in numeric literals work? When can you NOT use an underscore?

level: middleimportance: should knowfreq 42%

basics

~20 s

A string literal is text in double quotes; identical literals are shared (interned) in a string pool, so "hi" == "hi" is true. Underscores group digits in numbers for readability, like 1_000_000, but can't sit at the start, end, or next to a dot or base prefix.

open as a page

How does floating-point overflow and underflow behave in Java, and how does it differ from integer overflow?

level: middleimportance: should knowfreq 45%

basics

~10 s

Floating-point doesn't wrap. Too-big results become Infinity (or -Infinity); too-small results lose precision and shrink toward 0.0. Neither throws an exception. Integer overflow instead wraps around silently.

open as a page

How do Math.addExact and Math.multiplyExact help with integer overflow, and when would you use them?

level: middleimportance: should knowfreq 55%

basics

~10 s

They do the same math as + and *, but throw an ArithmeticException if the result overflows instead of silently wrapping. Use them where a wrong number would be dangerous.

open as a page

What happens when an int arithmetic operation exceeds its range, and how do you detect or avoid it?

level: middleimportance: should knowfreq 60%

basics

~10 s

The value silently wraps around: adding 1 to the maximum int (2147483647) gives the minimum int (-2147483648). Java does not throw an error. Use a long, or Math.addExact to throw on overflow.

open as a page

Explain widening and narrowing conversions between primitive types and when casts are required.

level: middleimportance: should knowfreq 50%

basics

~20 s

Widening (small to big, like int to long) happens automatically because no data is lost. Narrowing (big to small, like long to int) can lose data, so Java forces you to write an explicit cast.

open as a page

How are positive and negative infinity represented in Java floating-point, and how do operations behave with them?

level: middleimportance: should knowfreq 55%

basics

~10 s

Dividing a non-zero number by zero gives infinity (Double.POSITIVE_INFINITY or NEGATIVE_INFINITY) instead of throwing. Infinity arithmetic mostly stays infinite, but undefined combos like Infinity - Infinity give NaN.

open as a page

Do final fields receive default values, and how does that interact with the requirement that a final field must be assigned exactly once?

level: seniorimportance: should knowfreq 35%

basics

~20 s

A final field is briefly default-initialized (0/false/null) like any field, but the compiler still forces you to assign it exactly once before the constructor finishes. So you can't rely on the default as its final value.

open as a page

Explain character literals, escape sequences, and Unicode escapes (\uXXXX) in Java. What is unusual about how \uXXXX is processed?

level: seniorimportance: should knowfreq 38%

basics

~20 s

A char literal is one character in single quotes, like 'A'. Escapes like '\n' or '\t' write special characters. 'A' is a Unicode escape for 'A'. The surprise: \uXXXX is processed very early, before the code is even tokenized.

open as a page

How do floating-point literals and scientific notation work in Java, including special values and the difference between float and double precision?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Floating-point literals have a decimal point or an exponent: 3.14, 1.5e3 (means 1500), .5, 2f. Without a suffix they are double (64-bit, more precise); with f they are float (32-bit, less precise). There is no literal for infinity or NaN — those come from constants.

open as a page

Why is `int mid = (low + high) / 2` a bug in binary search, and how do you fix it?

level: seniorimportance: should knowfreq 50%

basics

~10 s

For large arrays, low + high can exceed Integer.MAX_VALUE and overflow to a negative number, so mid becomes wrong (often negative). Fix it with low + (high - low) / 2, which never overflows.

open as a page

Across NaN, infinities, and negative zero, why do Java's primitive == / < / > disagree with Double.compare and Double.equals, and what rule should you follow when implementing equals/hashCode or sorting doubles?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Primitive ==/</> follow IEEE 754: NaN is never equal (even to itself) and -0.0 equals 0.0. Double.compare/Double.equals instead define a clean total order: NaN equals itself and sorts last, and -0.0 sorts below +0.0. For equals/hashCode and sorting, use Double.compare.

open as a page

What is negative zero (-0.0) in Java? Since -0.0 == 0.0 is true, how can the two be distinguished, and when does the distinction matter?

level: seniorimportance: nice to knowfreq 30%

basics

~10 s

Floating-point has two zeros: +0.0 and -0.0. They compare equal with ==, but you can tell them apart with Double.compare or by checking 1.0/x (gives +Infinity for +0.0, -Infinity for -0.0).

open as a page

As a system grows, how do you decide between int, long, BigInteger/BigDecimal, and checked arithmetic to manage overflow risk?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Match the type to the value range and the cost of being wrong: int for small bounded counts, long for big counts/IDs/time, BigInteger/BigDecimal when values are unbounded or must be exact (money), and Math.*Exact at trust boundaries to fail loudly instead of wrapping.

open as a page