Types of compilers in Java
There are two types of compilers in Java
- javac compiler (which is present in jdk at location programfiles or programfiles 86 =>java=>jdk=>bin=>javac (javac.exe)
- jit compiler (which is present in jvm internally)
other types of compiler for ide software
ECLIPSE-JDT or ECJ(eclipse compiler java)
GNU Compiler for Java .(GCJ)
javac compiler Responsiblities
javac compiler is responsible for converting source file(java file ) into bytecode ( it involves various checks and validations during the compilation process).Master the art of Java programming with our comprehensive Java Training in Pune. Our expert-led courses cover everything from the fundamentals to advanced concepts.
Lexical Analysis:Scanning the source code to analyze and break it down into tokens such as keywords, identifiers, literals, and operators.
For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!
Syntax checking: The javac compiler checks the Java source code for syntax errors.
If any syntax errors are found, the compiler will print an error message and will not generate any bytecode.
example -Keyword and Identifier Validation (public- plic,void-viod),Semicolon and Braces Usage,Method Signatures and Parameters
typechecking -The javac compiler is responsible for performing type checking and ensuring proper type casting according to java rules and regulations.
example -primitive typecasting,wrapperclass casting
semantic Analysis: Beyond syntax, the compiler performs semantic analysis to ensure that the code follows the
language’s semantics, including type checking, resolving symbols, and enforcing language rules.
Intermediate Code Generation:Creating an intermediate representation (IR) of the source code, typically in the form of JVM bytecode instructions.
For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!
Optimization:Performing various optimizations to improve the efficiency of the generated bytecode, such as constant folding, dead code elimination, and more.
Code Generation:Generating final JVM bytecode from the optimized intermediate representation that adheres to the Java Virtual Machine Specification.
exception Handling -The javac compiler ensures proper handling of checked exceptions in Java code, compelling
developers to either handle them using try-catch blocks or declare them in method signatures via the “throws” keyword,
enforcing adherence to Java’s exception handling rules.
Difference between javac and ECJ( Eclipse Java Compiler )(JDT – Java Development Tools)
JDK compiler (javac): Standalone tool, used from the command line or integrated into build scripts.
Eclipse compiler (ECJ): Integrated into the Eclipse IDE, providing seamless compilation within the development environment. Gain hands-on experience, dive into real-world projects, and elevate your Java skills. Join SevenMentor’s Java Classes in Pune
JDK compiler (javac): Full compilation: Compiles the entire source code file, even if only a small part has changed.
Eclipse compiler (ECJ): Incremental compilation: Only recompiles the modified parts of the code, significantly reducing compilation time for large projects.
For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!
JDK compiler (javac): Basic code analysis, focused on checking for syntax errors and semantic errors.
Eclipse compiler (ECJ): In-depth code analysis, providing real-time error reporting, code warnings, and code refactoring suggestions.
JDK compiler (javac): Strict error handling, halting the compilation process upon encountering errors.
Eclipse compiler (ECJ): More lenient error handling, allowing developers to run code with warnings, potentially
enabling debugging and testing despite minor errors.
JDK compiler (javac): Generates bytecode targeted for a specific Java version, ensuring compatibility with that version of the JVM.
Eclipse compiler (ECJ): Supports compatibility with multiple Java versions, allowing developers to compile code for different target platforms.
Role and responsiblities of java interpreter and jit compiler
Interpreter is responsible for reading bytecode line by line and then executes(without translating them into native machine code)
jit compiler is responsible increase the performance and efficiency of an interpreter it compiles the program and convert bytecode into native machine code .
Anonymous class
In Java, the compiler automatically generates the name of an anonymous inner class. The generated name is based on the enclosing class name, along with a unique identifier. The naming convention usually involves a combination of the outer class name
For example, if you create an anonymous inner class within a class named MyClass, the generated name might be something like MyClass$1.
The compiler generates anonymous inner class names to ensure that there are no naming conflicts.
Since anonymous inner classes are not explicitly named, the compiler needs to come up with a unique name for each one to avoid confusion.
For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!
The unique identifier ensures that the generated name is unique for each anonymous inner class defined within the enclosing class.
public class MyClass {
public void createAnonymousClass() {
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println(“Inside anonymous class”);
}
};
r.run();
}
}
=====================================================================================
The Java Virtual Machine (JVM) is an integral part of the Java runtime environment and is responsible for executing Java bytecode. There are two primary modes of executing Java programs within the JVM: interpretation and compilation.
Interpreter:
Execution Method: In the interpreter mode, the JVM reads the bytecode line by line and translates it into machine code or executes it directly.
Pros:
Immediate execution: The interpreter can start executing the program without the need for a lengthy compilation process.
Platform independence: Since the JVM interprets bytecode, Java programs can run on any platform with a JVM.
Cons:
Slower execution: Interpreting bytecode can be slower compared to native machine code because the translation happens at runtime.
Limited optimization: The interpreter typically performs minimal optimization, resulting in potentially slower execution compared to a compiled version.
Compiler:
Execution Method: In the compilation mode, the JVM translates the entire Java bytecode into native machine code before execution.
Pros:
Faster execution: Compiled code can be executed more quickly than interpreted code because it is already in native machine language.
Advanced optimizations: Compilation allows for more sophisticated optimizations, which can improve the overall performance of the program.
Cons:
Longer startup time: The compilation process adds an extra step before the program can start running, potentially leading to longer startup times.
Platform dependence: Compiled code is platform-specific, so separate compilations may be required for different platforms.
In practice, modern JVMs often use a combination of both interpretation and compilation to achieve a balance between startup time and runtime performance. This approach is known as Just-In-Time (JIT) compilation. The JVM may initially interpret the bytecode and then selectively compile frequently executed portions of code into native machine code for improved performance.
Do watch the video on Java: Click Here
It’s important to note that JVM implementations may vary, and advancements in technology and JVM design may introduce new techniques or optimizations over time.
Author:-
Pooja Ghodekar
Call the Trainer and Book your free demo Class For Java Call now!!!
| SevenMentor Pvt Ltd.
© Copyright 2021 | SevenMentor Pvt Ltd.