skip to content

Java Language Fundamentals

The base layer of the language: syntax, types, variables, operators, control flow and arrays. Junior interviews live here, and senior ones return to it whenever a puzzle depends on integer overflow, char arithmetic or definite assignment.

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

explore

questions

185 · 6 sections

What is a block statement in Java, and what does it have to do with variable scope?

level: juniorimportance: must knowfreq 55%
basics
~20 s

A block is code wrapped in curly braces { }. It groups statements together. A variable you declare inside a block can only be used inside that block; once the block ends, the variable is gone.

open as a page

What kinds of comments does Java support, and what is the syntax for each?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Java has three: single-line starting with //, multi-line wrapped in /* ... /, and Javadoc written as /* ... */ which is a documentation comment the javadoc tool reads. All of them are ignored by the compiler.

open as a page

What are the standard Java naming conventions for identifiers, and how do conventions differ from the compiler's rules?

level: juniorimportance: must knowfreq 60%
basics
~20 s

Rules are what the compiler enforces; conventions are agreed style. By convention: classes use UpperCamelCase, variables and methods use lowerCamelCase, constants use UPPER_SNAKE_CASE, and packages are all lowercase. Breaking a convention still compiles, but it's bad style.

open as a page

What are keywords (reserved words) in Java, and why can't you use them as identifiers?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Keywords are words Java reserves for its own grammar (like class, if, return, int). The compiler treats them specially, so you can't use them to name your own variables, methods, or classes.

open as a page

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%
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.

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 is definite assignment in Java, and why does the compiler reject reading a local variable that might be unassigned?

level: juniorimportance: must knowfreq 62%
basics
~20 s

Definite assignment is a compile-time check that every local variable is given a value before you read it. If the compiler can find any path where the variable might not be set yet, it refuses to compile and reports a "variable might not have been initialized" error.

open as a page

What does the final keyword mean when applied to a local variable, and what happens if you try to reassign it?

level: juniorimportance: must knowfreq 70%
basics
~10 s

final means the variable can be assigned a value only once. After that first assignment you cannot change it. Trying to reassign it is a compile error, so the program will not even build.

open as a page

What is variable shadowing in Java, and can you give a common example?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Shadowing happens when a variable in an inner scope has the same name as one in an outer scope, so the inner one hides the outer one. A classic case is a constructor parameter named the same as a field.

open as a page

What are the three kinds of variables in Java, and how do they differ in where they live and how long they exist?

level: juniorimportance: must knowfreq 85%
basics
~20 s

Local variables live inside a method and disappear when it ends. Instance variables belong to each object and live as long as the object. Static variables belong to the class itself, so there is just one copy shared by everything.

open as a page

What is `var` in Java, and what does it actually do to a local variable declaration?

level: juniorimportance: must knowfreq 70%
basics
~20 s

var lets you declare a local variable without writing its type; the compiler figures out the type from the value you assign. The variable still has a fixed type — var just saves you from typing it.

open as a page

What are the arithmetic operators in Java and what does each one do?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Java has +, -, *, / and % for add, subtract, multiply, divide, and remainder. There are also unary + and - that keep or flip a number's sign.

open as a page

What is the simple assignment operator (=) in Java, and what does an assignment expression evaluate to?

level: juniorimportance: must knowfreq 55%
basics
~20 s

The = operator stores the value on its right into the variable on its left. An assignment is also an expression whose result is the value that was assigned, so you can write a = b = 5 and both become 5.

open as a page

What do Java's bitwise operators &, |, ^, and ~ do, and how do they differ from the logical operators && and ||?

level: juniorimportance: must knowfreq 62%
basics
~20 s

&, |, ^ compare two numbers bit by bit (AND, OR, XOR); ~ flips every bit. They work on whole numbers. && and || work on true/false, only look at the second value if needed, and give back true or false.

open as a page

What does the instanceof operator do in Java, and what type of value does it produce?

level: juniorimportance: must knowfreq 78%
basics
~10 s

instanceof checks whether an object is an instance of a given type (class or interface). It returns a boolean: true if the object's type matches that type or a subtype, otherwise false.

open as a page

What are Java's logical operators &&, ||, and !, and what does each do?

level: juniorimportance: must knowfreq 70%
basics
~20 s

&& is logical AND: true only if both sides are true. || is logical OR: true if at least one side is true. ! is NOT: it flips a boolean, turning true into false and false into true.

open as a page

What are autoboxing and unboxing in Java, and when does the compiler insert them?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Autoboxing is the automatic conversion of a primitive (like int) to its wrapper object (Integer). Unboxing is the reverse. The compiler inserts these conversions for you when a primitive is used where an object is expected, or vice versa.

open as a page

What is binary numeric promotion in Java, and what type results when you add a byte and a short?

level: juniorimportance: must knowfreq 55%
basics
~10 s

When you do arithmetic on two numbers, Java converts both to a common type before computing. Anything smaller than int (byte, short, char) becomes int first. So byte + short gives an int.

open as a page

What is a widening primitive conversion in Java, and why does it happen automatically?

level: juniorimportance: must knowfreq 62%
basics
~20 s

Widening turns a smaller numeric type into a bigger one, like int to long or int to double. Java does it automatically because the bigger type can always hold the value, so no data is lost.

open as a page

What is the difference between widening and narrowing reference conversion in Java (upcasting vs downcasting)?

level: juniorimportance: must knowfreq 78%
basics
~10 s

Upcasting treats an object as one of its parent types and happens automatically. Downcasting treats it as a more specific child type and must be written explicitly with a cast like (Dog) animal.

open as a page

What are wrapper classes in Java, and why do they exist?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Wrapper classes like Integer, Long, Double, Boolean, and Character are object versions of the eight primitive types. They exist because generics and collections only hold objects, not primitives, and they add helper methods and constants.

open as a page

What do the break and continue statements do inside a loop in Java?

level: juniorimportance: must knowfreq 75%
basics
~10 s

break stops the loop entirely and jumps to the code right after it. continue skips the rest of the current iteration and goes straight to the next one (re-checking the loop condition).

open as a page

What is the enhanced for-each loop in Java and what is its syntax?

level: juniorimportance: must knowfreq 70%
basics
~10 s

It is a shorter loop for going through every element of an array or collection without a counter. You write 'for (Type item : collection)' and 'item' takes each value in turn.

open as a page

What are the three parts of a Java for loop, and in what order do they execute?

level: juniorimportance: must knowfreq 85%
basics
~10 s

A for loop has three parts in parentheses, separated by semicolons: initialization, condition, and update. Init runs once first; then each pass checks the condition, runs the body, then runs the update.

open as a page

What is the if statement in Java and how does the optional else clause work?

level: juniorimportance: must knowfreq 70%
basics
~10 s

An if statement runs a block of code only when its condition is true. An optional else block runs when the condition is false. The condition must be a boolean.

open as a page

How do else-if chains work in Java, and how does ordering affect which branch runs?

level: juniorimportance: must knowfreq 60%
basics
~10 s

An else-if chain tests conditions in order from top to bottom. The first condition that is true runs its block, and the rest are skipped. An optional final else catches everything else.

open as a page