Java Tutorial
Jakob Jenkov |
Java was originally a programming language invented by Sun Microsystems (Sun was acquired by Oracle in January 2010). Since Java's invention it has become more than just a language. It is also a platform with a set of standard APIs and standard tools. Just exactly what Java has evolved into is covered in more detail in the next text in this tutorial, about what Java is today.
This Java tutorial trail (set of articles) describes the Java programming language. The intention of this tutorial is mostly to be a compressed introduction to Java for developers who already know a bit about programming and object-oriented concepts. I will try to explain the various different concepts as I use them, but a bit of programming background will helpful.
The target audience for this Java tutorial is both beginners who need to learn Java almost from scratch, and more experienced developers who just need to catch up on features they haven't used.
Java Tutorial Videos
I have made a few videos about Java related topics too. You can find them here:
- 7 + 2 Things I Love About Java
- Java Language (10 videos)
- Java Collections (9 videos)
- Java Concurrency (26 videos)
Java Topics Covered
This Java tutorial covers the following Java topics:
- What is Java?
- Installing the Java SDK
- Your First Java Application
- The Java main() Method
- Java Project Overview, Compilation and Execution
- Java Core Concepts
- Java Syntax
- Java Variables
- Java Data Types
- Java Math Operators and Math Class
- Java Arrays
- Java Strings
- Java Operations
- Java if Statement
- Java Ternary Operator
- Java switch Statement
- Java instanceof Operator
- Java for Loop
- Java while Loop
- Java Classes
- Java Fields
- Java Methods
- Java Constructors
- Java Packages
- Java Access Modifiers
- Java Inheritance
- Java Nested Classes
- Java Abstract Classes
- Java Interfaces
- Java Interfaces vs. Abstract Classes
- Java Enums
- Java Annotations
- Java Lambda Expressions
- Java Records
- Java Modules
- Java Exercises
Java APIs Covered
The whole Java platform consists of more than just the language concepts listed in the previous section. The Java platform contains a big set of ready-made components you can use. These components are organized into different APIs (Application Programming Interfaces) each of which address a specific area of functionality. This Java tutorial trail (articles listed above and in the left menu) only covers the Java language features. The Java APIs are covered in their own tutorial series. Here is a list containing most of the Java APIs:
API | Description |
---|---|
Java Collections | Contains functionality for working with sets, lists, stacks, queues etc. |
Java Concurrency | Explains the basic Java multithreading tools like threads and synchronized blocks, and the theory related to concurrency. |
Explains the Java concurrency utilities found in the java.util.concurrent package. | |
Java Cryptography | Contains functionality for working with encryption and decryption. |
Java Date Time | Explains the Java Date and Time APIs for working with dates and time. |
Java Exception Handling | Explains how Java exception handling works, and various tips and techniques around that. |
Java Generics | Explains Java Generics which enable to set a "generic" type for a given component at instantiation. For instance, to create a List of String objects. |
Java Internationalization | Covers the Java internationalization APIs which can help you adapt your applications to different country standards and languages. |
Java IO | Covers the Java IO APIs which enable you to read and write data from files and other external sources. |
Java JDBC | Covers the Java JDBC API which makes it possible for your Java apps to connect to relational databases. |
Java JSON | Covers various Java APIs for working with JSON in Java - e.g. serializing Java objects to JSON and vice versa. |
Java Logging | Explains the Java Logging APIs for logging data to log files. |
Java Networking | Explains the Java Networking API which enables you to make network connections to other computers. |
Java NIO | Explains the Java NIO (New IO / Nonblocking IO) API which provides a non-blocking IO API for Java. |
Java Reflection | Explains the Java Reflection API which enables you to inspect class and objects programmatically at runtime. |
Java Regular Expressions | The Java regular expressions API (Regex) . |
Java XML APIs | Explains the Java XML processing APIs. |
Java ZIP APIs | Explains the Java Zip compression and decompression APIs. |
Java JavaFX | Explains the powerful and popular JavaFX API for developing Java desktop apps. |
Java Versions Covered in This Tutorial
The Java language and platform is constantly evolving. New versions of Java are now released approximately every 6 months. I will update this tutorial with the latest features as fast as I am able to.
This particular tutorial trail only covers the core Java language features. It does not cover the Java APIs. Many of the Java APIs are covered elsewhere on this tutorial site.
If you are just looking for what is new in each Java version, the following sections will list the main additions to the latest Java versions.
New in Java 23
The main additions to Java 23 are:
- Primitive Types in Patterns, instanceof, and switch (Preview)
- Class-File API (Second Preview)
- Markdown Documentation Comments
- Vector API (Eighth Incubator)
- Stream Gatherers (Second Preview)
- Deprecate the Memory-Access Methods in sun.misc.Unsafe for Removal
- ZGC: Generational Mode by Default
- Module Import Declarations (Preview)
- Implicitly Declared Classes and Instance Main Methods
- Structured Concurrency (Third Preview)
- Scoped Values (Third Preview)
- Flexible Constructor Bodies (Second Preview)
New in Java 22
The main additions to Java 22 are:
- Region Pinning for G1
- Statements before super(...) (Preview)
- Foreign Function and Memory API
- Unnamed Variables & Patterns
- Class-File API (Preview)
- Launch Multi-File Source-Code Programs
- String Templates (2nd Preview)
- Vector API (7th Incubator)
- Stream Gatherers (Preview)
- Structured Concurrency (2nd Preview)
- Implicitly Declared Classes and Instance Main Methods (2nd Preview)
- Scoped Values
New in Java 21
The main additions to Java 21 are:
- String Templates (Preview)
- Sequenced Collections
- Generational ZGC
- Record Patterns
- Pattern Matching for switch
- Foreign Function & Memory API (Third Preview)
- Unnamed Patterns and Variables (Preview)
- Virtual Threads
- Scoped Values (Preview)
- Vector API (6th Incubator)
- Deprecate the Windows 32 bit x86 Port for Removal
- Prepare to Disallow the Dynamic Loading of Agents
- Key Encapsulation Mechanism API
- Structured Concurrency (Preview)
New in Java 20
The main additions to Java 20 are:
- Scoped Values (Incubator)
- Record Patterns (2nd Preview)
- Pattern Matching for switch (4th Preview)
- Foreign Function & Memory API (2nd Preview)
- Virtual Threads (2nd Preview)
- Structured Concurrency (2nd Incubator)
- Vector API (5th Incubator)
New in Java 19
The main additions to Java 19 are:
- Record Patterns (Preview)
- Linux/RISC-V Port
- Foreign Function & Memory API (Preview)
- Virtual Threads (Preview)
- Vector API (Fourth Incubator)
- Pattern Matching for switch (Third Preview)
- Structured Concurrency (Inbubator)
New in Java 18
The main additions to Java 18 are:
- Deprecate finalization for removal
- Internet Address Resolution SPI
- Pattern matching for switch - second preview
- Reimplementation of core reflection using method handles
- Small, simple web server able to serve only static content
- Foreign memory API (2nd incubator)
- Vector API (3rd incubator)
- UTF-8 as standard charset for Java APIs
- Code snippets in JavaDoc comments via the @snippet tag
New in Java 17
The main additions to Java 17 are:
- Restore always strict floating-point semantics.
- Enhanced pseudo-random number generators
- New MacOS rendering pipeline
- macOS/AArch64 port
- Deprecate the Applet API for Removal
- Strongly encapsulate JDK internals
- Pattern matching for switch
- Remove RMI Activation
- Sealed classes
- Remove experimental AOT and JIT compiler
- Deprecate the SecurityManager for removal
- Foreign function and memory API (incubator)
- Vector API for SIMD instruction access (2nd incubator)
- Context specific deserialization filters
New in Java 16
The main additions to Java 16 are:
- Packaging tool for generating native installers for Windows, Mac and Linux from your Java applications.
- Improvements to the Java Stream API.
- Java Records are out of preview and a permanent feature now.
- Vector API (incubator status) - for accessing CPU SIMD operations of the underlying hardware.
- ZGC Concurrent Thread-Stack Processing.
- Unix-Domain Socket Channels.
- Foreign Linker API (incubator).
- Foreign Memory Access API (3rd incubator).
- Invoke default methods via Java Reflection Proxy object.
- Pattern matching for the
instanceof
operator is out of preview and a permanent feature now. - Sealed classes are in second preview.
New in Java 15
The main additions to Java 15 are:
- Sealed Classes (Preview)
- Hidden classes
- Edwards-Curve Digital Signature Algorith (EdDSA).
- Reimplement the Legacy DatagramSocket API
- Pattern Matching for instanceof (Second Preview)
- Records (Second Preview)
- Foreign-Memory Access API (Second Incubator)
- Text blocks
- ZGC: A scalable low-latency garbage collector
- Shenandoah: A low-pause-time garbage collector
- Disable and deprecate biased locking
- Removed Nashorn JavaScript Engine
- Removed Solaris and Sparc ports
- Deprecate RMI Activation for Removal
New in Java 14
The main additions to Java 14 are:
- Records - Shallowly immutable classes (Preview).
- NullPointerException improvements.
- Text blocks - makes it easier to declare multiline strings.
- Java switch expressions become a permanent feature.
- Enhanced pattern matching for the Java instanceof operator.
- Non-volatile memory mapped byte buffers via the FileChannel API.
- Foreign memory access API to access memory outside the Java VM heap.
- NUMA aware memory allocation for the G1 garbage collector.
- Java Flight Recorder - Event streaming.
- A packaging tool for packing Java applications as selfcontained applications.
- Porting of Z garbage collector (ZGC) to MacOS and Windows
- Removal of the Concurrent Mark and Sweep garbage collector.
- Removal of pack200 / unpack200 zip tools for JAR files.
- Deprecation of Solaris/Sparc, Solaris/x64 and Linux/Sparc ports of Java.
Here is a link to the full list of changes in Java 14.
New in Java 13
The main additions to Java 13 are:
- Java switch expressions have been modified - not 100% backwards compatible with Java 12.
- Text blocks - makes it easier to declare multiline strings.
- Reimplementation of the legacy Socket API. The new implementation is cleaner, and should work better with user space threads - e.g fibers - which are being explored in Project Loom (possible future addition to Java).
- Z garbage collector now releases unused memory back to the OS, when unused for longer time.
Here is a link to the full list of changes in Java 13.
New in Java 12
The main additions to Java 12 are:
- A low pause time garbage collector called Shenandoah (OpenJDK only).
- Java switch expressions (preview - may be removed from Java again).
- JVM Constants API.
- Abortable mixed collections in G1 (garbage collections).
- Promptly return unused committed memory from G1.
Here is a link to the full list of changes in Java 12.
New in Java 11
The main additions to Java 11 are:
- Removed Java EE and Corba modules from JDK.
- HTTP Client made standard (the one added in Java 9).
- Java
var
keyword allowed as lambda expression parameter type. - Key agreement via elliptic curve cryptography.
- Unicode 10.
- New cryptographic algorithms.
- Launch single file source code programs (compiled when executed).
- TLS 1.3 support.
- Deprecate the Nashorn JavaScript engine
Here is a link to the full list of changes in Java 11.
New in Java 10
The main additions to Java 10 are:
- Local-Variable Type Inference
- Parallel Full Garbage Collection for G1 (a Garbage Collector)
- Graal included as experimental JIT compiler
- Several internal, JVM and platform level changes
Here is a link to the full list of changes in Java 10.
New in Java 9
The main additions to Java 9 are:
- Java Modules
- Java Reflection Module class
- Java Try With Resources Enhancement
- Java Compact Strings
- Java Microbenchmark Harness (JMH) included in the JDK
Here is a link to the full list of changes in Java 9.
New in Java 8
The main additions to Java 8 are:
- Java Lambda Expressions
- Java Streams (functional streams)
- JavaFX (bundled with Java SE from Java 8).
- The Nashorn JavaScript engine.
New in Java 7
The main new features in Java 7 are:
- Try with resources
- Catching Multiple Exceptions
- Java Fork and Join via ForkJoinPool
- Strings in Java switch statements.
- New file system API in Java NIO 2.0
- Type inference in generic declarations (the socalled "diamond" operator).
- Numeric literals with underscores as visual separators (e.g. 1_000_000).
- Binary numeric literals (e.g. 0b10100101).
- Support for dynamically typed languages in the Java Virtual Machine (JVM).
Get Started Learning Java
This page is merely an introduction to the Java tutorial trail. The real explaining starts from the next text (page) in the trail, What is Java? . You can see all the articles in this tutorial on every page, in the top left part of each page.
As you read the tutorials I would recommend that you play around with code examples etc. Personally, I learn better when I type in examples and run them. In order to do this, you need to:
- Write the Java code
- Compile the Java code
- Execute the compiled Java code
Install an IDE
The easiest way to do these three steps is to use a Java IDE (Integrated Development Environment). You can edit the Java code, compile it and execute it, all from within the IDE. There are three popular Java IDE's available:
Both Eclipse and Netbeans are free, and IntelliJ IDEA has a free community edition which is just fine when you start learning Java. Personally I use IntelliJ IDEA, but I have used all three IDEs from time to time. All of them will be fine for you to start out with.
If you do not want a full IDE, you can write Java code in a standard text editor, like Notepad (on Windows) or
Notepad++ (free text editor). Java file names have to end with the .java
extension. You can
then compile the Java files into Java byte code, and use the java
command to run the compiled
byte code.
Install a Java SDK
In order to compile and execute Java code this way, you need to download and install the Java SDK (Java Software Development Kit). You have two options: You can either download the Oracle Java SDK, or the Open JDK. From a Java perspective they are functionally equivalent, but there are some differences in the garbage collector implementations internally. I usually use the Open JDK release of the Java SDK.
You need to find out what version of Java you want to use. Normally, people tend to use either the version their company uses, or the latest long term support version (LTS), or the latest version. You might even want to install more than one Java SDK in case you want to play with new features in new versions. Just search online for "Oracle Java SDK Download" or "Open JDK Download" and you will find the page where you can download Java. Maybe include the version number in your search, if you want to download a specific version of the Java SDK.
Feel Free to Contact Me
If you disagree with anything I write here in this Java tutorial, or just have comments, questions, etc, feel free to send me an email. You wouldn't be the first to do so. You can find my email address on the about page.
Other Programming Languages
There are many other popular programming languages than Java. I am writing tutorials about a few of them. Here are the programming languages I am writing tutorials about:
Tweet | |
Jakob Jenkov |