What is the difference between JDK, JRE and JVM?
The JVM runs bytecode, the JRE is the JVM plus the standard library, and the JDK is the JRE plus tools such as
javacandjar. Since Java 11 Oracle ships no separate JRE: you install a JDK, or build a trimmed runtime withjlink.Why is Java platform independent? Is the JVM platform independent?
javaccompiles to bytecode, not native code, and any machine with a JVM runs it. The JVM itself is not platform independent: each OS and CPU has its own build, which turns the same bytecode into native instructions. More in what is Java.Why is the main method
public static void main(String[] args)?It is the exact signature the JVM calls:
publicso it is reachable from outside,staticbecause no object exists yet,voidbecause exit codes go throughSystem.exit, andString[] argsfor the command line arguments.Overloads are allowed, but the JVM never calls them. Instance main methods (
void main()) were a preview in Java 21 to 24 and became final in Java 25 with JEP 512.What are the primitive data types in Java?
Eight:
byte(8 bits),short(16),int(32),long(64),float(32),double(64),char(16, one UTF-16 unit) andboolean. Everything else, includingStringand arrays, is a reference type.Javapublic class Main { public static void main(String[] args) { System.out.println("int max: " + Integer.MAX_VALUE); System.out.println("long max: " + Long.MAX_VALUE); System.out.println("char 'A' as int: " + (int) 'A'); System.out.println("byte range: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE); } }The sizes are fixed by the language spec, unlike in C. See the Java data types guide.
What happens when you add 1 to
Integer.MAX_VALUE?Javapublic class Main { public static void main(String[] args) { int x = Integer.MAX_VALUE; x++; System.out.println(x); } }It prints
-2147483648.intarithmetic is 32-bit two's complement, soMAX_VALUE + 1silently wraps toInteger.MIN_VALUE.Use
Math.addExactto throwArithmeticExceptionon overflow, or switch tolongorBigInteger.Practice it: Factorial
What do
7 / 2,7 % 2and7 / 2.0return in Java?Javapublic class Main { public static void main(String[] args) { System.out.println(7 / 2 + " " + 7 % 2 + " " + 7 / 2.0); } }It prints
3 1 3.5.7 / 2is int division and truncates toward zero,7 % 2is the remainder, and7 / 2.0widens todouble. For an average of ints, write(double) sum / count.What does
1 + 2 + "3" + 4 + 5print in Java?Javapublic class Main { public static void main(String[] args) { System.out.println(1 + 2 + "3" + 4 + 5); } }It prints
3345.+runs left to right:1 + 2is addition,3 + "3"is concatenation, and every+after a string concatenates. Write"sum: " + (4 + 5)to add first.What happens in a Java
switchwithoutbreak?Javapublic class Main { public static void main(String[] args) { int x = 2; switch (x) { case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); default: System.out.print("other"); } System.out.println(); } }It prints
two three other. Colon-style cases fall through: after a match, execution runs into every following case until abreak. The arrow form from Java 14 (case 2 -> ...) never falls through.What is type casting in Java? What is widening and narrowing?
Casting converts a value to another type. Widening (
inttolongordouble) is automatic and safe; narrowing (doubletoint) needs an explicit cast and can lose data.Javapublic class Main { public static void main(String[] args) { int i = 100; long l = i; // widening, automatic double d = 3.99; int truncated = (int) d; // narrowing drops the fraction byte b = (byte) 200; // keeps the low 8 bits System.out.println(l + " " + truncated + " " + b); } }It prints
100 3 -56: the cast truncates 3.99 instead of rounding, and(byte) 200keeps only the low 8 bits.What are wrapper classes and autoboxing in Java?
Wrapper classes (
Integer,Double,Characterand the rest) let primitives be used as objects, for example in collections. Autoboxing is the compiler convertinginttoIntegerwithInteger.valueOf; unboxing is the reverse.Two traps: unboxing a
nullthrowsNullPointerException, and==on twoIntegers compares references.What is the difference between
final,finallyandfinalizein Java?They are unrelated.
finalblocks reassigning a variable, overriding a method or extending a class.finallyruns aftertry/catchwhether or not an exception was thrown.finalize()was a garbage collector hook, deprecated in Java 9 and for removal in Java 18; use try-with-resources orCleaner.What does the
statickeyword mean in Java?A
staticmember belongs to the class, so all objects share one copy.Javaclass Counter { static int created = 0; // shared by all objects int id; // one per object Counter() { created++; id = created; } } public class Main { public static void main(String[] args) { Counter a = new Counter(); Counter b = new Counter(); System.out.println(a.id + " " + b.id + " " + Counter.created); } }It prints
1 2 2. Static methods cannot usethisand are hidden, not overridden. More in static members.What is a constructor in Java? Can a constructor be private?
A constructor initializes a new object: it has the class's name and no return type, and the compiler adds a no-argument one only if you write none.
Yes, it can be
private, which blocksnewfrom other classes: that is how singletons, utility classes likeMathand factories likeList.ofwork. Until Java 25, when JEP 513 relaxed the rule,this(...)orsuper(...)had to be the first statement.
Java Basics Interview Questions
Campus placement basics: the JDK and JVM, data types, operators and their output puzzles.