JVM...doubt?

Hi all, I'm new to Java and reading some books i found a confusing concept about the Java Virtual Machine:

It is known that Java is machine independent, I mean, you can make your program and it runs on every platform, my question is:

HOW DOES THE JAVA VIRTUAL MACHINE WORKS?

Does the virtual machine interpret the bytecodes and run it on its own platform independently from the CPU or does it translate the bytecodes to the CPU particular code and then the program runs on the platform of the CPU?

thanks ; )

Ivo

[550 byte] By [ivo_hrva] at [2007-11-27 9:36:14]
# 1

as far as i know the JVM is also a java program that calls the main method for any program and so whenever a java program has a main() method and a JVM installed in the machine, there is no problem.. until unless the machine supports the JVM and the java sdk is installed, any java code that has the main() method runs without any other involvement.

Now the JVM is said to be machine independent unlike the C language which is machine dependent. So whenever a java code ir compile dnad run, the JVM automatically calls the static main() method that is public and instead of loading all the classes into the main code, the JVM executes all the required code in the imported class and returns theh result of it to the main() method of the class being run. So in this way java codes are also portable and quick and easy and fast.....this also defines a property of JVM that is platform independent unlike C...

These are the noticable differences one can find in Java and C but the answer to your question , my friend, shall be related to one of the concepts i just presented sorry for not being precise !!

hope it helped !

aron_phila at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 2
Well, it's a bit clearer nowthanks ;)
ivo_hrva at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 3

Java Virtual Machines have the option to choose how they execute code. Translating the bytecodes to the CPU's native instruction set (the CPU particular code) is known as Just-In-Time compilation (or "JIT compilation" for short). Many embedded devices have limited resources and so they use interpreters rather than JIT compilers. However, most standard edition JREs use a combination of interpretation and JIT compilation using a JIT compiler Sun calls HotSpot. The strategy they take is to comile the most frequently used bytecodes, and interpret the rest. This hybrid strategy is used because JIT compilation requires a (relatively) significant upfront investment, but the resulting native code runs much faster than it would if it were interpreted. Because of the upfront investment required by JIT compilation, this strategy only makes sense for code that is called relatively frequently. However, because native code runs much faster than interpreted code, JIT compilation achieves significant performance improvements on frequently called code.

@modia at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 4

The JVM is a abstract computer. It's instruction set and behavior are specified similarly to how you might do it for a physical CPU.

For each platform where Java runs--Windows, Solaris, Mac OS, HPUX, etc.--there is an executable program that's compiled for that particular platform and that implements the JVM spec.

There's a Windows-specific JVM implementation for Windows, a Mac-specific one for Mac, etc.

The compiled bytecode runs in that VM as if it were running on a physical machine. Each platform-specific VM implementation converts the Java bytecodes from instructions for the JVM to instructions for the particular hardware.

Your Java source code and the compiled bytecode are platform-independent. The JVM implementations that execute the bytecode are platform-dependent.

jverda at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 5
> Java Virtual Machines have the option to choose how> they execute code. Translating the bytecodes to the> CPU's native instruction set (the CPU particular> code) is known as Just-In-Time compilation This doesn't answer the OP's question.
jverda at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 6
> as far as i know the JVM is also a java programWrong from here onwards.
ejpa at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 7

> HOW DOES THE JAVA VIRTUAL MACHINE WORKS?

>

Big black box. You hardly ever have to know or care.

> Does the virtual machine interpret the bytecodes and

> run it on its own platform independently from the CPU

> or does it translate the bytecodes to the CPU

> particular code and then the program runs on the

> platform of the CPU?

>

yes.

Either one or the other depending on the JVM (though the first would require a JVM implemented in hardware, which though possible isn't the norm).

Of course "interpret" is to be used here in the loosest possible interpretation of the word.

In actuality most modern JVMs actually compile most bytecode into native executable format before executing it.

jwentinga at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 8
> In actuality most modern JVMs actually compile most> bytecode into native executable format before> executing it.I thought they compiled it only after executing it 10,000 times or something like that.
jverda at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 9

> > In actuality most modern JVMs actually compile

> most

> > bytecode into native executable format before

> > executing it.

>

>

> I thought they compiled it only after executing it

> 10,000 times or something like that.

could be. Would of course be implementation specific :)

jwentinga at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 10
wow...i see there was a big discussion going on here!!good :)Thank you all for your time !!!!!!It's clearer to me now what the JVM does.; )Ivo
ivo_hrva at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 11

@ ejp

> as far as i know the JVM is also a java program

Wrong from here onwards.

:

:

Why is it wrong ? Whats the correction needed ? Can u please provide the correct explanation fo what exactly JVM does or any link that explains it ?

Message was edited by:

aron_phil

aron_phila at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 12

> Why is it wrong ?

OK, you asked:

>> the JVM is also a java program

The JVM is not a Java program ...

>> until unless the machine supports the JVM and the java sdk is installed, any java code that has the main() method runs without any other involvement.

To the limited extent that this makes sense, it is back to front, and the JDK is not a requirement.

>> Now the JVM is said to be machine independent

No it isn't. Java and the bytecodes are machine-independent. The JVM is machine-dependent.

>> So whenever a java code ir compile dnad run

Eh?

>> and instead of loading all the classes into the main code, the JVM executes all the required code in the imported class and returns theh result of it to the main() method of the class being run

And this is just sheer nonsense.

>> These are the noticable differences one can find in Java and C

That wasn't the question, so this is irrelevant, as well as being wildly short of the truth.

>> sorry for not being precise !!

Indeed.

> Can u please provide the correct explanation fo what exactly JVM does or any link that explains it ?

Why? It's been explained correctly several times already in this thread. And if you aren't already aware of the correct explanation and haven't already read the appropriate 'links' why are you posting an answer at all?

ejpa at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...
# 13

> @ ejp

>

> > as far as i know the JVM is also a java program

>

> Wrong from here onwards.

> :

> :

>

> Why is it wrong ? Whats the correction needed ?

Most of it is just nonsense. I don't know if I could even call it right or wrong--it just doesn't make any sense.

Saying the JVM is a Java program is rather illogical. Certainly parts of it could be (probably are) written in Java. But think about it, if it was a pure Java program, where would it run? In a JVM like a Java program that you or I write? But then if that JVM is a Java program, where would it run? If the JVM doesn't need another JVM to run in, then why would your Java program or mine need a JVM to run in?

> Can u

> please provide the correct explanation fo what

> exactly JVM does or any link that explains it ?

See my earlier explanation. Or google for one. I'd be willing to bet Wikipedia has a decent explanation.

jverda at 2007-7-12 23:04:49 > top of Java-index,Java Essentials,New To Java...