📘 UNIT 1: Introduction to Java
🟢 1.1 Properties of Java
Java is:
● Simple: Syntax similar to C/C++
● Object-Oriented: Everything is an object
● Platform Independent: Write Once, Run Anywhere (WORA)
● Secure: No pointer access
● Robust: Automatic memory management
● Multithreaded: Supports multithreading
● Distributed: Built-in networking features
● Portable: Bytecode can run on any machine
● High Performance: JIT compiler improves speed
📝
Example: Android apps are written in Java – worksacross all Android phones.
Java Property What it Means Real-Life Use Case
Simple yntax is clean and easy to read,
S C/C++ student can quickly start Android app
A
especially for those who know C/C++ development using Java
Object-Oriented Everything is treated as an object Bank Accountclass can represent each
A
customer’s account in software
latform
P ou write code once, and it runs on any
Y ava-based billing software runs on
J
Independent OS with JVM Windows,Linux, andMacwithout
modification
Secure o direct pointer access (which prevents Java is used inBanking and ATM software
N
memory hacking) for secure transactions
Robust utomatic memory management + error
A mobile app doesn’t crash due to memory
A
handling leaks thanks toGarbage Collection
Multithreaded Java can perform many tasks at once game loads graphics, plays music, and
A
reads inputsimultaneously
Distributed ava can connect across computers
J chat applicationusing sockets to connect
A
over networks users globally
Portable ytecode (.class file) runs on any
B .classfile to your friend’s
ou copy your
Y
machine with JVM computer and it works instantly
igh Performance
H ompiles code during execution for
C Java web serveruses JIT to respond to
A
(JIT) faster performance users quickly in real-time
Prepared By: Nisha D. Desai UNIT : 1 INTRODUCTIONTO JAVA 1
🟢 1.2 Java vs C++
Feature Java C++
Memory Management Automatic (Garbage Collection) Manual (New/Delete)
Pointers No explicit pointer use Uses pointers
Platform Platform Independent Platform Dependent
Multiple Inheritance Through Interfaces Supported directly
Compilation Bytecode for JVM Native machine code
🟢 1.3 Java Compiler and Interpreter
javacconverts
● Compiler: .java→
.class(Bytecode)
● Interpreter (JVM): Executes bytecode line by line
● Java is both compiled and interpreted
🧠
Analogy: Like writing a story (compiler) and thenreading it (interpreter)
🟢 1.4 Use of JDK, JVM, JRE, JIT
● JDK (Java Development Kit): Contains tools (javac,java) + JRE. Used by developers.
● JRE (Java Runtime Environment): Contains JVM + Libraries.Needed torunJava.
● JVM (Java Virtual Machine): Core component. ConvertsBytecode to machine code.
● J
IT (Just-In-Time) Compiler: Improves performanceby compiling bytecode to native
code at runtime.
Prepared By: Nisha D. Desai UNIT : 1 INTRODUCTIONTO JAVA 2
📊
Diagram:
Term What it is Real-Life Analogy Function
DKJava
J fullsoftware
A itchenwith all
K ets youwriteand
L
Development Kit packageused by utensils + stove + compileJava code
developers recipes
REJava
J software
A ining table + food
D eeded only torunJava
N
Runtime packagethat warmer programs
Environment includes JVM +
Libraries
VMJava Virtual
J software engine
A hefwho reads your C
C onverts bytecode →
Machine that runs Java recipe and cooks machine code
bytecode accordingly
ITJust-In-Time
J compiler inside
A hef who
C peeds up the program by
S
Compiler JVM memorizes steps converting frequently used
for repeated orders code into machine code
Prepared By: Nisha D. Desai UNIT : 1 INTRODUCTIONTO JAVA 3
🧠 Real-World Analogy:
Suppose you're aJava Developer:
● JDK: Your entirekitchen– You cook, try recipes (write+ compile code)
● JRE: Just thetools to eat– Food is ready, you don’tcook (only running)
● JVM: Thechefinside the JRE – Interprets the bytecode(recipe) and cooks (runs code)
● J
IT: Chef’smemory– Learns which dishes are commonand cooks them faster next
time (compiles frequently used bytecode)
🟢 1.5 Garbage Collection
🔹 What is Garbage Collection?
hen your program creates objects, they take memory. If objects are no longer needed, Java
W
automatically frees that memoryusingGarbage Collector.
🔹 Why is it Needed?
free()or
In C/C++, the programmer must manually free memory (using delete
). If they
forget, memory leaks happen.
In Java:
● No need to worry about deleting objects
● JVM takes care of cleaning up unused memory
🔹 Example:
class Test {
public static void main(String args[]) {
String a = new String("Java"); // memory allocated
a = null; // object is now unreachable
[Link](); // request garbage collection
}
}
"Java"becomes unreachable. JVM willeventually remove it.
Here, the object
Prepared By: Nisha D. Desai UNIT : 1 INTRODUCTIONTO JAVA 4
🔹 How It Works Internally
1. JVM has aHeap memoryto store objects.
2. It runs theMark and Sweepalgorithm:
○ Mark: Find out which objects are still used
○ Sweep: Delete all unused ones
3. This happens in the background, without disturbing your program.
🔹 Real-Life Analogy:
Imagine acleaning robot (Garbage Collector):
● You’re using a whiteboard (memory) to write ideas (objects).
● After you're done, old notes are no longer needed.
● The robotautomaticallyerasesunused sectionsto give you space.
Prepared By: Nisha D. Desai UNIT : 1 INTRODUCTIONTO JAVA 5