views:

64

answers:

6

How a java program gets executed in comparision to a c program? What is the concept of bytecode? mean to say why the byte code is an advantage to java?

+2  A: 

An interpreter translates some form of source code into a target representation that it can immediately execute and evaluate. The structure of the interpreter is similar to that of a compiler, but the amount of time it takes to produce the executable representation will vary as will the amount of optimization. The following diagram shows one representation of the differences.

alt text

Also read this for more detail : Compilers And Interpreters - C. Jones

Pranay Rana
what is byte code and why it is an advantage to java?
Radheshyam Nayak
You're not going to learn the entire field of computer science on one single Stack Overflow thread... are you?
Ben James
@Radheshyam Nayak - see the ans of @paxdiablo for that
Pranay Rana
+1  A: 

A compiler generally takes source code and turns it into instructions that run on the hardware directly.

An interpreter generally processes the source code directly or first tokenises it into a byte-code type form to interpret that.

There are grey areas since you can think of the Java virtual machine as being the "hardware" on which Java byte code runs. Additionally, some CPUs are actually interpreters at the machine instruction level, running a program to interpret instructions (see microcode). And virtual machines like VMWare can also muddy the waters as well :-)

A Java compiler takes Java source files and compiles them to byte-code class files, which are then executed by the Java virtual machine. In most scenarios, this virtual machine is just a program running on "real" hardware.

paxdiablo
A: 

Java program is being compiled into bytecode, which read by a Virtual Machine. The Virtual Machine is OS-specific and therefore allows the program to be compatible with various OS's that have a JAVA VM available.

A C program, in the contrary, is being directly compiled into an executable code for a specific OS/Machine.

For example in .NET, you can produce the same bytecode using different languages such as C# and VB.

Serkan
A: 

A Java program, once its done, is converted to Java's bytecode, which is the middle code for all java applications or the code they build, but is not native code.

Once a Java program is executed, it only knows how to request the OS for the Java interpeter, much like C# and the MSIL(Microsft intermediate Language). Good thing about this is that Bytecode runs in any OS that has the propper interpeter to understand it, that is the Java Virtual Machine.

The compiler generates native code that is able to run only in the target OS, that's why you see C++ compilers for Windows, for Linux and many others. Good thing about native code is that doesn't have the middle interpeter to understand its code, since it communicates with the OS natively, and this means a good performance factor, which is why most game developers would preffer C++.

David Conde