aspose file tools*
The moose likes Beginning Java and the fly likes in JVM what is the need of Just in time compiler  and Java Interpreter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "in JVM what is the need of Just in time compiler  and Java Interpreter?" Watch "in JVM what is the need of Just in time compiler  and Java Interpreter?" New topic
Author

in JVM what is the need of Just in time compiler and Java Interpreter?

Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
Hi Friends,
In the JVM I saw two blocks like JAVA INTERPRETER & JUST IN-TIME COMPILER. Can, any one explain what is the need of compiler as well as an interpreter to process the bytecode generated by the java compiler.

Thanks,
Ravindra.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
You don't absolutely need a ocmpiler, or just-in time.
As far as I know, and I am a little vague about it, there are two sorts of programming language compilation:-
  • Compiled languages. You write x = y + 1 and it is converted to the equivalent in machine code.
  • Interpreted languages. You write y = x + 1 and the compiler produces and executes the machine code.
  • Java is sort of "half-and-half;" your Java code is converted into "bytecode" which is a set of bytes each of which represents an assembler instruction.
    You can see it if you get a hexadecimal editor and open a Java class; this is a HelloWorld variant which uses a JOptionPane for its output:-
    [Campbell@dhcppc0 disk]$ hexedit HelloWorld.class

    00000000 CA FE BA BE 00 00 00 2E 00 19 07 00 02 01 00 0A ................
    00000010 48 65 6C 6C 6F 57 6F 72 6C 64 07 00 04 01 00 10 HelloWorld......
    00000020 6A 61 76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74 java/lang/Object
    00000030 01 00 06 3C 69 6E 69 74 3E 01 00 03 28 29 56 01 ...<init>...()V.
    00000040 00 04 43 6F 64 65 0A 00 03 00 09 0C 00 05 00 06 ..Code..........
    00000050 01 00 0F 4C 69 6E 65 4E 75 6D 62 65 72 54 61 62 ...LineNumberTab
    00000060 6C 65 01 00 04 6D 61 69 6E 01 00 16 28 5B 4C 6A le...main...([Lj
    00000070 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B ava/lang/String;
    00000080 29 56 08 00 0E 01 00 0C 48 65 6C 6C 6F 20 57 6F )V......Hello Wo
    00000090 72 6C 64 21 08 00 10 01 00 07 53 75 63 63 65 73 rld!......Succes
    000000A0 73 0A 00 12 00 14 07 00 13 01 00 17 6A 61 76 61 s...........java
    000000B0 78 2F 73 77 69 6E 67 2F 4A 4F 70 74 69 6F 6E 50 x/swing/JOptionP
    000000C0 61 6E 65 0C 00 15 00 16 01 00 11 73 68 6F 77 4D ane........showM
    000000D0 65 73 73 61 67 65 44 69 61 6C 6F 67 01 00 3C 28 essageDialog..<(
    000000E0 4C 6A 61 76 61 2F 61 77 74 2F 43 6F 6D 70 6F 6E Ljava/awt/Compon
    000000F0 65 6E 74 3B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 4F ent;Ljava/lang/O
    00000100 62 6A 65 63 74 3B 4C 6A 61 76 61 2F 6C 61 6E 67 bject;Ljava/lang
    00000110 2F 53 74 72 69 6E 67 3B 49 29 56 01 00 0A 53 6F /String;I)V...So
    00000120 75 72 63 65 46 69 6C 65 01 00 0F 48 65 6C 6C 6F urceFile...Hello
    00000130 57 6F 72 6C 64 2E 6A 61 76 61 00 21 00 01 00 03 World.java.!....
    00000140 00 00 00 00 00 02 00 01 00 05 00 06 00 01 00 07 ................
    00000150 00 00 00 1D 00 01 00 01 00 00 00 05 2A B7 00 08 ............*...
    00000160 B1 00 00 00 01 00 0A 00 00 00 06 00 01 00 00 00 ................
    00000170 03 00 09 00 0B 00 0C 00 01 00 07 00 00 00 32 00 ..............2.
    00000180 04 00 01 00 00 00 0A 01 12 0D 12 0F 04 B8 00 11 ................
    00000190 B1 00 00 00 01 00 0A 00 00 00 16 00 05 00 00 00 ................
    000001A0 07 00 01 00 08 00 05 00 09 00 06 00 07 00 09 00 ................
    000001B0 0A 00 01 00 17 00 00 00 02 00 18 ...........

    --- HelloWorld.class --0x0/0x1BB----------------------------------------
    Note it begins with CAFEBABE (class files always do) and the translation of some of the obscure bits (eg what the Strings are) is shown. You can look up in the meanings of all the numbers in this part of the JVM specification.
    You can also use the javap application to see the meaning of the bytecode. For the same HelloWordl class you get this:-
    [Campbell@dhcppc0 disk]$ /usr/java/jdk1.6.0/bin/javap HelloWorld
    Compiled from "HelloWorld.java"
    public class HelloWorld extends java.lang.Object{
    public HelloWorld();
    public static void main(java.lang.String[]);
    }

    or with the -c flag, this:-
    [Campbell@dhcppc0 disk]$ /usr/java/jdk1.6.0/bin/javap -c HelloWorld
    Compiled from "HelloWorld.java"
    public class HelloWorld extends java.lang.Object{
    public HelloWorld();
    Code:
    0: aload_0
    1: invokespecial #8; //Method java/lang/Object."<init>" )V
    4: return

    public static void main(java.lang.String[]);
    Code:
    0: aconst_null
    1: ldc #13; //String Hello World!
    3: ldc #15; //String Success
    5: iconst_1
    6: invokestatic #17; //Method javax/swing/JOptionPane.showMessageDialog Ljava/awt/Component;Ljava/lang/Object;Ljava/lang/String;I)V
    9: return

    }

    You can see that the JVM uses something which is not machine code to run with. That is why you call it an interpreted language.

    Just-in-time compilation means that the JVM only converts the bytecode to machine code when actually required. I found several websites with more details on: here, from the Patent application, here, from the HotSpot JVM white paper, and here, an early suggestion for optimising the Java compiler.The JVM appears to translate only code it actually uses into machine language, and optimises frequently-used code as it is running.
    [ March 05, 2007: Message edited by: Campbell Ritchie ]
    Ravindranath Chowdary
    Ranch Hand

    Joined: Nov 08, 2006
    Posts: 71
    Thanks Ritchie,
    For giving me a clear idea on this.

    -Ravindra.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38818
        
      23
    You're welcome.

    And I am glad you found it clear.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Interpreted languages. You write y = x + 1 and the compiler produces and executes the machine code.


    I've seen wording like this so many times that maybe it's only me that feels uncomfortable with it. A pure interpreter that sees "a = b + c" doesn't "produce machine code" to make that happen. It gets the value of b, gets the value of c, adds them together and puts the result in a. If you read the source for the interpreter you can find all the routines that do those things.

    The JIT compiler does exactly "produce machine code".

    Many inerpreters work directly on the human-readable code that you write into plain text files. Java does surely make all this more confusing. The Java compiler puts out byte code, which is machine code for a non-existant machine. A JRE that doesn't have JIT compilation interprets byte code. A JRE that does have JIT can do that plus decide to produce machine code. Or not.

    PJ Plauger in Programming On Purpose says almost any input file can be thought of as a language, and almost any program that reads an input file and does something useful can be thought of as an interpreter. If you've written something as common as:

    you've written an interpreter for a tiny language


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Mr. C Lamont Gilbert
    Ranch Hand

    Joined: Oct 05, 2001
    Posts: 1170

    I like Stan's answer because it highlights the difference between 'interpretation' and 'compilation.' javascript and perl and interpreted languages. Java is not an interpreted language.

    Java source code is compiled into java byte code (.class). Java byte code is executed by the Java virtual machine. at runtime these bytes are converted into lower level machine code. This conversion is not static, but can be further altered and optimized as the JVM runs and decides some piece of code has room for improvement.

    IIRC.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38818
        
      23
    Yes, Stan James, what you wrote is more accurate than what I said.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    That runtime compilation to machine code in Java is optional, isn't it? Not present in the first JVMs? I'm not sure it's right to say "Java does this" but "all modern JVMs do it". How about Java phones? Do they do JIT?

    My old favorite interpreted language, REXX, is sometimes "tokenized" into something more palatable to the interpreter than plain text. It's not as cool as compiling to byte code, but much the same idea. That's not specfied as part of the language though; it's totally up to the implementer. Some implementers let you save the tokenized form to save some startup time, but it's a risky business and surely not cross platform like byte code.

    Sorry for being so picky.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 41839
        
      63
    It's still possible to execute Java code with the JIT turned off via "java -Xint ..."


    Ping & DNS - my free Android networking tools app
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: in JVM what is the need of Just in time compiler and Java Interpreter?