• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic query

 
rohit delhi
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we can directly code a java compiler for different machines then why to introduce byte code concept at all?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you don't need to recompile your code on every machine you want to run it on.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49828
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please be more informative in your thread titles.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that is why Java is platform independent.

Rohit , Welcome to JavaRanch
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
rohit delhi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic