UNIT–1 (Programming in Java)
1) Overview / Characteristics of Java
Java kya hai?
Java ek object-oriented programming language hai jo James Gosling ne develop ki thi.
Java kyu popular hai?
Kyuki Java ka main motto hai:
“Write Once, Run Anywhere (WORA)”
Matlab agar tumne program Windows pe banaya, toh same code Linux / Mac / Android pe bhi chal
sakta hai.
Real life example:
Jaise tumne ek PDF file banayi.
• Tum Windows me banao ya Mac me,
• PDF har device me open hoti hai.
Java bhi aise hi portable hai.
2) Java Features
Compiled + Interpreted, Platform Independent, Object-Oriented, Robust & Secure, Distributed,
Small & Simple, Multithreaded, Dynamic & Extensible, High Performance
Chalo ek ek ko asaan banaate hain:
2.1 Compiled + Interpreted (Dono ka combo)
Java me process 2 steps me hota hai:
Step 1: Compilation
.java file → javac compiler → .class (bytecode)
Step 2: Interpretation / Execution
.class file → JVM → runs on machine
Real life example:
Socho:
• Tumne Hindi me speech likhi
• Pehle usko English me convert kiya (Compilation)
• Fir har city me translator us speech ko local language me bol deta (JVM)
2.2 Platform Independent / Portable
Java program directly “Windows code” nahi banata.
Java bytecode banata hai jo JVM ke through chalta hai.
Real life example:
Socho tumhare paas ek universal charger hai.
• Phone Vivo ho, Samsung ho, iPhone ho
charger same — bas adapter change.
Java program = universal charger
JVM = adapter
2.3 Object-Oriented (OOP)
Java me sab kuch objects ke around hota hai.
Object kya hota hai?
Real world entity jiske paas:
• properties (data)
• behaviour (methods)
Real life example:
Car object
• properties: color, speed, model
• behaviour: start(), stop(), accelerate()
Java me tum classes se objects banate ho.
2.4 Robust (Strong / Reliable)
Robust means Java crash kam karta hai.
Reasons:
• Strong memory management
• Exception handling
• Garbage Collection
Real life example:
ATM machine ko dekho:
• agar customer galat PIN daale
• toh system crash nahi hota
• error show karta hai and continues working
Java bhi errors ko handle karta hai.
2.5 Secure
Java security kaafi strong hai.
Java me pointers (C/C++) jaise direct memory access nahi hota.
Isliye viruses ke liye tough ho jata hai.
Real life example:
Bank locker system:
• Customer ko direct vault access nahi dete
• Bas controlled access dete
Java = controlled access
2.6 Distributed
Java distributed applications support karta hai (network pe multiple machines).
Real life example:
Zomato/Swiggy
• app (frontend) tumhare phone pe
• server kahin aur
• database kahin aur
• delivery tracking system alag
Sab network se connected → distributed system
Java isme help karta.
2.7 Small & Simple
Java ka syntax easy hai and C++ ke complex features avoid karta.
Removes:
• Pointers
• Multiple inheritance
• Operator overloading (mostly)
Real life example:
TV remote
• Simple buttons: volume, channel
• complicated unnecessary controls nahi hote
Java is made to be simple.
2.8 Multithreaded
Java ek time pe multiple tasks kar sakta hai.
Real life example:
Tum phone pe:
• music sun rahe ho
• whatsapp message aa raha
• downloads chal rahe
• notifications aa rahi
Ye sab threads ki wajah se possible.
2.9 High Performance
Java speed C++ se thodi kam ho sakti,
lekin modern JVM (JIT compiler) speed improve karta hai.
Real life example:
Scooty vs Car:
• car faster
• but scooty also fast enough + efficient + easy
Java = efficient in real world apps.
2.10 Dynamic & Extensible
Java me libraries / modules ko runtime pe add kar sakte.
Supports:
• dynamic class loading
• extensibility
Real life example:
Mobile me new apps install krke phone ki capabilities extend karna.
3) Types of Java Programs
(A) Standalone Application
• normal program
• runs on computer
• command line / desktop app
Example:
Calculator software, VLC Player type apps
(B) Applets (old concept)
Browser me run hone wale Java programs.
Example:
Old websites pe animations/games.
Ab applets mostly obsolete hain.
4) Java Program Structure (Simple Java Program)
class SampleOne {
public static void main(String args[]) {
[Link]("Java is better");
Isme key points
1) class SampleOne
Class ka naam mostly file name ke same hota hai.
2) main() method
Ye program ka entry point hota hai.
3) public
main ko JVM kahin se bhi access kar sake.
4) static
JVM bina object banaye direct main ko call karta hai.
5) void
main kuch return nahi karta.
5) Java Program Compilation & Execution
Steps:
1. Create file: [Link]
2. Compile:
javac [Link]
→ creates [Link]
3. Run:
java Test
Real life example:
Cooking:
• vegetables cut → compile
• cook + serve → execute
6) JVM (Java Virtual Machine)
JVM ka role:
• bytecode ko machine code me convert karta
• program ko runtime environment provide karta
Real life example:
JVM = Interpreter
Jaise UN meeting me translator hota
• speaker kuch bolta
• translator sabko samjha deta in their language
7) Internal Architecture of JVM (Very Important)
JVM ke main parts:
7.1 Class Loader
.class file ko load karta.
Example:
Netflix pe movie load hone jaisi.
7.2 Runtime Data Areas
(a) Method Area / Class Area
• class info
• methods code
• static variables
Example:
Library me books ka record section.
(b) Heap
• objects yahan bante hain
Example:
Mall ka open space jahan new stalls lagte.
(c) Stack
• method calls
• local variables
• function execution
Example:
Plate stacking:
Last plate first remove (LIFO)
(d) PC Register
• current instruction ka address
Example:
GPS me “current location”.
(e) Native Method Stack
• native (C/C++) methods
Example:
Phone ka some features OS (native) handle karta.
7.3 Execution Engine
• bytecode execute karta
• interpreter + JIT
Example:
Teacher jo actually lecture deliver karta.
8) JDK vs JRE vs JVM (super exam question)
JVM
Only runs the bytecode.
JRE
= JVM + libraries
Used to run Java applications
JDK
= JRE + developer tools (compiler javac etc.)
Used to develop Java applications
Real life example:
• JVM = engine
• JRE = engine + car body
• JDK = car + toolkit (mechanic tools)
9) Garbage Collection (GC)
JVM automatically unused objects ko delete karta hai.
Real life example:
Tumhara room:
• jo cheeze use nahi ho rahi
• mom automatically dustbin me daal deti
Same role GC ka.
10) Security Features + Sandbox Model
Sandbox kya hai?
Java remote code (like applets) ko limited access deta hai.
Example:
Guest ko tumhare ghar me:
• kitchen tak allow
• bedroom vault tak nahi
Sandbox = restrictions for safety.