Java Important Topics: Detailed Notes
1. Java Virtual Machine (JVM) - Short Note with Diagram
The Java Virtual Machine (JVM) is a virtual environment that enables a computer to run Java programs as
well as programs written in other languages that are compiled to Java bytecode.
Key Roles:
- Loading, Verifying, Executing code
- Managing memory (Garbage Collection)
- Providing Runtime Environment
Diagram: Java Source Code (.java) --> Java Compiler (javac) --> Bytecode (.class) --> JVM -->
Machine-specific Instructions
2. Access Controls in Java and Static Modifier in Main Method
Access Control Modifiers:
- public: Accessible everywhere.
- protected: Accessible within package and subclasses.
- default: Accessible within package.
- private: Accessible only within class.
If 'static' is removed from 'main' method:
- Compilation succeeds but Runtime Error occurs because JVM needs static 'main' method to start program
execution without creating an object.
3. Java Characteristics Explanation
- Simple: Easy syntax, no pointers.
- Architecture-Neutral: Bytecode runs on any OS.
- Portable: Platform-independent.
- Interpreted: Bytecode interpreted at runtime.
- Robust: Strong error handling and memory management.
- Secured: JVM provides a secure execution environment.
Java Important Topics: Detailed Notes
4. Java Keywords - final and this with Example
final keyword: Used to define constants, prevent overriding and inheritance.
final class Vehicle {
final int speedLimit = 100;
final void display() {
System.out.println("Speed Limit: " + speedLimit);
}
}
this keyword: Refers to current object instance.
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
void display() {
System.out.println(id + " " + name);
}
}
5. Constructor in Java - Definition, Types with Example
Constructor: Special method to initialize objects.
Types:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
Java Important Topics: Detailed Notes
class Bike {
String model;
// Default Constructor
Bike() {
model = "Unknown";
}
// Parameterized Constructor
Bike(String m) {
model = m;
}
// Copy Constructor
Bike(Bike b) {
model = b.model;
}
}
6. Garbage Collection in Java with Code Example
Garbage Collection: JVM automatically reclaims memory by destroying unused objects.
class Test {
protected void finalize() {
System.out.println("Finalize method called");
}
public static void main(String[] args) {
Test t1 = new Test();
t1 = null; // Eligible for garbage collection
System.gc(); // Request JVM to run GC
}
}
Java Important Topics: Detailed Notes
7. Command Line Arguments and Varargs
Command Line Arguments Example:
public class Demo {
public static void main(String[] args) {
for(String s : args) {
System.out.println(s);
}
}
}
// Run: java Demo Hello Java
Variable Length Arguments (Varargs) Example:
public class Example {
static void show(String... names) {
for(String name : names) {
System.out.println(name);
}
}
public static void main(String[] args) {
show("John", "David", "Emma");
}
}