Shapes.java contains public class Shapes with a nested class Point inside it, and a second, non-public top-level class Helper. Main is in Shapes. What does javac Shapes.java produce, and what happens on java Shapes?
Java compiles source into platform-neutral bytecode that the JVM loads, verifies and runs (interpreter + JIT), giving 'write once, run anywhere'. Know its history, the buzzword features, JDK/JRE/JVM, the JVM's components, main(), primitive sizes and javap.
javac Hello.java → Hello.class (bytecode, one file per class) → java Hello runs it on the JVM. Every class file starts with the magic number 0xCAFEBABE.public static void main(String[] args): public (the JVM calls it), static (no object needed), void (returns nothing), args = command-line arguments (args[0] is the first).In 1991 a small Sun Microsystems team — James Gosling, Mike Sheridan and Patrick Naughton, the 'Green Team' — began the Green Project to program consumer devices such as set-top boxes. Those devices used many different processors, so the team wanted one language whose programs would run on all of them. Gosling's language was first called Oak, after a tree outside his office; a trademark clash forced a new name, and it was renamed Java (after the coffee) in 1995.
Java's public launch in 1995 coincided with the rise of the web: the HotJava browser and then Netscape Navigator 2.0 could run Java applets inside web pages, and the language became famous almost overnight. JDK 1.0 shipped in January 1996. Sun open-sourced the platform as OpenJDK in 2006–07, and Oracle acquired Sun in 2010.
| Release | Year | Landmark additions |
|---|---|---|
| JDK 1.0 | 1996 | First release: applets, AWT, the core libraries |
| JDK 1.1 | 1997 | Inner classes, JDBC, RMI, the event delegation model |
| J2SE 1.2 ('Java 2') | 1998 | Swing, the Collections Framework, a JIT compiler in the standard JVM |
| J2SE 1.4 | 2002 | assert, NIO, regular expressions |
| J2SE 5.0 | 2004 | Generics, autoboxing, enums, enhanced for loop, varargs, annotations |
| Java SE 7 | 2011 | try-with-resources, the diamond <>, strings in switch, multi-catch |
| Java SE 8 (LTS) | 2014 | Lambdas, streams, default methods in interfaces |
| Java SE 9 | 2017 | Modules (JPMS), JShell; the Applet API deprecated |
| Java SE 11 (LTS) | 2018 | appletviewer and the browser plug-in removed; HTTP Client; single-file launch |
| Java SE 17 / 21 / 25 (LTS) | 2021 / 2023 / 2025 | Sealed classes; virtual threads and switch pattern matching; compact source files and instance main methods |
Java comes in editions: Java SE (Standard Edition, the core platform), Java EE (Enterprise Edition, now Jakarta EE under the Eclipse Foundation) for servers, Java ME (Micro Edition) for small devices, and Java Card for smart cards.
Sun's 1995 white paper described Java with a famous list of buzzwords. Indian textbooks (Balagurusamy) present the same ideas as 'compiled and interpreted', 'platform-independent and portable' and so on. Each has a concrete mechanism behind it:
| Feature | Meaning | How Java achieves it |
|---|---|---|
| Simple | Easy to learn for C/C++ programmers | C-like syntax without pointers, operator overloading, multiple class inheritance, header files or goto |
| Object-oriented | Programs are built from classes and objects | Everything lives inside classes; only the 8 primitive types are not objects |
| Platform-independent, portable | Write once, run anywhere (WORA) | Bytecode runs on any JVM; primitive sizes are fixed (int is always 32 bits) |
| Architecture-neutral | Compiled code is not tied to any CPU or OS | One class-file format for every platform |
| Robust | Programs fail safely and predictably | Strong typing, compile-time checks, exceptions, no pointer arithmetic, garbage collection, array bounds checks |
| Secure | Untrusted code cannot harm the host | Bytecode verifier, class-loader isolation, no raw memory access; historically the applet sandbox |
| Compiled and interpreted | Two-stage execution | javac produces bytecode; the JVM interprets it and the JIT compiles hot code to native code |
| High performance | Speed close to native programs | JIT compilation (HotSpot) and efficient garbage collectors |
| Multithreaded | Several tasks run concurrently | Thread class, synchronized, wait/notify, java.util.concurrent |
| Distributed | Built for networked programs | java.net (sockets, URLs) and RMI |
| Dynamic | Adapts while running | Classes are loaded on demand; reflection and run-time type information |
A C or C++ compiler translates source code into native machine code for one processor and operating system; to run elsewhere you must recompile. The Java compiler instead translates source into bytecode — instructions for an imaginary computer, the Java Virtual Machine. Each platform has its own JVM, and every JVM runs the same class files.
javac Hello.java checks syntax and types and writes Hello.class. Every class, including nested ones, gets its own file (Outer$Inner.class).java Hello (no .class extension) starts a JVM, which loads, verifies and initialises the class.public class Hello {
public static void main(String[] args) {
System.out.println("Hello, KSHB!");
}
}$ javac Hello.java # compile: creates Hello.class (bytecode)
$ java Hello # run: the JVM loads Hello.class and calls main
Hello, KSHB!
$ java Hello.java # Java 11+: compile in memory and run a single source fileBytecode is a compact instruction set for a stack machine: each operation code (opcode) is one byte — hence 'byte code' — so there can be at most 256 of them (about 200 are defined). The bytecode is platform-independent; the JVM itself is platform-dependent (there is a JVM for Windows on x86, another for Linux on ARM, and so on).
The JVM is three things at once: a specification (the JVM Specification), an implementation (HotSpot in OpenJDK, Eclipse OpenJ9, GraalVM) and a running instance started by the java command. The JRE (Java Runtime Environment) packages a JVM with the core class libraries — what an end user needed just to run programs. The JDK (Java Development Kit) adds the tools a developer needs.
| Tool | Purpose |
|---|---|
| javac | Compiler: .java source → .class bytecode |
| java | Launcher: starts a JVM and runs a class (or a JAR with -jar) |
| javadoc | Generates HTML API documentation from the doc comments in the source |
| jar | Packages classes and resources into a .jar archive |
| jdb | Command-line debugger |
| javap | Class-file disassembler: lists members and bytecode |
| jshell | Interactive read-eval-print loop (Java 9) |
| jlink | Builds a custom, minimal runtime image (Java 9) |
| appletviewer | Ran applets without a browser — removed in Java 11 |
| javah | Generated C headers for native methods — removed in Java 10 (javac -h replaces it) |
| Area | Shared or per thread | Holds |
|---|---|---|
| Method area (HotSpot: Metaspace since Java 8) | Shared by all threads | Class metadata, method bytecode, the runtime constant pool and, in the specification's model, static variables (HotSpot stores their values with the Class object on the heap) |
| Heap | Shared by all threads | Every object and array; managed by the garbage collector |
| JVM stack | One per thread | A frame per method call: local variables, operand stack, frame data |
| PC register | One per thread | Address of the bytecode instruction being executed (undefined during native methods) |
| Native method stack | One per thread | Frames of native (C/C++) methods called through JNI |
The execution engine runs the code. Its interpreter executes bytecode one instruction at a time — quick to start, slow in loops. The JIT (just-in-time) compiler — HotSpot's C1 and C2 compilers, used in tiers — profiles the running program and compiles hot methods into optimised native code. The garbage collector reclaims unreachable heap objects (G1 has been the default collector since Java 9). The Java Native Interface (JNI) connects Java to native libraries.
StackOverflowError; too many live objects exhaust the heap (or Metaspace) → OutOfMemoryError.No pointers. Java removed explicit pointers for security (code downloaded from a network cannot poke at arbitrary memory), robustness (no dangling pointers, buffer overruns or pointer-arithmetic bugs) and to let the garbage collector know exactly where every reference is — it can even move objects. Java has references: they point to objects but cannot be incremented, converted to numbers or aimed at stack variables.
No multiple inheritance of classes. A class has exactly one superclass, which avoids the diamond problem and the complicated rules C++ needs (virtual base classes). Multiple inheritance of type is available through interfaces.
| C/C++ feature | In Java |
|---|---|
| Pointers and pointer arithmetic | References only |
| Multiple inheritance of classes | Single class inheritance plus multiple interfaces |
| Operator overloading | Not available (only built-in + for strings) |
| Global variables and functions | Everything inside classes; static members instead |
| #define, #include, header files | No preprocessor; packages and import |
| struct, union, typedef | Classes only |
| Destructors and delete | Garbage collection; try-with-resources |
| goto | A reserved keyword that is never used (labelled break/continue instead) |
| Default arguments | Method overloading instead |
| Unsigned integer types | Only char is unsigned |
| Templates | Generics (implemented by erasure) |
public class Sum {
public static void main(String[] args) { // the method the JVM looks for
int total = 0;
for (String a : args) // args holds the command-line words
total += Integer.parseInt(a);
System.out.println(args.length + " numbers, total " + total);
}
}
// $ java Sum 10 20 30 -> 3 numbers, total 60
// $ java Sum -> 0 numbers, total 0 (args is an empty array, never null)new Sum().System.exit(status) to report a status code.args[0] is the first argument, not the program name (unlike C's argv[0]).String args[], String... args, static public, and modifiers such as final or synchronized. A wrong signature still compiles, but launching fails with 'Main method not found'.void main() in small programs and 'compact source files' without a class declaration. Exams still expect the classic public static void main(String[] args).| Type | Size | Range / values | Default value of a field |
|---|---|---|---|
| byte | 1 byte (8 bits) | −128 to 127 | 0 |
| short | 2 bytes | −32,768 to 32,767 | 0 |
| int | 4 bytes | −2³¹ to 2³¹ − 1 (about ±2.1 billion) | 0 |
| long | 8 bytes | −2⁶³ to 2⁶³ − 1; literal suffix L | 0L |
| float | 4 bytes | IEEE 754 single precision, about 6–7 significant digits; suffix f | 0.0f |
| double | 8 bytes | IEEE 754 double precision, about 15–16 significant digits | 0.0d |
| char | 2 bytes | Unsigned UTF-16 code unit: '\u0000' to '\uffff' (0 to 65,535) | '\u0000' |
| boolean | Not precisely defined (JVM-dependent) | Only true or false — never 0/1, not convertible to int | false |
Literals: 0x1F (hex), 017 (octal), 0b101 (binary, Java 7), 1_000_000 (underscores, Java 7), 'A' (char), "text" (a String object, not a primitive). A floating literal is a double by default, so float f = 3.14; is a compile error — write 3.14f. Local variables have no default value and must be assigned before use; the defaults above apply to fields and array elements.
In arithmetic, byte, short and char are promoted to int first, so byte b = 10; b = b * 2; fails ('possible lossy conversion from int to byte'), while the compound b *= 2; compiles because it includes a hidden cast. Widening is automatic along byte → short → int → long → float → double (and char → int); narrowing needs a cast and keeps only the low bits: (byte) 200 is −56. Integer overflow wraps silently, and integer division truncates.
public class Types {
public static void main(String[] args) {
byte b = 10;
// b = b * 2; // error: possible lossy conversion from int to byte
b *= 2; // OK: compound assignment casts back -> 20
char c = 'A';
c++; // 'B'
System.out.println(b + " " + c + " " + (c + 1) + " " + (char) (c + 1)); // 20 B 67 C
int big = Integer.MAX_VALUE;
System.out.println(big + 1); // -2147483648 (wraps around)
System.out.println((byte) 200); // -56 (200 - 256)
System.out.println(7 / 2 + " " + 7 / 2.0); // 3 3.5
System.out.println(10 + 20 + "Java" + 10 + 20); // 30Java1020
}
}10 + 20 + "Java" is "30Java", but "Java" + 10 + 20 is "Java1020".The JDK's javap disassembler shows what the compiler produced. javap Hello lists the class's members; javap -c Hello adds the bytecode of every method; -p includes private members; -v (verbose) also prints the constant pool and the class-file version.
$ javap -c Hello
Compiled from "Hello.java"
public class Hello {
public Hello();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #13 // String Hello, KSHB!
5: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}Read it as a stack machine. The compiler added a default constructor: aload_0 pushes this, and invokespecial calls Object's constructor. In main, getstatic pushes the System.out object, ldc pushes the string constant, invokevirtual pops both and calls println, and return ends the method. Likewise int c = a + b; (with a, b, c in local slots 1–3) compiles to iload_1, iload_2, iadd, istore_3.
| Instruction | Used for |
|---|---|
| invokevirtual | Ordinary instance methods — dynamic dispatch on the object's class |
| invokestatic | Static methods |
| invokespecial | Constructors, private methods and super.method() calls — no dynamic dispatch |
| invokeinterface | Methods called through an interface type |
| invokedynamic | Call sites linked at run time (Java 7); used for lambdas and string concatenation |
Every class file begins with the magic number 0xCAFEBABE, followed by its version: major version = Java release + 44 (Java 8 → 52, Java 11 → 55, Java 17 → 61, Java 21 → 65). Running a class compiled for a newer release on an older JVM fails with UnsupportedClassVersionError.
Try the problem first, then reveal the working one step at a time.
Shapes.java contains public class Shapes with a nested class Point inside it, and a second, non-public top-level class Helper. Main is in Shapes. What does javac Shapes.java produce, and what happens on java Shapes?
Evaluate: (a) 1 + 2 + "3" + 4 + 5, (b) 'A' + 1, (c) "" + 'A' + 1, (d) (char) ('A' + 2), (e) 5 / 2 * 2.0, (f) (byte) 130.
For the Sum program in Section 6, what happens with java Sum 10 20 30, java Sum, and java Sum 10 abc? What is args[0] in the first case?
Name the JVM component or area involved: (a) java.lang.String is loaded at start-up; (b) a class file whose jump instruction lands in the middle of another instruction is rejected; (c) static int count; holds 0 before any static block runs; (d) a recursive method without a base case crashes; (e) a loop run a million times speeds up after a while; (f) a static list keeps growing until memory runs out.
Compute the ranges of short and long, and the values of (byte) 200 and (short) 70000.
Which JDK tool would you use to: (a) see the bytecode inside a class file, (b) generate HTML documentation, (c) bundle classes into one archive, (d) debug from the command line, (e) try out a statement interactively?
Java is called architecture-neutral, yet a Windows JVM cannot run on Linux. Explain why both statements are true.
Name the runtime data area that holds: (a) a local variable of a method, (b) an object created with new, (c) a static variable, (d) the address of the next instruction for a thread, (e) frames of a C function called through JNI. Which of them are shared between threads?
On a classic JDK (Java 24 or earlier), which of these can launch the program? (a) public static void main(String args[]) (b) static public void main(String... a) (c) public void main(String[] args) (d) public static int main(String[] args) (e) public static void Main(String[] args)
char is 2 bytes (a UTF-16 code unit), not 1 as in C; boolean is not a number and has no specified size.args[0] is the first command-line argument, not the program name; with no arguments, args is an empty array, not null.12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.