Introduction to Java Programming: Core OOP & JVM Architecture
A beginner's guide to compiled bytecode, classes, methods, strong typing, and the Java Virtual Machine.
Overview & Context
Created by James Gosling at Sun Microsystems in 1995, Java is an object-oriented, strongly typed, class-based programming language. Designed around the architectural mandate of 'Write Once, Run Anywhere' (WORA), Java compiles source code into platform-independent bytecode that runs on any operating system equipped with a Java Virtual Machine (JVM). Today, Java powers the enterprise backend systems of global banks, Android mobile apps, and massive distributed infrastructure.
1. The Java Platform Architecture: JVM, JRE, JDK
Bytecode compilation and cross-platform portability
Understanding the three Java acronyms is essential:
- **JDK (Java Development Kit):** The complete software development package containing the compiler (`javac`), debugging tools, and the runtime.
- **JRE (Java Runtime Environment):** The software layer needed to execute compiled Java programs on an end-user device.
- **JVM (Java Virtual Machine):** The virtual computer engine that loads bytecode (`.class` files), verifies code security, executes instructions, and handles automatic garbage collection.
2. Static Typing & Primitive Data Types
Unlike Python or JavaScript, Java is strictly statically typed. You must explicitly declare the data type of every variable before compiling:
- `int`: 32-bit signed integer (e.g. `int attendance = 48;`)
- `double`: 64-bit floating point number for decimals (e.g. `double cgpa = 8.75;`)
- `boolean`: `true` or `false`
- `char`: 16-bit Unicode character (e.g. `char grade = 'A';`)
- `String`: Reference type representing text sequences.
3. Writing Your First Java Program
Anatomy of public class and public static void main
In Java, every line of executable code must live inside a class. Furthermore, the source file name must match the `public class` name exactly (`StudentReport.java`):
// StudentReport.java
public class StudentReport {
// The main method is the entry point of every Java application
public static void main(String[] args) {
String studentName = "Elena Rostova";
int classesConducted = 48;
int classesAttended = 40;
// Calculate attendance rate (cast to double to avoid integer division truncation)
double attendancePercentage = ((double) classesAttended / classesConducted) * 100.0;
System.out.println("Student Name: " + studentName);
System.out.printf("Current Attendance: %.2f%%
", attendancePercentage);
if (attendancePercentage >= 75.0) {
System.out.println("Status: Eligible for Final Examinations");
} else {
System.out.println("Status: Attendance Shortage");
}
}
}4. Core Object-Oriented Principles
Java organizes code around four core Object-Oriented Programming (OOP) pillars:
1. **Encapsulation:** Hiding internal object state behind private fields and providing public getter/setter methods.
2. **Inheritance:** Deriving new classes from existing classes to reuse attributes and behaviors.
3. **Polymorphism:** Allowing subclasses to provide custom implementations of methods defined in their parent class.
4. **Abstraction:** Hiding complex implementation details behind clean interfaces and abstract classes.
5. Common Java Beginner Pitfalls
- **Integer Division:** Writing `int a = 5 / 2;` produces `2`, not `2.5`. Always use `double` literals like `5.0 / 2` when computing fractional percentages.
- **String Comparison:** In Java, never compare strings with `==`. The `==` operator checks if two references point to the exact same memory address! Always compare strings using `.equals()`: `str1.equals(str2)`.
Summary & Key Takeaways
- 1Java compiles source code into portable bytecode executed by the JVM.
- 2Every Java program requires a `public static void main(String[] args)` entry method.
- 3Java is statically typed: all variables require explicit type declarations.
- 4Always compare string contents using `.equals()`, not `==`.
Why does Java use garbage collection?
In older languages like C, developers had to manually allocate and free computer memory, which frequently caused memory leaks and crashes. The Java Virtual Machine automatically identifies and removes objects that are no longer referenced in your program through automatic Garbage Collection (GC).
Is Java related to JavaScript?
No. Despite sharing 'Java' in their names for marketing reasons in 1995, Java and JavaScript are completely distinct languages with entirely different runtimes, syntax rules, object models, and design philosophies.
Recommended Practical Tools
Next Recommended Reads
Discover Python's core design philosophy, interpreted execution model, dynamic typing, and why readability makes it the optimal first language.
Learn how the modern web works. Master semantic HTML5 elements, the CSS box model, Flexbox layout techniques, and mobile-first responsive design.