Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4418

Confusion about java

I have some confusion about java. Java is compiler or interpreter language. I got some link which has some great description but the confusion is some one says java is compiled language or some of them are says java is interpreter language? Even how can I understand for any language that this language is compiled or interpreter or both. I have another question is that JVM is part of OS or when we installed java then JVM is installed.

Upvotes: 0

Views: 150

Answers (8)

arjun99
arjun99

Reputation: 358

Java is a programming language expressly designed for use in the distributed environment of the Internet. It enforces an object-oriented programming model. Java can be used to create complete applications that may run on a single computer or be distributed among servers and clients in a network

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201517

Java is generally both. It is compiled to a byte-code that is executed by a Just-In-Time compiler1. Java also has the ability to run interpretters with ScriptEngine.

There is/was also gcj (The GNU Compiler for the Java™ Programming Language) their homepage says2 (in part)

Compiled applications are linked with the GCJ runtime, libgcj, which provides the core class libraries, a garbage collector, and a bytecode interpreter. libgcj can dynamically load and interpret class files, resulting in mixed compiled/interpreted applications.

1Anything that correctly implements the Java Virtual Machine is Java (but Oracle controls the trademark, so it might not be called Java), so it could be (and has been) implemented as an interpretter (kaffe falls back to a byte-code interpretter if the JIT doesn't support your platform).

2Sadly latest news from September 22, 2009

Upvotes: 0

Sachin
Sachin

Reputation: 359

  The journey of Java code to a Machine code is .
    1. You write .java file.    
    2. give it to javac(the java compiler)
    3. javac compiles it and gives you .class file(call it bytecode)
    4. you put this .class file on JVM(just a software kind of thing) .
    5. JVM interprets that .class file(the bytecode we call it)
    6. JVM vomits machine code acceptable by underlying platform [OS+CPU].            


     JVM is a software package kind of thing which you need to run
     your bytcode it must be compatible with your underlying platform 
     to convert bytecode to machinecode .

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075467

Java is compiler or interpreter language

It's more accurate to call Java compiled than interpreted. Java source code is compiled to a binary form called "bytecode." At runtime, the JVM runs the bytecode, and may compile that bytecode to machine code on-the-fly (using something called a Just In Time [JIT] compiler). Java bytecode is effectively a Java-specific machine code (put this value in this register, jump to this location, etc.). This is what makes it possible for Java programs to be run anywhere a JVM is available.

... is that JVM is part of OS...

No, the JVM is a program that you install.

Example:

This Java source code:

public class Example {
    public static final void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Compiles to this bytecode (without debugging info) (traditional "hexdump" format):

00000000  ca fe ba be 00 00 00 34  00 1a 0a 00 06 00 0c 09  |.......4........|
00000010  00 0d 00 0e 08 00 0f 0a  00 10 00 11 07 00 12 07  |................|
00000020  00 13 01 00 06 3c 69 6e  69 74 3e 01 00 03 28 29  |........()|
00000030  56 01 00 04 43 6f 64 65  01 00 04 6d 61 69 6e 01  |V...Code...main.|
00000040  00 16 28 5b 4c 6a 61 76  61 2f 6c 61 6e 67 2f 53  |..([Ljava/lang/S|
00000050  74 72 69 6e 67 3b 29 56  0c 00 07 00 08 07 00 14  |tring;)V........|
00000060  0c 00 15 00 16 01 00 0d  48 65 6c 6c 6f 2c 20 57  |........Hello, W|
00000070  6f 72 6c 64 21 07 00 17  0c 00 18 00 19 01 00 07  |orld!...........|
00000080  45 78 61 6d 70 6c 65 01  00 10 6a 61 76 61 2f 6c  |Example...java/l|
00000090  61 6e 67 2f 4f 62 6a 65  63 74 01 00 10 6a 61 76  |ang/Object...jav|
000000a0  61 2f 6c 61 6e 67 2f 53  79 73 74 65 6d 01 00 03  |a/lang/System...|
000000b0  6f 75 74 01 00 15 4c 6a  61 76 61 2f 69 6f 2f 50  |out...Ljava/io/P|
000000c0  72 69 6e 74 53 74 72 65  61 6d 3b 01 00 13 6a 61  |rintStream;...ja|
000000d0  76 61 2f 69 6f 2f 50 72  69 6e 74 53 74 72 65 61  |va/io/PrintStrea|
000000e0  6d 01 00 07 70 72 69 6e  74 6c 6e 01 00 15 28 4c  |m...println...(L|
000000f0  6a 61 76 61 2f 6c 61 6e  67 2f 53 74 72 69 6e 67  |java/lang/String|
00000100  3b 29 56 00 21 00 05 00  06 00 00 00 00 00 02 00  |;)V.!...........|
00000110  01 00 07 00 08 00 01 00  09 00 00 00 11 00 01 00  |................|
00000120  01 00 00 00 05 2a b7 00  01 b1 00 00 00 00 00 19  |.....*..........|
00000130  00 0a 00 0b 00 01 00 09  00 00 00 15 00 02 00 01  |................|
00000140  00 00 00 09 b2 00 02 12  03 b6 00 04 b1 00 00 00  |................|

The symbolic version of what that bytecode says (which you can get from javap -c Example) is this:

public class Example {
  public Example();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public static final void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String Hello, World!
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return
}

Upvotes: 1

venkat
venkat

Reputation: 259

Java is not a compiler or interpreter. Java is a coding language. java code is compiled into bytecode and stored with .class extension. when the code is executed the bytecode is converted using JIT.The result is machine code which is then fed to the memory and is executed.

Upvotes: 0

Rahman
Rahman

Reputation: 3795

1.Java is compiler or interpreter language

Its having both. At first, Java compiler compiles .java file and creates a .class file. Later on, Java interpreter convert it to a platform dependent file. For this reason java is called Platform Independent language because through interpreter its converted to such a language which can be understood by current system/OS.

2.JVM is part of OS or when we installed java then JVM is installed.

Its a part of JAVA. Not a part of OS

Upvotes: 0

Sergey Maksimenko
Sergey Maksimenko

Reputation: 588

Starting from your second question, JVM is separate product, and you need to install it yourself. For the first question, java compliles your program into jvm bytecode, and sends itstructions to machine as interpreter

Upvotes: 0

Ankur Singhal
Ankur Singhal

Reputation: 26077

Java is a compiled programming language, but rather than compile straight to executable machine code, it compiles to an intermediate binary form called JVM byte code. The byte code is then compiled and/or interpreted to run the program

JVM is not part of OS, when you install Java, you actually create a environment called JVM.

More usefull information here

Upvotes: 0

Related Questions