Java Notes
Java provides platform independence, object-oriented structure, type safety, garbage collection, security checks, multithreading, and JIT optimisation for building reliable applications.
Java is popular because several features work together: the compiler checks code before it runs, the JVM runs bytecode on different systems, and the standard library supplies tools for large applications. A feature is useful only when you understand both its benefit and its limit.
Main features at a glance
| Feature | What it gives a developer | Important limit |
|---|---|---|
| Platform independence | The same bytecode can run on compatible JVMs on different operating systems. | Native libraries, file paths, databases, and environment settings can still differ. |
| Object-oriented design | Classes and objects help organise data and behaviour. | Using classes does not automatically make a design good. |
| Type safety | Many incorrect assignments and method calls fail at compile time. | Types cannot prove that business rules are correct. |
| Automatic memory management | The JVM reclaims unreachable objects. | Objects kept by unnecessary references can still consume memory. |
| Security features | No pointer arithmetic, bytecode checks, and access modifiers reduce common risks. | Authentication, validation, updates, and secure configuration are still required. |
| Multithreading | A program can perform independent work concurrently. | Shared data can create race conditions and deadlocks. |
| JIT optimisation | Long-running code can be optimised while the JVM is running. | Startup time and memory use can be higher than a small native program. |
Platform independent
Java does not normally compile source code directly into Windows, Linux, or macOS machine code. The compiler creates bytecode in a .class file. A compatible Java Virtual Machine (JVM) on the target computer executes that bytecode.
This is the technical basis for Write Once, Run Anywhere. It does not mean that a Java project can ignore its environment. For example, a file path such as C:\reports\daily.txt is Windows-specific and may need adjustment on another operating system. Use Java APIs such as Path.of(...) when you need code that works cleanly across operating systems.
Object-oriented and class-based
Java organises programs with classes. A class defines data (fields) and actions (methods); an object is a usable instance of that class. This lets related information stay together instead of spreading through unrelated variables and functions.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The name and score belong to one student object, and addBonus() changes that object's state. This is a practical example of encapsulation: data and the operations that use it are kept together.
Statically typed and robust
Every variable, method parameter, and return value has a declared type. Before running a program, javac checks whether those types are used correctly. This catches many mistakes early.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Java is called robust because it combines type checks, exception handling, array-bound checks, and garbage collection. Robust does not mean a program cannot fail: invalid user input, network failures, and incorrect logic still need to be handled deliberately.
Automatic memory management
In Java, you create objects with new, but you do not manually free their memory. The garbage collector can reclaim an object when no reachable part of the program refers to it anymore.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Garbage collection is not a command that immediately cleans memory after clear(). It only makes the old object eligible for collection. If a long-lived collection, cache, listener, or static field keeps a reference, the object remains reachable and memory usage can keep growing.
Secure by design
Java removes or controls several low-level operations that can cause serious bugs in unmanaged languages:
- Java has no pointer arithmetic in normal application code.
- The JVM verifies bytecode before it executes it.
- Access modifiers such as
private,protected, andpubliccontrol which code can use a class member. - Array access is checked; an invalid index throws an exception instead of silently writing into unrelated memory.
- The standard library provides established cryptography, networking, and permission-related APIs.
These are safety features, not a guarantee that an application is secure. A Java application can still be vulnerable if it trusts unsafe input, stores passwords badly, uses outdated dependencies, or gives users incorrect permissions.
Multithreaded
A thread is an independent path of execution inside one program. Java includes language and library support for threads, executors, locks, concurrent collections, and asynchronous tasks.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The two lines may appear in a different order on different runs because the scheduler decides when each thread receives CPU time. Later, learn synchronization and executor services before sharing mutable data between threads.
High performance through the JVM and JIT
Java source is first compiled by javac into bytecode. While a program runs, the JVM watches frequently executed code, often called hot code, and can compile it into efficient machine instructions using the Just-In-Time (JIT) compiler.
So Java is not accurately described as only interpreted or only compiled:
| Stage | Responsibility |
|---|---|
javac | Converts .java source files into bytecode. |
| JVM runtime | Loads, verifies, and executes bytecode. |
| JIT compiler | Optimises hot code while the application is running. |
This design can give strong performance for long-running server applications. In exchange, a Java application may have warm-up time and a larger memory footprint than a small native executable.
Rich standard library and ecosystem
Java includes APIs for collections, file handling, networking, concurrency, dates, database access, XML, JSON-related tooling through libraries, testing, and more. Its ecosystem also contains mature tools such as Maven, Gradle, JUnit, Spring, Hibernate, and many logging libraries.
The advantage is productivity and stability. The responsibility is dependency management: choose maintained libraries, update them, and do not add a large framework when a small standard-library solution is enough.
What to remember
- Java's portability comes from bytecode plus a compatible JVM.
- Classes and objects help organise related state and behaviour.
- Type checks and garbage collection reduce common errors but do not replace careful design.
- Java supports concurrent work, but shared data must be coordinated correctly.
- The JVM can optimise long-running code with JIT compilation.
- Every feature has a trade-off; know the mechanism instead of memorising only feature names.
Continue the lesson path