Java Core Fundamentals
1. Architecture & Execution Model
1.1 WORA / CORA Concept
Write Once, Run Anywhere (WORA) / Compile Once, Run Anywhere (CORA).
Java source code is compiled into platform-independent bytecode, which the JVM then
interprets and executes on any target OS/CPU.
1.2 Compilation & Execution Flow
[Link] → [javac] → .class (bytecode) → [JVM / Interpreter] →
Machine Code (in memory)
1.3 Platform Independence
● Java programs are platform-independent — bytecode (.class) is not native binary.
● The JVM acts as interpreter/translator between bytecode and the host OS.
● The JVM itself is platform-dependent — separate JVM builds exist for Windows,
Linux, macOS, etc.
1.4 JVM / JRE / JDK
● JVM — Engine that executes Java bytecode.
● JRE — Runtime environment: JVM + standard class libraries.
● JDK — Full development kit: JRE + compiler (javac) + build tools.
2. Core Language Fundamentals
2.1 Arrays
A fixed-size, indexed container storing elements of a single type.
int[] numbers; // Recommended declaration
int numbers[]; // Alternative (valid but not preferred)
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
2.2 Command-Line Arguments
public class CommandLineExample {
public static void main(String[] args) {
for (int i = 0; i < [Link]; i++) {
[Link]("Argument " + i + ": " + args[i]);
}
}
}
# Compile
javac [Link]
# Run
java CommandLineExample Hello 123 World
# Output
Argument 0: Hello
Argument 1: 123
Argument 2: World
2.3 User Input
Scanner scanner = new Scanner([Link]);
2.4 Wrapper Classes
● Autoboxing (Primitive → Object):
int i = 4; Integer j = [Link](i);
● Unboxing (Object → Primitive):
Integer i = new Integer(15); int j = [Link]();
2.5 Primitive Data Types & Memory Sizes
1 byte = 8 bits
Type Size Range (approx) Notes
byte 1 byte (8 bits) -128 to 127
short 2 bytes -32,768 to 32,767
char 2 bytes 0 to 65,535 (Unicode)
int 4 bytes -2^31 to 2^31-1
float 4 bytes ~±3.4e38 Single precision
long 8 bytes -2^63 to 2^63-1
double 8 bytes ~±1.7e308 Double precision
boolean JVM-dependent true / false See note below
Note: The primitive boolean size is not precisely defined by the Java spec — typically 1 byte in arrays, 4 bytes as a
standalone variable on the stack. The wrapper Boolean is an Object reference (4–8 bytes + heap overhead).
3. The Object Class
Any class that does not explicitly extend another class implicitly extends [Link].
Java interfaces do NOT extend Object.
3.1 Key Object Methods
● hashCode() — Returns a hash value for the object.
● equals() — Compares object references by default.
● wait() — Suspends the current thread until notify() or notifyAll() is called.
● notify() — Wakes up a single thread waiting on the object's monitor.
● notifyAll() — Wakes up all threads waiting on the object's monitor.
● toString() — Returns a string representation of the object.
● clone() — Creates and returns a copy of the object.
● finalize() — Called by the GC before object destruction.
Note: finalize() was deprecated in Java 9 and removed in Java 18. Use [Link] or
[Link] instead.
4. Strings & Immutability
4.1 Literal vs. new Instantiation
● String s1 = "Abc"; String s2 = "Abc"; → 1 object created (String Constant Pool).
● String s3 = new String("Abc"); String s4 = new String("Abc"); → 3 objects total (1 in
SCP if not already present + 2 heap objects).
● s1 == s2 → true (same SCP reference).
● [Link](s2) → true (same character content).
Note: The 3-object count applies ONLY if "Abc" was not already in the SCP. If already present (e.g., from s1), then s3 and
s4 create only 2 new heap objects.
4.2 Common Utility Methods
● [Link]() / [Link]()
● [Link](2) / [Link](1, 2)
4.3 Why String is Final / Immutable
● Security — Protects sensitive data (DB URLs, credentials, socket paths) from
unauthorized modification.
● String Pool Optimisation — Permits reuse of literals in the SCP, conserving heap
memory.
● Thread Safety — Read-only instances eliminate synchronisation requirements.
● Hashing / Caching — Guarantees a stable hash code; optimal for HashMap keys.
4.4 How to Create an Immutable Class
● Declare the class as final.
● Mark all fields as private and final.
● Initialise all fields via constructor only.
● Omit setter methods.
● Provide getter methods only; return deep copies of mutable fields:
public ArrayList getListOfStates() {
return (ArrayList) [Link]();
}
Note: clone() performs a shallow copy. For absolute immutability with mutable elements, use [Link](listOfStates)
(Java 10+) or perform a manual deep copy.
5. Object Cloning & Copy Types
Type Code Example Reference Behaviour New Object? Impact of Changes
clone() A a1 = (A) [Link](); New object (shallow Yes Primitives independent;
copy by default) nested refs shared
Shallow Copy A a1 = a; Both point to same No Changes reflect in both
object references
Deep Copy A a1 = new A(a); Fully independent Yes Modifications do not
object graph affect original
6. Environment Variables
● PATH — OS variable used to locate native system executables (.exe, binaries).
● CLASSPATH — JVM / Application ClassLoader variable used to locate compiled .class
files and JAR dependencies.
7. Thread Concurrency & volatile
Without volatile
class Test {
static int var = 5;
}
Multiple threads on separate CPU cores maintain local cache copies of var. Writes from one
thread may not flush to main memory immediately, causing stale reads and concurrency
bugs.
With volatile
class Test {
static volatile int var = 5;
}
Writes flush directly to main memory; reads bypass local CPU caches — ensuring visibility
across all threads.
Note: volatile guarantees visibility but NOT atomicity. Compound operations like var++ are not thread-safe even with
volatile. Use AtomicInteger or a synchronized block instead.
8. Java Core Interview Questions
# Question
1 Can we override static methods in Java?
2 Can you overload the main method in Java?
3 Can we override private methods in Java?
4 What is the base class for all classes in Java?
5 Can you list important methods from the Object class?
6 Which two methods should you override when using a custom object as a key in a
HashMap?
7 What is the difference between HashMap and HashSet in Java?
8 Can we have an abstract class without any abstract methods?
9 What is a transient variable, and when should you use it?
10 Can you call the start() method twice on the same thread in Java?
11 Why is String immutable in Java?
12 How do you make a custom class immutable (list the required steps)?
13 Can we have static methods inside an interface?
14 Can you declare a constructor as final?
15 What is the difference between StringBuffer and StringBuilder?
16 What is the Java CLASSPATH, and how does it differ from PATH?
17 How do you sort a list of custom objects in Java?
18 What is the purpose of the volatile keyword in Java?
19 What are two different ways to explicitly invoke the Garbage Collector in Java?
20 What is a marker interface? Can you provide examples?
21 How many objects will be created in given String literal/new instance scenarios?
22 What is the difference between Checked and Unchecked exceptions?
23 What is the difference between ArrayList and LinkedList, and when would you
select each?
24 What is the difference between wait() and sleep() in Java multi-threading?
Sahi Chai, Roz Wahi Sab
25 You started three worker threads from the main thread. How do you ensure the
main thread completes last?
[Link]