Java follows the principle of "Write Once, Run Anywhere (WORA)", where source code is compiled into platform-independent bytecode instead of native machine code.
Java Platform
The Java platform consists of three major components.1. JDK (Java Development Kit) provides everything required to develop Java applications. It contains the compiler, debugger, documentation tools, archiving tools, JShell, JRE, and many utilities.
2. JRE (Java Runtime Environment) provides the runtime environment required to execute Java applications. It includes the JVM and standard Java libraries but does not contain development tools.
3. JVM (Java Virtual Machine) is responsible for loading classes, verifying bytecode, executing bytecode, managing memory, performing garbage collection, and providing runtime services.
The relationship is: JDK → contains JRE → contains JVM.
Java Compilation and Execution
Java programs are written in .java source files. The Java compiler (javac) compiles the source code into .class files containing bytecode.
The JVM executes bytecode instead of Java source code. During execution, the JVM interprets bytecode and uses the Just-In-Time (JIT) Compiler to convert frequently executed code into native machine instructions for better performance.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Compile and execute the program.
javac HelloWorld.java
java HelloWorld
Execution flow: Source Code (.java) → javac → Bytecode (.class) → JVM → Machine Code → Operating System
Java source code is compiled only once into bytecode. Every operating system has its own JVM implementation capable of executing the same bytecode. A Java application compiled on Windows can execute on Linux or macOS without recompilation, provided a compatible JVM is available.
Structure of a Java Source File
A Java source file may contain the following components.package com.example.demo;
import java.util.List;
public class Demo {
public static void main(String[] args) {
}
}
The typical order is:
- Package Declaration - Import Statements
- Class / Interface / Enum / Record Declaration
- Fields
- Constructors
- Methods
- Nested Types
Only one public top-level class is allowed per source file, and its filename must exactly match the class name.
Entry Point
Execution starts from the main() method.public static void main(String[] args)
Each keyword has a specific purpose.
- public allows the JVM to invoke the method.- static allows invocation without creating an object.
- void indicates no return value.
- String[] args contains command-line arguments.
Since Java 21, the language supports simplified source files for small programs, but the traditional
main() method remains the standard approach for production applications.
Packages
A package groups related classes and prevents naming conflicts.package com.backendml.orders;
Benefits include better organization, modularity, access control, and namespace management. Package names conventionally use lowercase reverse domain notation.
com.backendml.service
com.backendml.model
com.backendml.repository
Imports
The import statement allows using classes without specifying their fully qualified names.import java.util.List;
import java.util.Map;
Wildcard imports are also supported.
import java.util.*;
Explicit imports are generally preferred because they improve readability and make dependencies clear.
Identifiers
Identifiers are names assigned to packages, classes, interfaces, methods, variables, and constants. Valid identifiers:employee
employeeName
MAX_SIZE
calculateSalary
Identifiers cannot begin with digits, contain spaces, or use Java reserved keywords.
Keywords
Java reserves keywords for language constructs. Examples include:class
interface
enum
record
public
private
protected
static
final
abstract
sealed
non-sealed
permits
extends
implements
return
if
else
switch
try
catch
throw
throws
new
this
super
Keywords cannot be used as identifiers.
Comments
Java supports three types of comments. 1. Single-line comment.// Single-line comment
2. Multi-line comment.
/*
Multiple
line
comment
*/
3. Documentation comment.
/**
* Calculates employee salary.
*/
Documentation comments are processed by the Javadoc tool to generate API documentation.
Assertions
Assertions verify assumptions during development and testing.int age = 25;
assert age >= 18;
Assertions are disabled by default and must be enabled using:
java -ea MyApplication
Assertions should not replace input validation or business rule validation.
Variables
A variable stores data in memory. Variables are categorized as:1. Local Variables: Declared inside methods.
2. Instance Variables: Declared inside a class but outside methods.
3. Static Variables: Declared using the static keyword and shared across all instances.
Detailed coverage of variables and data types is discussed separately.
Primitive and Reference Types
Java categorizes all values into two groups.1. Primitive Types directly store values.
2. Reference Types store references to objects.
int number = 100;
String name = "John";
Primitive types and reference types are covered in detail in the Data Types article.
Object Creation
Objects are created using the new keyword.Employee employee = new Employee();
Object creation involves memory allocation, constructor invocation, and reference assignment. The internal object creation process is discussed in the JVM Internals article.
Java is Pass-by-Value
Java is always pass-by-value. For primitive types, the value itself is copied. For objects, the reference value is copied, not the object itself.class Employee {
String name;
}
Employee employee = new Employee();
Employee another = employee;
Both variables refer to the same object because the object reference value was copied.
Unicode Support
Java internally uses Unicode, allowing source code and strings to contain characters from almost every language.String message = "नमस्ते";
String city = "東京";
Naming Conventions
Java follows widely accepted naming conventions.1. Package names use lowercase.
com.backendml.orders
2. Class, interface, enum, and record names use PascalCase.
OrderService
Employee
CustomerRecord
3. Methods and variables use camelCase.
calculateSalary()
employeeName
4. Constants use uppercase with underscores.
MAX_CONNECTIONS
DEFAULT_TIMEOUT
5. Generic type parameters commonly use single uppercase letters.
T
K
V
E
JShell
JShell is Java's interactive Read-Eval-Print Loop (REPL). It allows executing Java statements without creating complete source files.jshell
jshell> int x = 10
jshell> x * 5
JShell is useful for experimentation, API exploration, and quick validation of code snippets.
Preview Features
Java introduces some language features as Preview Features before making them permanent. Preview features are disabled by default. They must be explicitly enabled during compilation and execution.javac --enable-preview --release 25 Demo.java
java --enable-preview Demo
Preview features allow developers to evaluate new language capabilities before final standardization.
Important Facts
1. Java source files use the .java extension.2. Compiled bytecode uses the .class extension.
3. Java is case-sensitive.
4. Every application executes inside a JVM.
5. The JVM executes bytecode, not Java source code.
6. The JDK includes the JRE.
7. The JRE includes the JVM.
8. Java always uses pass-by-value.
9. Java supports automatic memory management through garbage collection.
10. Java source code is compiled once but can execute on multiple operating systems without recompilation.