aspose file tools*
The moose likes Beginning Java and the fly likes basic query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic query" Watch "basic query" New topic
Author

basic query

rohit delhi
Greenhorn

Joined: Jan 31, 2011
Posts: 5
If we can directly code a java compiler for different machines then why to introduce byte code concept at all?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So you don't need to recompile your code on every machine you want to run it on.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39430
    
  28
Welcome to the Ranch

Please be more informative in your thread titles.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

And that is why Java is platform independent.

Rohit , Welcome to JavaRanch


Mohamed Sanaulla | My Blog
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Don't think yourself noob for asking that question.

Sometimes even the experienced developer never question why Java is the way it is...

The most stupid question is the one that is not asked....


Java Experience
rohit delhi
Greenhorn

Joined: Jan 31, 2011
Posts: 5
Thanks for your suggestions.
Still I want to know that bytecode is also not machine independent. We need a java interpreter which is unique for every machine. So why not use a machine compatible java compiler instead of using interpreter?

Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

rohit delhi wrote:Thanks for your suggestions.
Still I want to know that bytecode is also not machine independent. We need a java interpreter which is unique for every machine. So why not use a machine compatible java compiler instead of using interpreter?



Java compiler is javac which you use to convert the source code to byte code.
You won't want to send the source code to the customer to first compile it. Right? You want to deliver the code which is machine understandable...

Bytecode is partially machine level code which is run on a virtual machine is platform(not railways platform ) independent.

Interpreter is for running the java programs. This is because Java specifications mandates a number of run time checks all of which are not possible to be run by using a compiler.

I assume you know the difference between compiler and interpreter.

Hope that helps....
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

rohit delhi wrote:Thanks for your suggestions.
Still I want to know that bytecode is also not machine independent. We need a java interpreter which is unique for every machine. So why not use a machine compatible java compiler instead of using interpreter?



Adding to what Sunny said,

These days almost all the OS have JRE which you can use to execute your Bytecode/Java applications. But the bytecode depends on only one platform i.e Java Virtual Machine
rohit delhi
Greenhorn

Joined: Jan 31, 2011
Posts: 5
Sunny wrote:
Bytecode is partially machine level code which is run on a virtual machine is platform(not railways platform ) independent


Thanks a lot sunny for your assitance. I am highly grateful.

Can you please explain me the above lines in detail.

What I know about diff. between interpreter and compiler is the execution pattern(compiler do it in one go and interpreter accomplish line by line).If there are any other differences...please update me about them.

Another thing that why is it easier to code an interpreter than a compiler?
rohit delhi
Greenhorn

Joined: Jan 31, 2011
Posts: 5
Mohamed Sanaulla wrote:
These days almost all the OS have JRE which you can use to execute your Bytecode/Java applications


Thanks a lot Mohamed Sanaulla. Can you please further explain me that how can we use the same JRE for different machines.

What I really want to add is (This is what I am concluding) : Java source code and byte both are not machine languages.Byte code can be considered a machine language for its own JVM. But similarly the code of any language can be considered machine language for its own compiler/interpreter.

Then what is different in Java if every time i need a different interpreter for different machines which is the scenario with every other language.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

The JRE- Java Runtime Environment consists of the Java Virtual Machine and the Java API Class files. The compiler converts the code into an equivalent Bytecode (Class files). Once the Bytecode is generated- It can be distributed to execute on any platform without the need to recompile them for different platforms.

And the JVM is NOT an interpreter, but generally we use the term JVM interprets the Bytecode, but every implementation of the JVM will have different approach to execute your bytecode- Some of them are- Just in Time compilation(JIT), Adaptive optimizer and so on.

Now coming to other languages(C/C++)- You would have to recompile the code each time for the target platform (also note that the primitives like int would have different size on different bit OS/platforms), but that's not the case with Java- You just compile it once. But to execute your bytecode you would need some software- which is the JVM and that has to be installed in which ever platform you are going to execute your code.

Agree that you would need to install platform specific JRE (Java Runtime environment) but that shouldn't be so much of a concern as posed by languages like C/C++. These days most of the OS come pre installed with JRE. You should think of JVM as a mini OS sitting on top of your actual OS.

rohit delhi
Greenhorn

Joined: Jan 31, 2011
Posts: 5
Thanks Mohamed Sanaulla for your valuable guidance. It really helped me out in solving my doubt. To consider JVM as an OS on top of a real OS makes things clear.Thanks a lot.
 
 
subject: basic query