OldSchool
OldSchool

Reputation: 2183

how the bytecode is interpreted in java?

As you have guessed from my question that i am new to java , my question is a simple one that is about the working of JVM. I have studied from wikipedia that bytecodes are fed to either interpreter or JIT compiler and which is executed on accordance of type of the underlying platform by the JVM. My questions are:-

  1. Does the term "platform" refer to hardware or both hardware and OS?
  2. Do two devices having different platform have different JVMs?

Upvotes: 1

Views: 200

Answers (5)

chronodekar
chronodekar

Reputation: 2746

I would suggest that you go back to the basics of how computers work. Visually the stack would look like this;

----------------
Application
----------------
Operating System
----------------
Hardware
----------------

At the bottom of the stack you have the hardware. This can be of any architecture - x86, ARM, PowerPC ... etc. Above this you have the Operating System which handles the interaction between the user-facing application and the underlying hardware. And on top, you have the application.

Now, assume that you have to support an application to run on multiple pieces of hardware. For every platform (OS+hardware combo) you would need to maintain different application source code. Something like this;

----------------------       ||    ----------------------
Application (x86)            ||    Application (ARM)
----------------------       ||    ----------------------
Operating System (x86)       ||    Operating System (ARM)
----------------------       ||    ----------------------
Hardware (x86)               ||    Hardware (ARM)   
----------------------       ||    ----------------------

Which, depending on the complexity of your application, becomes a maintenance nightmare. The Java Runtime Environment (JRE) promises a "code once, run anywhere" philosophy. Simply put, they add another layer to the stack like this;

---------------------------------------------------------
                       Application (Java)
---------------------------------------------------------
JRE (x86)                    ||    JRE (ARM)
----------------------       ||    ----------------------
Operating System (x86)       ||    Operating System (ARM)
----------------------       ||    ----------------------
Hardware (x86)               ||    Hardware (ARM)   
----------------------       ||    ----------------------

Doing things this way means that you won't need to worry about what kind of hardware your application will run on. Makes things a LOT easier to maintain.

And I like to think that my ASCII art answered your question?

Upvotes: 2

JohnTheBeloved
JohnTheBeloved

Reputation: 2433

JVM is a software itself and can be written for both the hardware and OS, JVM can be written for as low as a Wristwatch to as PDAs to Car Navigators, to as complex as Television Sets.

see http://www.cubrid.org/blog/dev-platform/understanding-jvm-internals/

Upvotes: 1

assylias
assylias

Reputation: 328568

Platform refers to a combination of a processor architecture and an OS although some combinations of OS/CPU may share some code.

To get an idea of how it works internally, why not look at how the hotspot JVM is implemented as an example. The source code is here and you can see branches per cpu (for cpu specific code), os (for OS specific code), cpu_os (for code that changes for each cpu/os combination) and share which is common code accross CPUs and OSes.

You will see that, in the case of hotspot, a significant portion of the code is shared across platforms.

Upvotes: 1

tilpner
tilpner

Reputation: 4351

Does the term "platform" refer to hardware or both hardware and OS?

Yes, e.g. a GNU/Linux JVM is on a different platform than a Windows JVM.

Do two devices having different platform have different JVMs?

Most likely, as e.g. an ELF executable will not run on Windows, which expects a PE executable.

You should, in an ideal world, not care about what platform your code runs on, as long as you don't use any platform specific features. Even basic file IO often requires platform-specific code.

Upvotes: 1

Amir Afghani
Amir Afghani

Reputation: 38511

Does the term "platform" refer to hardware or both hardware and OS?

Hardware/OS

Do two devices having different platform have different JVMs?

They may have the same JVM (by version number), but the implementation of those JVM's is going to be different. The JVM itself is just a compiled program - and the two binaries will be different. They may behave exactly the same, but they are indeed different program executables.

Upvotes: 2

Related Questions