Java Platform Overview
Java is a high-level, class-based, object-oriented programming language originally developed by Sun Microsystems in 1995. Following Oracle's acquisition of Sun in 2009, Java development and distribution are now managed by Oracle Corporation.
The Java platform comprises three primary editions:
- Java SE (Standard Edition): The foundational platform for general-purpose desktop and server applications.
- Java EE (Enterprise Edition): Designed for large-scale, distributed, multi-tiered enterprise applications—now evolved into Jakarta EE under the Eclipse Foundation.
- Java ME (Micro Edition): A lightweight version tailored for embedded systems and resource-constrained devices.
Cross-Platform Execution Model
Java achieves portability through bytecode compilation. Source code written in .java files is compiled by javac into architecture-neutral .class files containing JVM instructions. These bytecode files are interpreted or JIT-compiled at runtime by the Java Virtual Machine (JVM), which is implemented separately for each operating system. As a result, the same .class file runs unchanged on Windows, macOS, Linux, or any other platform with a compatible JVM.
Runtime vs. Development Kits
- JVM: The engine that executes bytecode.
- JRE (Java Runtime Environment): Includes the JVM plus core libraries (e.g.,
java.lang,java.util) required to run Java applications. - JDK (Java Development Kit): Bundles the JRE along with development tools (
javac,javadoc,jdb, etc.) and source code. Installing the JDK suffices for both development and execution.
Installing the JDK
-
Download: Obtain the latest LTS JDK from https://www.oracle.com/java/technologies/javase-downloads.html or use an open-source alternative like Adoptium Temurin. Select the correct package for your OS and architecture (e.g., x64 or ARM64).
-
Install: Run the installer. Avoid default paths containing spaces or non-ASCII characters (e.g.,
C:\Program Files). Prefer clean, English-only paths such asC:\dev\jdk-21. -
Key Directories:
bin: Contains executables (javac,java,jar, etc.).conf: Holds configuration files (e.g.,java.security).lib: Includes core libraries and tools JARs.jmods: Modular JARs used for linking custom runtimes.include: Native header files for JNI development.
Command-Line Basics
Before using IDEs, mastering terminal operations is essential:
| Command | Purpose |
|---|---|
cd <path> |
Navigate into a directory |
cd .. |
Move up one level |
cd /d X: |
Switch drive (Windows) |
dir (Windows) / ls (macOS/Linux) |
List directory contents |
cls (Windows) / clear (macOS/Linux) |
Clear terminal screen |
Configuring the PATH Environment Variable
To invoke JDK tools globally (e.g., javac from any folder), add the JDK’s bin directory to your system’s PATH:
- Windows: System Properties → Advanced → Environment Variables → Edit
PATH→ AddC:\dev\jdk-21\bin. - macOS/Linux: Append
export PATH="/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home/bin:$PATH"to~/.zshrcor~/.bash_profile, then runsource ~/.zshrc.
Verify with java -version and javac -version.
Writing and Running Your First Program
Create a file named Greeting.java:
public class Greeting {
public static void main(String[] arguments) {
System.out.println("Hello, Java!");
}
}
From the terminal, navigate to the file’s location and execute:
javac Greeting.java # Compiles to Greeting.class
java Greeting # Runs the program
Expected output: Hello, Java!
Common Pitfalls and Fixes
- Syntax errors: Ensure all braces
{}, parentheses(), and semicolons;are properly matched. - Case sensitivity:
greeting≠Greeting; class names must match filename exactly. - File extension confusion: Save as
Greeting.java, notGreeting.java.txt. Enable "Show file extensions" in your OS settings. - Compilation vs. execution: Use
javac Greeting.java(with.java) to compile; usejava Greeting(without.class) to run.
Enhanced Text Editing with Notepad++
While basic editors suffice, Notepad++ improves productivity with features like syntax highlighting, line numbering, bracket matching, and encoding control. Install it from https://notepad-plus-plus.org/ and configure:
- Encoding → UTF-8 without BOM
- Language → Java (auto-detects
.java)
Core Syntax Elements
Comments
- Single-line:
// This is ignored by the compiler - Multi-line:
/* Block comment spanning multiple lines */ - Documentation:
/** Javadoc comment for API generation */
Reserved Words
Java reserves certain identifiers (e.g., public, class, static, void) for language constructs. They cannot be used as variable or class names.
Literals
Immutable values directly written in code:
| Type | Example |
|---|---|
| String | "Java rocks" |
| Integer | 42, -7, 0xFF |
| Floating-point | 3.14, 2.99e8f, 6.022e23 |
| Character | 'A', '\n', '🙂' |
| Boolean | true, false |
| Null | null (only for reference types) |
Example usage:
public class LiteralDemo {
public static void main(String[] args) {
System.out.println(123); // int literal
System.out.println(4.56); // double literal
System.out.println('Z'); // char literal
System.out.println(false); // boolean literal
System.out.println("Hello"); // string literal
}
}
Variables
Named memory locations whose contents may change. Declare with type, name, and optional initializer:
int userAge = 28;
double price = 29.99;
String productName = "Laptop";
Multiple declarations (discouraged for clarity):
int x = 10, y = 20, z = 30;
Reassignment:
x = 42; // updates existing variable
Primitive Data Types
Java enforces strict typing. Primitives include:
| Category | Type | Bytes | Range |
|---|---|---|---|
| Integer | byte |
1 | -128 to 127 |
short |
2 | -32,768 to 32,767 | |
int |
4 | -231 to 231−1 | |
long |
8 | -263 to 263−1 | |
| Floating | float |
4 | ±1.4e−45 to ±3.4e38 |
double |
8 | ±4.9e−324 to ±1.8e308 | |
| Text | char |
2 | Unicode 0 to 65,535 |
| Logic | boolean |
— | true or false |
Note: Suffixes L (for long) and F (for float) are required when literals exceed default type ranges.
User Input via Scanner
Read console input using java.util.Scanner:
import java.util.Scanner;
public class InputDemo {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = reader.nextInt();
System.out.println("You are " + age + " years old.");
reader.close();
}
}
Always call close() to release resources.
Identifier Rules
Names for classes, variables, methods, and packages must follow:
- Start with a letter,
$, or_(never a digit). - Contain only letters, digits,
$, or_. - Not match any Java keyword.
- Be case-sensitive (
count≠Count).
Naming conventions:
- PascalCase: Class names (
UserProfile,DataProcessor). - camelCase: Variables and methods (
userName,calculateTotal()). - UPPER_SNAKE_CASE: Constants (
MAX_RETRY_COUNT,DEFAULT_PORT).