0

Interview question : What JVM does when we run “java A” , A is java class.

I explained like : JVM creates memory, try to find out the A java class using Java reflection API.

JVM loads the java.lang packages.

Any one please explain what are end to end functionalities of JVM!

Lundin
  • 195,001
  • 40
  • 254
  • 396
Ravichandra
  • 1,260
  • 7
  • 23
  • 37
  • 2
    No, the JVM is far to complex to describe in a series of books. You need to be more specific. – Peter Lawrey Jun 24 '14 at 05:49
  • It is a container which provides whatever resources the program needs (provided it has the authority to provide those resources). It provides a complete execution environment for a java program to run. – TheLostMind Jun 24 '14 at 05:49
  • possible duplicate of [What is a java ClassLoader?](http://stackoverflow.com/questions/2424604/what-is-a-java-classloader) – Alexandre Santos Jun 24 '14 at 05:50
  • 1
    I believe they just wanted to hear: "JVM tries to invoke public static method called 'main' in class A". – wladimiiir Jun 24 '14 at 05:51
  • 1
    JVM is a container, it provides the environment to run a java program. it is free of your operating system and provides its own environment. only files written in java and then converted to its byte code with .class extension are executed b JVM. – Aradhna Jun 24 '14 at 05:51
  • here you go: http://docs.oracle.com/javase/specs/jvms/se8/html/ but I think there's too much information to provide it on the interview. –  Jun 24 '14 at 05:52
  • The JVM is a virtual-machine and it implements an idealized form of computer architecture including op-codes (and there is an [assembler](http://jasmin.sourceforge.net/)). The JVM is responsible for taking the instructions of your program and executing them on your real hardware. This includes managing IO, UI, Threads, Memory, etc transparently (and portably). – Elliott Frisch Jun 24 '14 at 05:54
  • @ElliottFrisch - "The JVM is responsible for taking the instructions of your program and executing them on your real hardware." ?. Doesn't it forward the IO, UI, thread calls to the underlying OS? – TheLostMind Jun 24 '14 at 06:09
  • @TheLostMind Loading, evaluating (and optimizing / JITing) the instructions and translating to the native OS. – Elliott Frisch Jun 24 '14 at 06:19

3 Answers3

1

Most programming languages compile source code directly into machine code, suitable for execution on a particular microprocessor architecture. The difference with Java is that it uses bytecode .

Java bytecode executes on a special type of microprocessor.But, there wasn't a hardware implementation of this microprocessor available when Java was first released. Instead, the processor architecture is emulated by what is known as a "virtual machine". This virtual machine is an emulation of a real Java processor. The only difference is that the virtual machine isn't running on a CPU - it is being emulated on the CPU of the host machine.

The Java Virtual Machine is responsible for interpreting Java bytecode, and translating this into actions or operating system calls. The Java Virtual Machine forms part of a large system, the Java Runtime Environment (JRE). Each operating system and CPU architecture requires a different JRE. The JRE comprises a set of base classes, which are an implementation of the base Java API, as well as a JVM.

edit:for more details click oracle link

awksp
  • 11,764
  • 4
  • 37
  • 44
Techy
  • 2,626
  • 7
  • 41
  • 88
1

Start JVM process, initialize classpath for bootstrap/system/extension classloaders, call Class.forName("A")

Then jvm looks for class "A" through classloader hierarchy (bootstrap -> extension -> system). If found, static method main is invoked.

Topics not covered in this simple explanation:

  • java memory
  • other startup details - i.e. java agents
  • how exactly java classpath is formed
  • classfile loading, parsing, validation and execution (interpreted/compiled)
  • Java8 launcher also supports JavaFX applications natively
  • much more

note: Java does not load java.lang classes directly - it invokes Class.forName and loads all required dependences recursively. However Java5+ uses so-called CDS (Class Data Sharing) to quickly load precompiled set of system classes.

Sergey Alaev
  • 3,851
  • 2
  • 20
  • 35
1

JVM is too complex to describe here. There is an excellent resource written by one of it's creator in order to you can master JVM. The Java Virtual Machine

In your case, When you type "java A", a new JVM is started and the "bootstrap class loader" is responsible for loading key Java classes like java.lang.Object and other runtime code into memory first. The runtime classes are packaged inside of the JRE/lib/rt.jar file. In a JVM, each and every class is loaded by some instance of a java.lang.ClassLoader. The ClassLoader class is located in the java.lang package and we are free to subclass it to add their own functionality to class loading.

Learn more about class loaders from here: The basics of Java class loaders

indika
  • 821
  • 7
  • 13