Theory
दो Commands, दो Mysteries
Lab में आपका पहला BookBridge session ऐसा दिखता है:
javac BookBridge.java
java BookBridge
दो अलग commands। पहला succeed होने पर बिल्कुल कुछ print नहीं करता। दूसरा असल में आपका program run करता है। ज़्यादातर students यह 3 साल तक type करते हैं बिना यह पूछे हर एक असल में क्या करता है, और फिर exam exactly यही पूछता है: Java compiler और interpreter explain कीजिए।
Theory
Scriptwriter और Narrator
एक story सोचिए जो Gujarati में लिखी गई है और हर state के listeners तक पहुँचनी चाहिए।
javac scriptwriter है: यह आपकी story को एक बार एक standard script (bytecode) में translate करता है जिसे कोई भी listener natively नहीं बोलता।
JVM local narrator है: हर state में, एक narrator उस standard script को पढ़ता है और इसे local language में live perform करता है। एक script, कई narrators। Writing एक बार होती है; narration हर machine पर, हर run पर होती है।
Theory
दो Tools, Formally
Java compiler (javac): human-readable source code (BookBridge.java) को bytecode (BookBridge.class) में translate करता है, Java Virtual Machine का standard instruction set। यह run करने से पहले एक बार होता है।
Java interpreter (JVM): .class file load करता है और actual machine पर bytecode instructions को एक-एक करके execute करता है। बार-बार run होने वाले code के लिए, इसका JIT (just-in-time) compiler hot bytecode को on the fly native machine code में translate करता है, तो long-running programs speed up होते हैं।
तो Java compiled और interpreted दोनों है: bytecode में compiled, JVM से interpreted (plus JIT)।
Practical
BookBridge.java, पहली Listing
public class BookBridge {
public static void main(String[] args) {
System.out.println("== BookBridge Library ==");
System.out.println("Books ready to issue: 12");
}
}
Follow along
Source से Running Program तक
- BookBridge.java लिखिए Public class का नाम exactly filename से match होना चाहिए: public class BookBridge, BookBridge.java में रहता है।
- Compile कीजिए: javac BookBridge.java javac code check करता है और BookBridge.class (bytecode) produce करता है। Success होने पर कुछ print नहीं होता; errors यहीं सब कुछ रोक देते हैं।
- Run कीजिए: java BookBridge CLASS का नाम दीजिए, filename नहीं। JVM BookBridge.class load करता है, main ढूँढता है, और execute करता है।
Quiz
javac command exactly क्या produce करता है?
- Native machine code की एक .exe file, सीधे run करने के लिए ready
- Bytecode की एक .class file, जिसे execute करने के लिए एक JVM चाहिए
- कुछ नहीं; javac source file से सीधे program run करता है
- इस program के लिए customised एक नया JVM
Show the answer
Bytecode की एक .class file, जिसे execute करने के लिए एक JVM चाहिए
javac bytecode पर रुकता है: एक .class file जिसे कोई भी CPU सीधे run नहीं कर सकता, यही exactly वजह है हर machine को एक JVM चाहिए। Option A एक C compiler के output को describe करता है। Option C javac को java command से confuse करता है (और इस syllabus के model में java भी source run नहीं करता)। Option D एक serious face के साथ nonsense है: JVM हर machine पर एक बार installed होता है, कभी per-program generate नहीं होता।
Think first
अपने Friend की Mistake Spot कीजिए
आपका friend successfully compile करता है, फिर java BookBridge.class type करता है और class न मिलने का error पाता है। Tap करने से पहले: क्या ग़लत हुआ?
Show the answer
java command एक class name लेता है, filename नहीं। java BookBridge.class type करना JVM को literally "class" नाम की एक class के लिए "BookBridge" नाम के एक package के अंदर search कराता है, जो exist नहीं करता। सही command है java BookBridge: JVM खुद bytecode file ढूँढते समय .class append करता है। Filename के साथ compile कीजिए, class name के साथ run कीजिए।
Watch out
यह Answer में Marks कहाँ Leak होते हैं
"Java एक interpreted language है" अकेले लिखना design point खो देता है: यह पहले bytecode में compiled होता है, फिर interpreted होता है। हमेशा दोनों stages present कीजिए।
और एक full-marks answer में JIT मत भूलिए: JVM एक slow line-by-line interpreter नहीं रहता; यह run के दौरान frequently executed bytecode को native code में compile करता है। JIT वह वजह है modern Java performance long-running programs के लिए C++ के करीब है।
Theory
इसे Backward और Forward Connect कीजिए
यह 2-stage pipeline पिछले lesson की "write once, run anywhere" के पीछे की machinery है: javac travelling bytecode बनाता है, JVM local runner है। BCA204 के Python से contrast कीजिए, जहाँ आपने कभी एक अलग compile command run नहीं किया, और BCA104 के C से, जहाँ compilation सीधे machine code तक गया। इस semester का हर BookBridge program exactly इन 2 commands से गुज़रता है।
Summary
Key takeaways
- javac = Java compiler: .java source in, .class bytecode out, run करने से पहले एक बार।
- JVM = Java interpreter: bytecode load करता है और इसे local machine पर execute करता है।
- JVM के अंदर JIT hot bytecode को run time पर native code में compile करता है, near-native speed के लिए।
- Java compiled (bytecode में) और interpreted (JVM से) दोनों है; exam में दोनों बोलिए।
- Filename के साथ compile कीजिए (javac BookBridge.java), class name के साथ run कीजिए (java BookBridge)।
- Public class का नाम exactly filename से match होना चाहिए।
- Memory hook: scriptwriter एक बार लिखता है, narrator हर जगह perform करता है।