Introduction to the Java Dev Env
Summary
- JVM – Java Virtual Machine
- JRE – Java Runtime Environment
- JDK – Java Development Kit
- JIT – Just-in-time compilation
- IDE – Integrated development environment
- JNI – Java Native Interface
- java : it is the launcher for all the java applications.
- javac : complier of the java programming languages.
- javadoc: it is the API documentation generator.
- jar: creates and manage all the JAR files.
Workflow
In Java, there is a process of compilation and interpretation.
- Java Code -> byte codes (by the Java Compiler).
- Byte codes -> machine code (by the JVM).
- Machine code: executed directly by the machine.
Source:
Java Architecture and its Components | JVM, JRE and JDK …. https://www.edureka.co/blog/java-architecture/
https://static.javatpoint.com/core/images/java-interpreter.png
JVM / JRE / JDK
Overview
Details
JVM
JVM
is Java Virtual Machine — Runs/ Interprets/ translates Bytecode into Native Machine Code
What it does
The JVM performs the following operation:
- Loads code
- Verifies code
- Executes code
- Provides runtime environment (Initialization)
JVM provides definitions for the following:
- Memory area
- Class file format
- Register set
- Garbage-collected heap
- Fatal error reporting etc.
WORA (Write Once Run Anywhere)
JVM ensures the interoperability of Java programs across different implementations. i.e.
- JVMs are available for many hardware and software platforms
- JVM is platform dependent
- Programmers do not need to worry about idiosyncrasies of the underlying hardware platform.
JVM largely helps in the abstraction of inner implementation from the programmers who use libraries for their JDK programs.
Source:
Must Know Java Concepts — Features, JDK & Program …. https://medium.com/geekculture/must-know-java-concepts-features-jdk-program-execution-e1590ea056f1
Differences between JDK, JRE and JVM – GeeksforGeeks. https://www.geeksforgeeks.org/differences-jdk-jre-jvm/
JVM Architecture
Source: JVM | Java Virtual Machine – Javatpoint. https://www.javatpoint.com/jvm-java-virtual-machine
Classloader
Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java program, it is loaded first by the classloader. There are three built-in classloaders in Java.
Class (Method) Area
Class (Method) Area stores per-class structures such as the constant runtime pool, field and method data, the code for methods.
Heap
It is the runtime data area in which objects are allocated.
Stack
- Stack stores frames
- Stack holds local variables and partial results and plays a part in method invocation and return.
- Each thread has a private JVM stack, created at the same time as the thread.
Frames
- A frame is a section in the java stack memory area where the information about a method is stored. After the execution is over, that frame is deleted from the stack. Frames also have the state of variables, references of other objects in a heap.
- A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
Program Counter Register
PC (program counter) register contains the address of the Java virtual machine instruction currently being executed.
Native Method Stack
It contains all the native methods used in the application.
Execution Engine
It contains:
- Virtual processor – also known as a virtual CPU (vCPU), is a physical central processing unit (CPU) that is assigned to a virtual machine (VM).
- Interpreter: Read bytecode stream, then execute the instructions. See #Interpreter session for details.
- Garbage collector
- Just-In-Time(JIT) compiler: It is used to improve performance.
- JIT compiles parts of the byte code that have similar functionality at the same time
- Hence reduces the amount of time needed for compilation.
- Here, the term "compiler" refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
- Java Native Interface
- Java Native Interface (JNI) is a framework that provides an interface to communicate with another application written in another language like C, C++, Assembly etc.
- Java uses JNI framework to send output to the Console or interact with OS libraries.
Source: Details about Java Virtual Machine you mus know. – TecH-TaLkS. https://techtalks935.wordpress.com/2017/09/29/details-about-java-virtual-machines-you-mus-know/
Java interpreter
Java Interpreter is a computer program (system software) that implements the JVM. It is responsible for reading and executing the program.
It reads the source program and translates the source code instruction by instruction.
Step 1 Java compiler compiles the source code into the
Step 2 JVM converts the byte code ( .class files) into machine code. i.e. The JVM converts that high-level program code into assembly language machine code using the Java interpreter. The JVM uses the Interpreter at runtime
Step 3: Executes the code on the host machine: Once the class is loaded, Java follows a convention and searches for the class that contains the main()
method and invoke main()
.
Source: Java Interpreter – Javatpoint. https://www.javatpoint.com/java-interpreter
Where else can we find a java interpreter?
- In the JVM
- In Java-enabled browsers (like Google Chrome) – Used to run the Applet
Interpreter vs Compiler
Interpreter | Compiler |
---|---|
It translates the code instruction by instruction. | It translates the entire program at once. |
Its execution is slower. | Its execution is faster. |
Its compile time is less. | It takes more time to compile the code. |
It does not generate the intermediate object code. | It generates the intermediate object code. |
It compiles the program until an error is found. | All the errors show once at the end of the compilation. |
Python, PHP, Ruby, and Perl use an interpreter. | Java, C++, Scala, and C uses a compiler. |
Source: Java Interpreter – Javatpoint. https://www.javatpoint.com/java-interpreter
JRE
Java Runtime Environment (JRE)
- is an installation package that provides an environment to only run (not develop) the java program (or application) onto your machine.
- is only used by those who only want to run Java programs that are end-users of your system.
The on-disk system takes your Java code, combines it with the needed libraries, and starts the JVM to execute it.
JRE Architecture
The JRE contains libraries and software needed by your Java programs to run.
Source:
Java Architecture and its Components | JVM, JRE and JDK …. https://www.edureka.co/blog/java-architecture/
https://images.idgesg.net/images/article/2020/01/jw-whatisjre-fig1-100827419-large.jpg?auto=webp&quality=85,70
JVM and JRE in the runtime environment.
What is a runtime environment?
A software program needs to execute, and to do that, it needs an environment to run in.
The runtime environment loads class files and ensures access to memory and other system resources to run them. Most software used the operating system (OS) as its runtime environment in the past.
Source: What is the JRE? Introduction to the Java Runtime …. https://www.infoworld.com/article/3304858/what-is-the-jre-introduction-to-the-java-runtime-environment.html
JRE in runtime env
The program ran inside whatever computer it was on but relied on operating system settings for resource access. Resources, in this case, would be things like memory and program files and dependencies. The Java Runtime Environment changed all that for Java programs.
JVM in runtime env
JRE is the implementation of JVM.
- Manages system memory and drives Java code or applications
- Becomes an instance of JRE at the runtime of a Java program.
- It is widely known as a runtime interpreter.
JDK
Java Development Kit (JDK) is a software development environment used for developing Java applications and applets.
It includes the
- Java Runtime Environment (JRE) – see # JRE
- Development Tools – see #Development Tools
Development Tools
It has
- Debugger – jdb
- Interpreter/loader (Java) (A program that reads in as input a source program, along with data for the program, and translates the source program instruction by instruction. For example, the Java interpreter java translates a . class file into code that can be executed natively on the underlying machine.)
- Compiler (javac)
- Archiver (jar) (a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution.)
- Documentation generator (Javadoc)
- see https://en.wikipedia.org/wiki/Java_Development_Kit for the full list
Source:
Differences between JDK, JRE and JVM – GeeksforGeeks. https://www.geeksforgeeks.org/differences-jdk-jre-jvm/
http://getkt.com/wp-content/uploads/2019/01/Understand-JDK-JRE-JVM.jpg
Compilers, Interpreters, and Emulators. https://introcs.cs.princeton.edu/java/82compiler/
IDE
An integrated development environment is an all-in-one tool for writing, editing, compiling, and running computer program. Software architecture IDE focuses on software development from the perspective of system architecture on the basis of software architectural formalized description.
IDE vs JDK
-
JDK, or Java Development Kit, is a bunch of compiled components that aid you in developing Java programs. a JDK (or more in general, an SDK or Software Development Kit) contains the functions, objects, and other components (such as virtual machines to run your app) for you to use in order to develop your own software. i.e. The JDK is the set of tools that allow you to write, compile and run Java code.
-
An IDE, or Integrated Development Environment, is a software tool that acts as the communication platform between you, the programmer, and the JDK. It’s the tool through which you can "talk" to the JDK, and use it to write your Java programs. i.e. The IDE relies on the JDK and has features to speed up your development activity
Source: ELI5: What’s the difference in JDK vs IDE : explainlikeimfive. https://www.reddit.com/r/explainlikeimfive/comments/3ngxew/eli5_whats_the_difference_in_jdk_vs_ide/
IDE Example
NetBeans, Eclipse, IntelliJ, and Visual Studio
Example – Run HelloWorld
This is how we run the HelloWorld App
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Compile the file – HelloWorld.class
is generated by this step
% javac HelloWorld.java
Run the app
% java HelloWorld
Result
"Hello world!"
Reference
- Tutorialspoint.com. 2022. Java Virtual Machine Tutorial. [online] Available at: https://www.tutorialspoint.com/java_virtual_machine/index.htm [Accessed 2 January 2022].
- freeCodeCamp.org. 2022. JVM Tutorial – Java Virtual Machine Architecture Explained for Beginners. [online] Available at: https://www.freecodecamp.org/news/jvm-tutorial-java-virtual-machine-architecture-explained-for-beginners/ [Accessed 2 January 2022].
- LinkedIn. 2022. jvm: Online Courses, Training and Tutorials on LinkedIn Learning. [online] Available at: https://www.linkedin.com/learning/search?keywords=jvm&u=61697657 [Accessed 2 January 2022].
- GeeksforGeeks. 2022. How JVM Works – JVM Architecture? – GeeksforGeeks. [online] Available at: https://www.geeksforgeeks.org/jvm-works-jvm-architecture/ [Accessed 2 January 2022].
- www.javatpoint.com. 2022. JVM | Java Virtual Machine – Javatpoint. [online] Available at: https://www.javatpoint.com/jvm-java-virtual-machine [Accessed 4 January 2022].
- Ibm.com. 2022. JVM vs. JRE vs. JDK: What’s the Difference?. [online] Available at: https://www.ibm.com/cloud/blog/jvm-vs-jre-vs-jdk [Accessed 4 January 2022].
- Tyson, M., 2022. What is the JRE? Introduction to the Java Runtime Environment. [online] InfoWorld. Available at: https://www.infoworld.com/article/3304858/what-is-the-jre-introduction-to-the-java-runtime-environment.html [Accessed 4 January 2022].
- Kalwan, A., 2022. Java Architecture and its Components | JVM, JRE and JDK | Edureka. [online] Edureka. Available at: https://www.edureka.co/blog/java-architecture/ [Accessed 4 January 2022].