Java Compiler, Java Interpreter

javac એ compiler છે (source થી bytecode) અને JVM એ interpreter છે (bytecode થી execution, speed માટે JIT સાથે): બે tools, બે jobs, એક running program.

9 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


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 સુધી

  1. BookBridge.java લખો public class name એ filename સાથે exactly match થવો જોઈએ: public class BookBridge BookBridge.java માં lives કરે છે.
  2. Compile: javac BookBridge.java javac code ને check કરે છે અને BookBridge.class (bytecode) produce કરે છે. success કંઈ print નથી કરતું; errors અહીં everything ને stop કરે છે.
  3. Run: java BookBridge CLASS name આપો, filename નહીં. JVM BookBridge.class ને load કરે છે, main ને શોધે છે, અને execute કરે છે.

Quiz

javac command exactly શું produce કરે છે?

  1. native machine code ની .exe file, directly run કરવા ready
  2. bytecode ની .class file, જેને execute કરવા JVM ની જરૂર છે
  3. કંઈ નહીં; javac source file માંથી directly program ને run કરે છે
  4. આ 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 કરે છે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Java

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Java Compiler, Java Interpreter · Java Programming Language · Gri-Learn