Theory
બે commands, બે mysteries
તમારું first BookBridge session lab માં આમ દેખાય છે:
javac BookBridge.java
java BookBridge
બે different commands. પહેલો એ જો succeed થાય તો કંઈ print નથી કરતો. બીજો actually તમારા program ને run કરે છે. મોટાભાગના students આને 3 years સુધી type કરે છે without asking કે દરેક one really શું કરે છે, અને પછી exam exactly એ પૂછે છે: explain the Java compiler and interpreter.
Theory
scriptwriter અને narrator
Gujarati માં લખેલી story નો વિચાર કરો જે દરેક state ના listeners સુધી પહોંચવી જોઈએ.
javac એ scriptwriter છે: એ તમારી story ને once standard script (bytecode) માં translate કરે છે જે કોઈ listener native રીતે બોલતો નથી.
JVM એ local narrator છે: દરેક state માં, narrator એ standard script ને read કરે છે અને local language માં live perform કરે છે. એક script, many narrators. writing once happens છે; narration દરેક machine પર, દરેક run પર happens છે.
Theory
બે tools, ઔપચારિક રીતે
Java compiler (javac): human-readable source code (BookBridge.java) ને bytecode (BookBridge.class) માં translate કરે છે, Java Virtual Machine નો standard instruction set. આ once happens છે, running પહેલાં.
Java interpreter (the JVM): .class file ને load કરે છે અને bytecode instructions ને actual machine પર one by one execute કરે છે. જે code repeatedly run થાય છે, તેના માટે તેનો JIT (just-in-time) compiler hot bytecode ને native machine code માં on the fly translate કરે છે, એટલે long-running programs speed up થાય છે.
એટલે Java both compiled અને interpreted છે: bytecode માં compiled, JVM દ્વારા interpreted (plus JIT).
Practical
BookBridge.java, first 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 name એ filename સાથે exactly match થવો જોઈએ: public class BookBridge BookBridge.java માં lives કરે છે.
- Compile: javac BookBridge.java javac code ને check કરે છે અને BookBridge.class (bytecode) produce કરે છે. success કંઈ print નથી કરતું; errors અહીં everything ને stop કરે છે.
- Run: java BookBridge CLASS name આપો, filename નહીં. JVM BookBridge.class ને load કરે છે, main ને શોધે છે, અને execute કરે છે.
Quiz
javac command exactly શું produce કરે છે?
- native machine code ની .exe file, directly run કરવા ready
- bytecode ની .class file, જેને execute કરવા JVM ની જરૂર છે
- કંઈ નહીં; javac source file માંથી directly program ને run કરે છે
- આ program માટે customised નવું JVM
Show the answer
bytecode ની .class file, જેને execute કરવા JVM ની જરૂર છે
javac bytecode પર stop થાય છે: .class file જે કોઈ CPU directly run નથી કરી શકતો, જે exactly છે કે શા માટે દરેક machine ને JVM ની જરૂર છે. Option A C compiler ના output ને describe કરે છે. Option C javac ને java command સાથે confuse કરે છે (અને even java આ syllabus ના model માં source ને run નથી કરતું). Option D nonsense છે serious face સાથે: JVM એ machine દીઠ once install થાય છે, ક્યારેય per program generated નથી થતું.
Think first
તમારા friend ની mistake ને spot કરો
તમારો friend successfully compile કરે છે, પછી java BookBridge.class type કરે છે અને class ને not finding વિશે error મેળવે છે. tap કરતા પહેલા: શું wrong થયું?
Show the answer
java command class name લે છે, filename નહીં. java BookBridge.class type કરવાથી JVM literally "class" નામની class ને "BookBridge" નામના package ની અંદર search કરે છે, જે exist નથી કરતી. correct command એ java BookBridge છે: JVM પોતે bytecode file ને શોધતી વખતે .class ને append કરે છે. filename સાથે compile કરો, class name સાથે run કરો.
Watch out
જ્યાં marks આ answer માં leak થાય છે
માત્ર "Java is an interpreted language" લખવાથી design point lose થાય છે: એ પહેલા bytecode માં compiled છે, પછી interpreted. હંમેશા both stages ને present કરો.
અને full-marks answer માં JIT ને forget ન કરો: JVM slow line-by-line interpreter તરીકે stay નથી કરતું; એ run દરમિયાન frequently executed bytecode ને native code માં compile કરે છે. JIT એ reason છે કે modern Java performance long-running programs માટે C++ ની close છે.
Theory
એને backward અને forward connect કરો
આ 2-stage pipeline એ previous lesson ના "write once, run anywhere" ની પાછળનું machinery છે: javc travelling bytecode બનાવે છે, JVM એ local runner છે. BCA204 ના Python સાથે contrast કરો, જ્યાં તમે ક્યારેય separate compile command ને run નથી કર્યું, અને BCA104 ના C સાથે, જ્યાં compilation સીધું machine code તરફ ગયું. દરેક BookBridge program આ semester માં exactly આ 2 commands માંથી goes છે.
Summary
Key takeaways
- javac = Java compiler: .java source in, .class bytecode out, running પહેલાં once.
- JVM = Java interpreter: bytecode ને load કરે છે અને local machine પર execute કરે છે.
- JVM ની અંદર JIT hot bytecode ને run time પર native code માં compile કરે છે, near-native speed માટે.
- Java both compiled (bytecode માં) અને interpreted (JVM દ્વારા) છે; exam માં both કહો.
- filename સાથે compile કરો (javac BookBridge.java), class name સાથે run કરો (java BookBridge).
- public class name એ filename સાથે exactly match થવો જોઈએ.
- Memory hook: scriptwriter once લખે છે, narrator everywhere perform કરે છે.