JAVA CORE CONCEPT
What are the components of Java Architecture?
What is Java Architecture?
Here, I will explain you the java architecture in simple steps.
- In Java, there is a process of compilation and interpretation.
- The code written in Java, is converted into byte codes which is done by the Java Compiler.
- The byte codes, then are converted into machine code by the JVM.
- The Machine code is executed directly by the machine.
What are the components of Java Architecture?
What is Java Architecture?
Here, I will explain you the java architecture in simple steps.
- In Java, there is a process of compilation and interpretation.
- The code written in Java, is converted into byte codes which is done by the Java Compiler.
- The byte codes, then are converted into machine code by the JVM.
- The Machine code is executed directly by the machine.
Components of Java Architecture
There are three main components of Java language: JVM, JRE, and JDK.
Java Virtual Machine, Java Runtime Environment and Java Development Kit respectively.
Let me elaborate each one of them one by one:
Java Virtual Machine:
Ever heard about WORA? (Write once Run Anywhere). Well, Java applications are called WORA because of their ability to run a code on any platform. This is done only because of JVM. The JVM is a Java platform component that provides an environment for executing Java programs. JVM interprets the bytecode into machine code which is executed in the machine in which the Java program runs.
There are three main components of Java language: JVM, JRE, and JDK.
Java Virtual Machine, Java Runtime Environment and Java Development Kit respectively.
Let me elaborate each one of them one by one:
Java Virtual Machine:
Ever heard about WORA? (Write once Run Anywhere). Well, Java applications are called WORA because of their ability to run a code on any platform. This is done only because of JVM. The JVM is a Java platform component that provides an environment for executing Java programs. JVM interprets the bytecode into machine code which is executed in the machine in which the Java program runs.
Explanation:
Class Loader: Class loader is a subsystem of JVM. It is used to load class files. Whenever we run the java program, class loader loads it first.
Class method area: It is one of the Data Area in JVM, in which Class data will be stored. Static Variables, Static Blocks, Static Methods, Instance Methods are stored in this area.
Heap: A heap is created when the JVM starts up. It may increase or decrease in size while the application runs.
Stack: JVM stack is known as a thread stack. It is a data area in the JVM memory which is created for a single execution thread. The JVM stack of a thread is used by the thread to store various elements i.e.; local variables, partial results, and data for calling method and returns.
Native stack: It subsumes all the native methods used in your application.
Execution Engine:
- JIT compiler
- Garbage collector
JIT compiler: The Just-In-Time (JIT) compiler is a part of the runtime environment. It helps in improving the performance of Java applications by compiling bytecodes to machine code at run time. The JIT compiler is enabled by default. When a method is compiled, the JVM calls the compiled code of that method directly. The JIT compiler compiles the bytecode of that method into machine code, compiling it “just in time” to run.
Garbage collector: As the name explains that Garbage Collector means to collect the unused material. Well, in JVM this work is done by Garbage collection. It tracks each and every object available in the JVM heap space and removes unwanted ones.
Garbage collector works in two simple steps known as Mark and Sweep:
Mark – it is where the garbage collector identifies which piece of memory is in use and which are not
Sweep – it removes objects identified during the “mark” phase.
Java Runtime Environment:
The JRE software builds a runtime environment in which Java programs can be executed. The JRE is the on-disk system that takes your Java code, combines it with the needed libraries, and starts the JVM to execute it. The JRE contains libraries and software needed by your Java programs to run. JRE is a part of JDK (which we will study later) but can be downloaded separately.
Java Development Kit:
The Java Development Kit (JDK) is a software development environment used to develop Java applications and applets. It contains JRE and several development tools, an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) accompanied with another tool.
The blue area shown in the diagram is JDK. Now, let me elaborate the development tools to you all.
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.
Moving ahead with Java architecture, let us understand how Java platform is independent?
How is Java platform independent?
When is any programming language called as platform-independent? Well, if and only if it can run on all available operating systems with respect to its development and compilation.
Now, Java is platform-independent just because of the bytecode. Let me tell you what exactly is a bytecode? In simple terms,
Bytecode is a code of the JVM which is machine-understandable.
Bytecode execution in Java proves it is a platform-independent language.
Here, I will show you the steps involved in the process of java bytecode execution.
Class Loader: Class loader is a subsystem of JVM. It is used to load class files. Whenever we run the java program, class loader loads it first.
Class method area: It is one of the Data Area in JVM, in which Class data will be stored. Static Variables, Static Blocks, Static Methods, Instance Methods are stored in this area.
Heap: A heap is created when the JVM starts up. It may increase or decrease in size while the application runs.
Stack: JVM stack is known as a thread stack. It is a data area in the JVM memory which is created for a single execution thread. The JVM stack of a thread is used by the thread to store various elements i.e.; local variables, partial results, and data for calling method and returns.
Native stack: It subsumes all the native methods used in your application.
Execution Engine:
- JIT compiler
- Garbage collector
JIT compiler: The Just-In-Time (JIT) compiler is a part of the runtime environment. It helps in improving the performance of Java applications by compiling bytecodes to machine code at run time. The JIT compiler is enabled by default. When a method is compiled, the JVM calls the compiled code of that method directly. The JIT compiler compiles the bytecode of that method into machine code, compiling it “just in time” to run.
Garbage collector: As the name explains that Garbage Collector means to collect the unused material. Well, in JVM this work is done by Garbage collection. It tracks each and every object available in the JVM heap space and removes unwanted ones.
Garbage collector works in two simple steps known as Mark and Sweep:
Mark – it is where the garbage collector identifies which piece of memory is in use and which are not
Sweep – it removes objects identified during the “mark” phase.
Java Runtime Environment:
The JRE software builds a runtime environment in which Java programs can be executed. The JRE is the on-disk system that takes your Java code, combines it with the needed libraries, and starts the JVM to execute it. The JRE contains libraries and software needed by your Java programs to run. JRE is a part of JDK (which we will study later) but can be downloaded separately.
Java Development Kit:
The Java Development Kit (JDK) is a software development environment used to develop Java applications and applets. It contains JRE and several development tools, an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) accompanied with another tool.
The blue area shown in the diagram is JDK. Now, let me elaborate the development tools to you all.
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.
Moving ahead with Java architecture, let us understand how Java platform is independent?
How is Java platform independent?
When is any programming language called as platform-independent? Well, if and only if it can run on all available operating systems with respect to its development and compilation.
Now, Java is platform-independent just because of the bytecode. Let me tell you what exactly is a bytecode? In simple terms,
Bytecode is a code of the JVM which is machine-understandable.
Bytecode execution in Java proves it is a platform-independent language.
Here, I will show you the steps involved in the process of java bytecode execution.
Below is the explanation of the steps involved:
sample.java → javac (sample. class) → JVM(sample.obj) → final output
First source code is used by java compiler and is converted in .class file. The class file code is in byte code form and that class file is used by JVM to convert into an object file. After that, you can see the final output on your screen.
Moving ahead in Java architecture article, let us understand the concept of JIT in Java.
JIT in Java
Just In Time compiler commonly known as JIT, is basically responsible for performance optimization of java based applications at run time. The performance of an application is dependent on a compiler.
Here is a simple diagram showing you the internal process going on.
sample.java → javac (sample. class) → JVM(sample.obj) → final output
First source code is used by java compiler and is converted in .class file. The class file code is in byte code form and that class file is used by JVM to convert into an object file. After that, you can see the final output on your screen.
Moving ahead in Java architecture article, let us understand the concept of JIT in Java.
JIT in Java
Just In Time compiler commonly known as JIT, is basically responsible for performance optimization of java based applications at run time. The performance of an application is dependent on a compiler.
Here is a simple diagram showing you the internal process going on.
The JIT compiler compiles the byte code of the method into machine code, compiling it “Just In Time” to run. When a method is compiled, the JVM calls the compiled code of that method directly.
Let’s dive deeper:
The byte code has to be interpreted or compiled to proper machine instructions depending on the instruction set provided. Also, these can be directly executed if the instruction architecture is byte code based. Interpreting the byte code affects the speed of execution.
In order to improve performance, JIT compilers interact with the Java Virtual Machine (JVM) at run time and compile suitable bytecode sequences into native machine code (as shown in the diagram). While using a JIT compiler, the hardware is able to execute the native code, as compared to having the JVM interpret the same sequence of bytecode repeatedly and incurring overhead for the translation process.
Let’s dive deeper:
The byte code has to be interpreted or compiled to proper machine instructions depending on the instruction set provided. Also, these can be directly executed if the instruction architecture is byte code based. Interpreting the byte code affects the speed of execution.
In order to improve performance, JIT compilers interact with the Java Virtual Machine (JVM) at run time and compile suitable bytecode sequences into native machine code (as shown in the diagram). While using a JIT compiler, the hardware is able to execute the native code, as compared to having the JVM interpret the same sequence of bytecode repeatedly and incurring overhead for the translation process.
What are Comments in Java?
Single-line comments
Multi-line comments
Documentation comments
Single-line comments
Multi-line comments
Documentation comments
Comments in Java are often are non-executable statements, used to understand the code better and to make it more readable. As mentioned above, it can also be helpful in preventing code execution when testing an alternative code. In short, Java comments are non-executable statements, used to understand Code better.
Single-line Comments
This type of comment is mainly used for describing the code functionality. It is the easiest typed comments. Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by Java implies it will not be executed. Now, let’s take a small example to know how single-line comments can be used.
This type of comment is mainly used for describing the code functionality. It is the easiest typed comments. Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by Java implies it will not be executed. Now, let’s take a small example to know how single-line comments can be used.
You can see in the above example that I have used single-line comments to comment on the specific lines in a program. That’s how it works. Now, let’s understand what are multi-line comments.
Multi-line Comments
To describe a full method in a code or a complex snippet single line comments can be tedious to write since we have to give ‘//’ at every line. So to overcome this multi-line comments can be used. Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored by Java. It is used to comment multiple lines of code.
Now, let’s take a small example to know how multi-line comments can be used.
To describe a full method in a code or a complex snippet single line comments can be tedious to write since we have to give ‘//’ at every line. So to overcome this multi-line comments can be used. Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored by Java. It is used to comment multiple lines of code.
Now, let’s take a small example to know how multi-line comments can be used.
You can see in the above example I have used multi-line comments to comment on the lines in a program. That’s how it works. Now, let’s understand what is documentation comments.
Documentation Comments
This type of comments is used generally when you are writing code for a project/ software package. It helps you to generate a documentation page for reference, which can be used for getting information about methods present, its parameters, etc.
This type of comments is used generally when you are writing code for a project/ software package. It helps you to generate a documentation page for reference, which can be used for getting information about methods present, its parameters, etc.
What are Java Keywords and reserved words?
Keywords are predefined which have a unique meaning and functionality in Java programming language. These keywords are also known as reserved keywords which mean they cannot be used as a variable name, class, method or any other identifier. There are 57 reserved keywords in Java. Meanwhile, in this huge list of java keywords, there are some which are not used anymore and few versions do not support a tiny number of keywords.
Keywords are predefined which have a unique meaning and functionality in Java programming language. These keywords are also known as reserved keywords which mean they cannot be used as a variable name, class, method or any other identifier. There are 57 reserved keywords in Java. Meanwhile, in this huge list of java keywords, there are some which are not used anymore and few versions do not support a tiny number of keywords.