Java Technology  «Prev  Next»
Lesson 4 The Java compiler
Objective Java compiler used to compile Java

Evolution of the Java Compiler from JDK 7 to JDK 22

The evolution of the Java Compiler from JDK 7 to JDK 22 has seen numerous enhancements, optimizations, and new features aimed at improving performance, usability, and language capabilities. Here are the key changes and advancements in the Java Compiler over this period:
JDK 7
  • Project Coin: Introduced small language enhancements such as try-with-resources, diamond operator, multi-catch exceptions, improved literals, and more, which the compiler had to support.
  • InvokeDynamic: Added support for the `invokedynamic` bytecode instruction, enhancing the JVM's support for dynamic languages.

JDK 8
  • Lambda Expressions: Added support for lambda expressions and the associated new bytecode instructions for improved functional programming capabilities.
  • Method References: Simplified referring to methods and constructors, requiring new parsing rules and bytecode generation.
  • Default Methods in Interfaces: Enabled interfaces to have default methods, requiring changes to the way the compiler handles method resolution.

JDK 9
  • Module System (Project Jigsaw): Introduced a module system to improve the modularity of Java applications. This required significant changes to the compiler to handle module declarations, module paths, and module-related access control.
  • Jigsaw: The compiler had to be adapted to check module boundaries and enforce module rules.
  • JShell: Introduced the Read-Eval-Print Loop (REPL) tool, which required the compiler to support interactive code evaluation.

JDK 10
  • Local-Variable Type Inference (var): Added support for the `var` keyword, allowing the compiler to infer the type of local variables, thus simplifying code without losing type safety.


JDK 11
  • New String Methods: Added several new methods to the `String` class (e.g., `strip`, `lines`, `repeat`), which the compiler needed to support.
  • Nest-Based Access Control: Improved the handling of nested classes, enhancing access control and reducing boilerplate code.

JDK 12
  • Switch Expressions (Preview): Introduced switch expressions, allowing switch statements to return values and making them more expressive.

JDK 13
  • Text Blocks (Preview): Added support for multi-line string literals, simplifying the inclusion of multi-line text in code.

JDK 14
  • Pattern Matching for `instanceof` (Preview): Simplified the code by combining type checks and casts in one step.
  • Records (Preview): Introduced records to simplify data carrier classes, requiring new syntax and bytecode support.

JDK 15
  • Sealed Classes (Preview): Introduced sealed classes, allowing more control over class hierarchies and enabling better optimization by the compiler.

JDK 16
  • Pattern Matching for `instanceof`: Finalized the pattern matching feature for `instanceof`.

JDK 17 (LTS)
  • Sealed Classes: Finalized the support for sealed classes.
  • Pattern Matching for Switch (Preview): Extended pattern matching to switch statements, making code more expressive and concise.

JDK 18
  • UTF-8 by Default: Changed the default character set to UTF-8, affecting how source files are read and compiled.
  • Simple Web Server: Added a simple web server for prototyping and testing, not directly a compiler feature but useful for developers.


JDK 19
  • Virtual Threads (Preview): Introduced lightweight virtual threads, impacting concurrency handling but not directly related to the compiler.

JDK 20
  • Improved Pattern Matching: Continued enhancements in pattern matching for both `instanceof` and switch.

JDK 21
  • Further Enhancements to Records and Sealed Classes: Ongoing improvements and refinements to records and sealed classes.

JDK 22
  • Project Loom (Virtual Threads): Enhanced support for virtual threads, making concurrency easier to manage and potentially impacting how the compiler handles threading constructs.
  • Refinements to Pattern Matching: Continued advancements in pattern matching, including enhancements for records, sealed classes, and switch statements.
  • More Language Features: Continued introduction of new language features and syntactic sugar to make code more expressive and maintainable.

Summary The Java Compiler has evolved significantly from JDK 7 to JDK 22, incorporating numerous language enhancements and optimizations. These changes have improved the expressiveness, performance, and usability of the Java language, making it more powerful and easier to use for developers. The introduction of features such as lambda expressions, the module system, local-variable type inference, pattern matching, records, and sealed classes have required continuous updates and improvements to the compiler to ensure robust and efficient code generation.

Java Compiler

Use the Java compiler to compile Java programs. The Java compiler[1] is probably the most important tool in the Java 2 SDK because it is responsible for compiling Java source code into executable Java programs. The Java compiler creates executable program code in a special format known as bytecode. Most compilers for other programming languages generate native executable code, which is code that is created to run solely on a specific type of computer platform. The Java compiler takes a very different approach by generating bytecode that can be executed on any platform that supports Java.

Compiling a Java Applet

The file TicTacToe.java that ships with the Java SDK contains the source code for a demonstration Java Applet that allows you to play Tic-Tac-Toe against the computer. To compile this source code file into an executable applet with the Java compiler, you would issue the following command at a command-line prompt (which you will do in this lesson's exercise):
javac TicTacToe.java
  1. The name of the Java compiler
  2. The source code file to be compiled

Java is a case-sensitive programming language. This especially applies to the Java API, which consists of classes, methods, and variables that are all given case-sensitive names. Even the names of applets are case-sensitive, which means the name of the applet in the source code (DateTime) must match the name of the source code file (DateTime.java).
After issuing the javac command, the file TicTacToe.class will be created, which is the executable applet class file that is ready to be embedded in a Web page. Executable Java programs are stored in files with a .class file extension because they are comprised of classes .
A class defines data and methods and is a unit of organization in a Java program.
A class is an object-oriented programming construct that attempts to make the modeling of real-world objects in software much more intuitive.
The Web page TicTacToe.html contains the embedded applet, and must be opened in a Web browser in order to run the applet.

Compiling Java Applet - Exercise

Click the Exercise link below to compile a Java applet using the Java compiler.
Compiling Java Applet - Exercise
[1] Class: A class is a template that defines the implementation of an object, effectively acting as a blueprint for the object.

SEMrush Software