In this article, we have covered the basics of java including: jvm, jdk, jre working. What are differences among these. Also, discussed if Java is free or paid? Also included JIT compiler and interpreter working.
Table of Contents:
- Java History in Brief
- Is Java Free or Paid?
- Origination of ‘Java’ Word As Language Name
- Concept of JVM, JRE and JDK
- Java SE vs. Java EE
- Java Working In Practice
- What Just Happened Internally and Role of JIT Compiler & Interpreter
- Difference Between Java Compiler and JIT Compiler
- Difference Between JIT Compiler and Interpreter
Java History in Brief
Java was originally developed by James Gosling & his team at Sun Microsystems and released in 1995 as a core component of Sun Microsystems. Then in Jan 2010, Oracle acquired Sun MIcrosystems. So, officially Java is owned by Oracle now.
Is Java Free or Paid?
Since 2018-19, a sequence of events has been taking place regarding Java and there are multiple articles and news out there related to the topic: Is Java paid or free?
Let’s see in brief, what happend in these years and why?
First thing first: Java is a language not a software or tool, so nobody can put a price on a language to use that. So, Java was not chargeable ever and can’t be either in future.
Everybody can use this language as a free of cost to develop any software/tool. But, like other languages java also needs a compiler/interpreter to covert the human written code to a machine native code. In case of Java, JDK serves that purpose. It compiles the written code and converts it into the bytecode and then into machine code.
Since, Oracle acquired Sun Microsystems so Oracle thought to make some profit out of Java. So, Oracle introduced pricing to use this jdk compiler. Oracle kept 2 options for us. one is OpenJDK and another one is OracleJDK.
The open jdk is an open source compiler. But it might have some bugs since it is an open source. Also, if any client companies (who is using jdk) need any support or a quick resolution to the bugs present in the compiler then it is not feasible in case of opensource jdk. So, for such companies Oracle gave the option to use Oracle JDK. For this, Oracle thought of charging them (client organisations using oracle jdk) for the support and quick resolutions to the bugs.
But, there happened another interesting event: As soon as Oracle announced the above, other companies also seek the opportunity and got chip in. They also announced that if some organisation wants to use the opensource jdk they can use and These companise WILL PROVIDE YOU THE REQUIRED SUPPORT. These other companies includes: Amazon Corretto by Amazon, Alibaba Dragonwell by Alibaba, Azul Zulu by Azul Systems, IBM Java SDK by IBM, JetBrains Runtime by Jetbrains, Microsoft Build of OpenJDK by Microsoft, Red Hat build of OpenJDK by RedHat , OpenLogic OpenJDK by OpenLogic and many more.
Since Oracle didn’t get much traction towards the Oracle JDK as they planned and didn’t get the expected business. So from Java 17, Oracle has made the Oracle JDK also as free to use for dev, testing, production, commercial etc. Still, if you need any support that is chargeable.
NOTE: JRE and JVM are the client side softwares, which were always free in this entire event.
Origination of ‘Java’ Word As Language Name
The Dutch brought coffee to Southeast Asia in the 1600s. They transported coffee trees to locations where it is still grown today, including Bali and Sumatra. Java was a different island where they started growing coffee, and it is from this island that the word “java” originated.
The exact context of the term’s initial use is unknown. The name was perhaps initially used by the Dutch, who might have used it to describe single-origin coffee from Java. However, as the coffee industry expanded, the name was embraced by more and more people globally, and any distinctiveness was lost. Coffee is now referred to as “java” in a generic sense rather than only as coffee from the Island of Java.
This is also the reason of why Java language has a logo of steaming cup of coffee.
Concept of JVM, JRE and JDK
These 3 are the most important things to understand if we want to understand how Java works.
What is Java Virtual Machine (JVM)?
The main responsibility of JVM is to translate the bytecode into machine code and execution of the java program. Along with these it is also responsible for memory management and security.
JVM can also run any code written into another language if it is translated to Java Bytecode.
JVM is built up from the following 3 sub components:
- Class Loader Subsystem: Which is responsible for loading, linking and initializing a Java class file (i.e., “Java file”).
- Runtime Data Areas:This contains method areas, PC registers, stack areas and threads.
- Execution Engine: This component actually excutes the java program and it consists java interpreter, JIT compiler and garbage collector.
The parts of JVM memory includes: Method Area, Heap, JVM Language Stacks, PC Registers, Native Method Stacks.
Whenever we run the java command (ie: $java Mycode) on the command prompt to run the java program, a new JVM instance is created each time.
JVM is just the specification, there are multiple implementations available for this specification by multiple providers. Ex: Java Hotspot is provided by Oracle, JRockit by BEA Systems etc.
What is Java Runtime Environment (JRE)?
This is the environment in which our java program/application gets executed. JRE uses heap space for dynamic memory allocation for Java objects.
What is Java Development Kit (JDK)?
Java Development Kit, or JDK, is a software development kit often called a superset of JRE. JDK is not platform independent (it is platform-specific), so we need to install different jdk binary for each operating system (e.g., Mac, Unix and Windows).
The Role/Responsibilities of JDK in Java
JDK contains all the tools required to compile, debug and run a program developed using the Java platform.
JDK Components
JDK includes all the Java tools, executables and binaries needed to run Java programs. This includes JRE, a java compiler, a debugger, an archiver and other tools that are used in Java development.
JDK vs. JRE vs. JVM: Key differences
- JDK is the development tool, while JRE is for execution of the program.
- JVM is the actual execution unit for a java application.
- JVM is included in both JDK and JRE – Java programs can’t run without it.
- We can say that JRE=JVM+Java Packages Classes (like util, math, lang etc) + runtime libraries.
- Also, we can say that: JDK =JRE+debugger+java compiler+other development tools.
- JVM, JRE, JDK all are platform dependent because configurations differ for each OS. But, Java is platform dependent.
- JRE just contains those files which are required to run a java application. But, JDK contains JRE and all those libraries, required to develop a Java application.
Summarized Difference (JVM vs JRE vs JDK)
- JVM = JIT Compiler + Java Interpreter + Garbage Collector + class loader
- JRE = JVM + Library Classes
- JDK = JRE + Development Tool
Other Important Points
- Since, JVM is platform dependent. That’s why we would require different JVM for linux and Mac. But the java bytecodes are platform-independent instructions for the Java VM. So, the same bytecodes can be run on any of the platform.
- Whenever any java software is shipped to a client then the source code will not be shipped, only the compiled code will be shipped. So that the client(end user) can install the JRE (not JDK, which is bigger in size) and execute the compiled code
Java SE vs. Java EE
Java is synonymous with Java Standard Edition (Java SE) or Core Java. Wherever we use word Java, it means we are talking about the Java SE. On the flip side, Java EE provides APIs and is typically used to run larger applications.
Java Working In Practice
- When we run a java class using command line then: we first run the following command:
- Now, we will invoke the JVM to run the above generated class file:
What Just Happened Internally and Role of JIT Compiler & Interpreter
Java compiler (present into JDK) compiles the source code and coverts it into byte code. JIT compiler (part of JVM) compiles the byte code and converts it into machine native code. Since, executing the machine native code is much faster than executing the bytecode. So, we can say that the JIT compiler helps to improve the performance of Java programs by compiling bytecodes into native machine code at run time. And the interpreter doesn’t convert the bytecode to machine native code, it only evaluates the bytecode and executes the instructions written in bytecode.
Question: If executing native code is much more efficient then why JIT compiler doesn’t compile the entire bytecode into native machine code?
Ans: For sure, it will be very much efficient if JIT compiler compiles the entire bytecode, but compilation itself is a costly operation, so JIT compiler compiles only that part of the code which is used more frequently, so that the interpreter does not have to interprete that part of the code again and again. And the interpretation time can be saved by one-time investment by JIT compiler in terms of the memory+time.
The difference between compiler and interpreter is: Compiler translates the entire program at once but interpreter executes the code instruction by instruction. That’s why in case of compiler we get all the errors shown at once at the end of the compilation. but interpreter stops executing as soon as any error occurs.
Question: Why would we even want to compile the source code at the first place, why can’t we just interpret directly and convert the source code directly into the machine level code?
Ans: For high level languages we first compile them into a language that is easier to parse and easier to interpret. Then we interprete that compiled code using the interpreter. Java is one of these high level and complex language. Other languages includes: Ruby, Python.
Difference Between Java Compiler and JIT Compiler
- Java compiler is part of JDK , but JIT compiler is part of JVM.
- Java compiler converts human readable code to bytecode. JIT compiler converts bytecode to native machine code. But it JIT only converts to optimise the performance and for only those part of the code which is frequenctly used.
Difference Between JIT Compiler and Interpreter
- JIT compiler converts bytecode to native machine code. On the other side, Interpreter doesn’t convert bytecode to native code. It just analyses/interprets the code and executes that.