thumb|300px|A snippet of Java code.
The syntax of Java is the set of rules defining how a Java program is written and interpreted.
The syntax is mostly derived from C and C++. Unlike C++, Java has no global functions or variables, but has data members which are also regarded as global variables. All code belongs to classes and all values are objects. The only exception is the primitive data types, which are not considered to be objects for performance reasons (though can be automatically converted to objects and vice versa via autoboxing). Some features like operator overloading or unsigned integer data types are omitted to simplify the language and avoid possible programming mistakes.
The Java syntax has been gradually extended in the course of numerous major JDK releases, and now supports abilities such as generic programming and anonymous functions (function literals, called lambda expressions in Java). Since 2017, a new JDK version is released twice a year.
Basics
The Java "Hello, World!" program program is as follows:
<syntaxhighlight lang="java">
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
</syntaxhighlight>
Since Java 25, a simplified Hello World program without an explicit class may be written:
<syntaxhighlight lang="java">
void main() {
IO.println("Hello World!");
}
</syntaxhighlight>
Identifier
An identifier is the name of an element in the code. There are certain standard naming conventions to follow when selecting names for elements. Identifiers in Java are case-sensitive.
An identifier can contain:
- Any Unicode character that is a letter (including numeric letters like Roman numerals) or digit.
- Currency sign (such as ).
- Connecting punctuation character (such as Underscore|).
An identifier cannot:
- Start with a digit.
- Be equal to a reserved keyword, null literal or Boolean literal.
Keywords
Keywords
The following words are keywords and cannot be used as identifiers under any circumstances, of which there are 47.
- <code>abstract</code>
- <code>assert</code>
- <code>boolean</code>
- <code>break</code>
- <code>byte</code>
- <code>case</code>
- <code>catch</code>
- <code>char</code>
- <code>class</code>
- <code>continue</code>
- <code>default</code>
- <code>do</code>
- <code>double</code>
- <code>else</code>
- <code>enum</code>
- <code>extends</code>
- <code>final</code>
- <code>finally</code>
- <code>float</code>
- <code>for</code>
- <code>if</code>
- <code>implements</code>
- <code>import</code>
- <code>instanceof</code>
- <code>int</code>
- <code>interface</code>
- <code>long</code>
- <code>native</code>
- <code>new</code>
- <code>package</code>
- <code>private</code>
- <code>protected</code>
- <code>public</code>
- <code>return</code>
- <code>short</code>
- <code>static</code>
- <code>super</code>
- <code>switch</code>
- <code>synchronized</code>
- <code>this</code>
- <code>throw</code>
- <code>throws</code>
- <code>transient</code>
- <code>try</code>
- <code>void</code>
- <code>volatile</code>
- <code>while</code>
Reserved identifiers
The following words are contextual keywords and are only restricted in certain contexts, of which there are 16.
- <code>exports</code>
- <code>module</code>
- <code>non-sealed</code>
- <code>open</code>
- <code>opens</code>
- <code>permits</code>
- <code>provides</code>
- <code>record</code>
- <code>requires</code>
- <code>sealed</code>
- <code>to</code>
- <code>transitive</code>
- <code>var</code>
- <code>when</code>
- <code>with</code>
- <code>yield</code>
Reserved words for literal values
The following words refer to literal values used by the language, of which there are 3.
- <code>true</code>
- <code>false</code>
- <code>null</code>
Unused
The following words are reserved as keywords, but currently have no use or purpose, of which there are 4.
- <code>_</code>
- <code>const</code>
- <code>goto</code>
- <code>strictfp</code>
Literals
{| class="wikitable"
|-
!colspan="2"|Integers
|-
!binary (introduced in Java SE 7)
| ( followed by a binary number)
|-
!octal
| ( followed by an octal number)
|-
!hexadecimal
| ( followed by a hexadecimal number)
|-
!decimal
| (decimal number)
|-
!colspan="2"|Floating-point values
|-
!rowspan="2" | float
|, , (decimal fraction with an optional exponent indicator, followed by )
|-
|, ( followed by a hexadecimal fraction with a mandatory exponent indicator and a suffix )
|-
!rowspan="2" | double
|, , , (decimal fraction with an optional exponent indicator, followed by optional )
|-
|, ( followed by a hexadecimal fraction with a mandatory exponent indicator and an optional suffix )
|-
!colspan="2"|Character literals
|-
!char
|, , (character or a character escape, enclosed in single quotes)
|-
!colspan="2"|Boolean literals
|-
!boolean
|,
|-
!colspan="2"|null literal
|-
!null reference
|
|-
!colspan="2"|String literals
|-
!String
| (sequence of characters and character escapes enclosed in double quotes)
|-
!colspan="2"|Characters escapes in strings
|-
!Unicode character
| ( followed by the hexadecimal unicode code point up to U+FFFF)
|-
!Octal escape
| (octal number not exceeding 377, preceded by backslash)
|-
!Line feed
|
|-
!Carriage return
|
|-
!Form feed
|
|-
!Backslash
|
|-
!Single quote
|
|-
!Double quote
|
|-
!Tab
|
|-
!Backspace
|
|}
Integer literals are of <code>int</code> type by default unless <code>long</code> type is specified by appending <code>L</code> or <code>l</code> suffix to the literal, e.g. <code>367L</code>. Since Java SE 7, it is possible to include underscores between the digits of a number to increase readability; for example, a number can be written as .
Variables
Variables are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement by assigning a value.
<syntaxhighlight lang="java">
int count; // Declaring an uninitialized variable called 'count', of type 'int'
count = 35; //Initializing the variable
int count = 35; // Declaring and initializing the variable at the same time
</syntaxhighlight>
Multiple variables of the same type can be declared and initialized in one statement using comma as a delimiter.
<syntaxhighlight lang="java">
int a, b; // Declaring multiple variables of the same type
int a = 2, b = 3; // Declaring and initializing multiple variables of the same type
</syntaxhighlight>
Type inference
Since Java 10, it has become possible to infer types for the variables automatically by using <code>var</code>.
<syntaxhighlight lang="java">
import java.io.FileOutputStream;
// stream will have the FileOutputStream type as inferred from its initializer
var stream = new FileOutputStream("file.txt");
// An equivalent declaration with an explicit type
FileOutputStream stream = new FileOutputStream("file.txt");
</syntaxhighlight>
Code blocks
The separators and signify a code block and a new scope. Class members and the body of a method are examples of what can live inside these braces in various contexts.
Inside of method bodies, braces may be used to create new scopes, as follows:
<syntaxhighlight lang="java">
void doSomething() {
int a;
{
int b;
a = 1;
}
a = 2;
b = 3; // Illegal because the variable b is declared in an inner scope..
}
</syntaxhighlight>
Comments
Java has three kinds of comments: traditional comments, end-of-line comments and documentation comments.
Traditional comments, also known as block comments, start with <code>/*</code> and end with <code>*/</code>, they may span across multiple lines. This type of comment was derived from C and C++.
<syntaxhighlight lang="java">
/* This is a multi-line comment.
It may occupy more than one line. */
</syntaxhighlight>
End-of-line comments start with <code>//</code> and extend to the end of the current line. This comment type is also present in C++ and in modern C.
<syntaxhighlight lang="java">
// This is an end-of-line comment
</syntaxhighlight>
Documentation comments in the source files are processed by the Javadoc tool to generate documentation. This type of comment is identical to traditional comments, except it starts with <code>/**</code> and follows conventions defined by the Javadoc tool. Technically, these comments are a special kind of traditional comment and they are not specifically defined in the language specification.
<syntaxhighlight lang="java">
/**
- This is a documentation comment.
- @author John Doe
- /
</syntaxhighlight>
Universal types
Classes in the package <code>java.lang</code> (but not its subpackages) are implicitly imported into every program, as long as no explicitly-imported types have the same names. Important ones include:
<code>java.lang.System</code>
<code>java.lang.System</code> is one of the most fundamental classes in Java. It is a utility class for interacting with the system, containing standard input, output, and error streams, system properties and environment variables, time, and more.
<code>java.lang.Object</code>
<code>java.lang.Object</code> is Java's top type. It is implicitly the superclass of all classes that do not declare any parent class (thus all classes in Java inherit from <code>Object</code>). All values can be converted to this type, although for primitive values this involves autoboxing.
<code>java.lang.Record</code>
All records implicitly extend <code>java.lang.Record</code>. Thus, a <code>record</code>, while treated as a <code>class</code>, cannot extend any other class.
<code>java.lang.Enum<E></code>
All enumerated types (enums) in Java implicitly extend <code>java.lang.Enum<E></code>. Thus, an <code>enum</code>, while treated as a <code>class</code>, cannot extend any other class. The type parameter <code>E</code> is constrained as <syntaxhighlight lang="java" inline>Enum<E extends Enum<E>></syntaxhighlight>.
<code>java.lang.Class<T></code>
<code>java.lang.Class<T></code> is a class that represents a <code>class</code> or <code>interface</code> in the application at runtime. It cannot be constructed via direct instantiation, but is rather created by the JVM when a class is derived from the bytes of a <code>.class</code> file. A class literal can be obtained through <code>X.class</code> on such a class <code>X</code>. For instance, <code>String.class</code> returns <code>Class<String></code>. An unknown class being modeled can be represented as <code>Class<?></code> (where <code>?</code> denotes a wildcard).
While <code>X.class</code> appears to be accessing a field named "", there is actually no such field named ""; it is in fact performing the instruction <code>ldc</code>, which pushes a constant from the class's runtime constant pool into the operand stack.
<code>java.lang.String</code>
<code>java.lang.String</code> is Java's basic string type. It is immutable. It does not implement <code>Iterable<Character></code>, so it cannot be iterated over in a for-each loop, but can be converted to <code>char[]</code>. Some methods treat each UTF-16 code unit as a <code>char</code>, but methods to convert to an <code>int[]</code> that is effectively UTF-32 are also available. <code>String</code> implements <code>java.lang.CharSequence</code>, so <code>char</code>s in the <code>String</code> can be accessed by the method <code>charAt()</code>.
<code>java.lang.Throwable</code>
<code>java.lang.Throwable</code> is the supertype of everything that can be thrown or caught with Java's <code>throw</code> and <code>catch</code> statements. Its direct known subclasses are <code>java.lang.Error</code> (for serious unrecoverable errors) and <code>java.lang.Exception</code> (for exceptions that may naturally occur in the execution of a program).
<code>java.lang.Error</code>
<code>java.lang.Error</code> is the supertype of all error classes and extends <code>Throwable</code>. It is used to indicate conditions that a reasonable application should not catch.
<code>java.lang.Exception</code>
<code>java.lang.Exception</code> is the supertype of all exception classes and extends <code>Throwable</code>. It is used to indicate conditions that a reasonable application may have reason to catch.
<code>java.lang.Math</code>
<code>java.lang.Math</code> is a utility class containing mathematical functions and mathematical constants (such as <code>Math.sin()</code>, <code>Math.pow()</code>, and <code>Math.PI</code>).
<code>java.lang.IO</code>
<code>java.lang.IO</code> is a class introduced in Java 25 (previously residing in <code>java.io</code> as <code>java.io.IO</code>). It allows simpler access to the standard input and output streams over <code>System.in</code> and <code>System.out</code>.
Primitives
Each primitive type has an associated wrapper class (see primitive types).
Program structure
Java applications consist of collections of classes. Classes exist in packages but can also be nested inside other classes.
<code>main</code> method
Every Java application must have an entry point. This is true of both graphical interface applications and console applications. The entry point is the <code>main</code> method. There can be more than one class with a <code>main</code> method, but the main class is always defined externally (for example, in a manifest file). The <code>main</code> method along with the main class must be declared <code>public</code>. The method must be <code>static</code> and is passed command-line arguments as an array of strings. Unlike C++ or C#, it never returns a value and must return <code>void</code>. However, a return code can be specified to the operating system by calling <code>System.exit()</code>.
<syntaxhighlight lang=Java>
public static void main(String[] args) {
// ...
}
</syntaxhighlight>
Packages
Packages are a part of a class name and they are used to group and/or distinguish named entities from other ones. Another purpose of packages is to govern code access together with access modifiers. For example, <code>java.io.InputStream</code> is a fully qualified class name for the class <code>InputStream</code> which is located in the package <code>java.io</code>.
A package is essentially a namespace. Packages do not have hierarchies, even though the periods may suggest so. A package can be controlled whether it is accessible externally or internally of a project using modules.
A package is declared at the start of the file with the <code>package</code> declaration:
<syntaxhighlight lang=Java>
package org.wikipedia.examples;
public class Foo {
// ...
}
</syntaxhighlight>
Classes with the <code>public</code> modifier must be placed in the files with the same name and extension and put into nested folders corresponding to the package name. The above class <code>org.wikipedia.examples.Foo</code> will have the following path: <code>org/wikipedia/examples/Foo.java</code>.
Modules
Modular programming (modules) is used to group packages and tightly control what packages belong to the public API. Contrary to JAR files, modules explicitly declare which modules they depend on, and what packages they export. Explicit dependency declarations improve the integrity of the code, by making it easier to reason about large applications and the dependencies between software components.
The module declaration is placed in a file named at the root of the module's source-file hierarchy. The JDK will verify dependencies and interactions between modules both at compile-time and runtime.
For example, the following module declaration declares that the module <code>org.wikipedia.bar</code> depends on another <code>org.wikipedia.baz</code> module, and exports the following packages: <code>org.wikipedia.bar.alpha</code> and <code>org.wikipedia.bar.beta</code>:
<syntaxhighlight lang="java">
module org.wikipedia.bar {
requires org.wikipedia.baz;
exports org.wikipedia.bar.alpha;
exports org.wikipedia.bar.beta;
}
</syntaxhighlight>
The public members of <code>org.wikipedia.bar.alpha</code> and <code>org.wikipedia.bar.beta</code> packages will be accessible by dependent modules. Private members are inaccessible even through a means such as reflective programming (reflection). In Java versions 9 through 16, whether such 'illegal access' is de facto permitted depends on a command line setting.
The JDK became modularized in Java 9. For example, the majority of the Java standard library is exported by the module <code>java.base</code>.
Import declaration
An import statement is used to resolve a type belonging to another package (namespace). It can be seen as similar to <code>using</code> in C++.
Type import declaration
A type import declaration allows a named type to be referred to by a simple name rather than the full name that includes the package. Import declarations can be single type import declarations or import-on-demand declarations. Import declarations must be placed at the top of a code file after the package declaration.
<syntaxhighlight lang="java">
package org.wikipedia.examples;
import java.util.Random; // Single type declaration
public class ImportsTest {
public static void main(String[] args) {
/* The following line is equivalent to
- java.util.Random random = new java.util.Random();
- It would have been incorrect without the import.
- /
Random random = new Random();
}
}
</syntaxhighlight>
Import-on-demand declarations are mentioned in the code. A "wildcard import" (or "glob import") imports all the types of the package. A "static import" imports members of the package.
<syntaxhighlight lang="java">
/**
- This form of importing classes makes all classes
- in package java.util available by name, could be used instead of the
- import declaration in the previous example.
- /
import java.util.*;
</syntaxhighlight>
A wildcard import may possibly import nothing, if no symbols exist in that package. For example, statements like <syntaxhighlight lang="java" inline>import java.*;</syntaxhighlight> are legal, but do nothing, as all classes reside in packages within package <code>java</code>; such an import would not import all classes in the package recursively.
Static import declaration
This type of declaration has been available since J2SE 5.0. Static import declarations allow access to static members defined in another class, interface, annotation, or enum; without specifying the class name:
<syntaxhighlight lang="java">
import static java.lang.System.out; // 'out' is a static field in java.lang.System
public class HelloWorld {
public static void main(String[] args) {
/* The following line is equivalent to
System.out.println("Hi World!");
and would have been incorrect without the import declaration. */
out.println("Hello World!");
}
}
</syntaxhighlight>
Import-on-demand declarations allow to import all the fields of the type:
<syntaxhighlight lang="java">
/**
- This form of declaration makes all
- fields in the java.lang.System class available by name, and may be used instead
- of the import declaration in the previous example.
- /
import static java.lang.System.*;
</syntaxhighlight>
Enum constants may also be used with static import. For example, this enum is in the package called <code>screen</code>:
<syntaxhighlight lang="java">
public enum Colors {
RED, BLUE, GREEN
};
</syntaxhighlight>
It is possible to use static import declarations in another class to retrieve the enum constants:
<syntaxhighlight lang="java">
import org.wikipedia.screen.ColorName;
import static org.wikipedia.screen.ColorName.*;
public class Dots {
/* The following line is equivalent to 'ColorName foo = ColorName.RED',
and it would have been incorrect without the static import. */
ColorName foo = RED;
void shift() {
/* The following line is equivalent to
if (foo == ColorName.RED) foo = ColorName.BLUE; */
if (foo == RED) {
foo = BLUE;
}
}
}
</syntaxhighlight>
Module import declaration
Since Java 25, modules can be used in import statements to automatically import all the packages exported by the module. This is done using <code>import module</code>. For example, <syntaxhighlight lang="Java" inline>import module java.sql;</syntaxhighlight> is equivalent to
<syntaxhighlight lang="Java">
import java.sql.*;
import javax.sql.*;
// Remaining indirect exports from java.logging, java.transaction.xa, and java.xml
</syntaxhighlight>
Similarly, <syntaxhighlight lang="Java" inline>import module java.base;</syntaxhighlight>, similarly, imports all 54 packages belonging to <code>java.base</code>.
<syntaxhighlight lang="Java">
import module java.base;
/**
- importing module java.base allows us to avoid manually importing most classes
- the following classes (outside of java.lang) are used:
- java.text.MessageFormat
- java.util.Date
- java.util.List
- java.util.concurrent.ThreadLocalRandom
- /
public class Example {
public static void main(String[] args) {
List<String> colours = List.of("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet");
IO.println(MessageFormat.format("My favourite colour is {0} and today is {1,date,long}",
colours.get(ThreadLocalRandom.current().nextInt(colours.size())),
new Date()
));
}
}
</syntaxhighlight>
Operators
Operators in Java are similar to those in C++. However, there is no <code>delete</code> operator due to garbage collection mechanisms in Java, and there are no operations on pointers since Java does not support them. Another difference is that Java has an unsigned right shift operator (<code>>>></code>), while C's right shift operator's signedness is type-dependent. Operators in Java cannot be overloaded. The only overloaded operator is <code>operator+</code> for string concatenation.
{| class="wikitable"
|-
! style="text-align: center" | Precedence
! style="text-align: center" | Operator
! style="text-align: center" | Description
! style="text-align: center" | Associativity
|-
! rowspan=3| 1
| style="border-bottom-style: none; border-top-style: none" | <code>()</code>
| style="border-bottom-style: none; border-top-style: none" | Method invocation
| style="vertical-align: center" rowspan="4" | Left-to-right
|-
| style="border-bottom-style: none; border-top-style: none" | <code>[]</code>
| style="border-bottom-style: none; border-top-style: none" | Array access
|-
| style="border-bottom-style: none; border-top-style: none" | <code>.</code>
| style="border-bottom-style: none; border-top-style: none" | Class member selection
|-
! 2
| style="border-bottom-style: none" | <code>++</code> <code>--</code>
| style="border-bottom-style: none" | Postfix increment and decrement
|-
! rowspan=5| 3
| style="border-bottom-style: none" | <code>++</code> <code>--</code>
| style="border-bottom-style: none" | Prefix increment and decrement
| style="vertical-align: center" rowspan="5" | Right-to-left
|-
| style="border-bottom-style: none; border-top-style: none" | <code>+</code> <code>-</code>
| style="border-bottom-style: none; border-top-style: none" | Unary plus and minus
|-
| style="border-bottom-style: none; border-top-style: none" | <code>!</code> <code>~</code>
| style="border-bottom-style: none; border-top-style: none" | Logical NOT and bitwise NOT
|-
| style="border-bottom-style: none; border-top-style: none" | <code>(type) val</code>
| style="border-bottom-style: none; border-top-style: none" | Type cast
|-
| style="border-top-style: none" | <code>new</code>
| style="border-top-style: none" | Class instance or array creation
|-
! 4
| <code>*</code> <code>/</code> <code>%</code>
| Multiplication, division, and modulus (remainder)
| style="vertical-align: center" rowspan="13" | Left-to-right
|-
! rowspan=2| 5
| style="border-bottom-style: none;" | <code>+</code> <code>-</code>
| style="border-bottom-style: none;" | Addition and subtraction
|-
| style="border-top-style: none" | <code>+</code>
| style="border-top-style: none" | String concatenation
|-
! 6
| <code><<</code> <code>>></code> <code>>>></code>
| Bitwise left shift, signed right shift and unsigned right shift
|-
! rowspan=3| 7
| style="border-bottom-style: none;" | <code><</code> <code><=</code>
| style="border-bottom-style: none;" | Relational "less than" and "less than or equal to"
|-
| style="border-bottom-style: none; border-top-style: none" | <code>></code> <code>>=</code>
| style="border-bottom-style: none; border-top-style: none" | Relational "greater than" and "greater than or equal to"
|-
| style="border-top-style: none" | <code>instanceof</code>
| style="border-top-style: none" | Type comparison
|-
! 8
| <code>==</code> <code>!=</code>
| Relational "equal to" and "not equal to"
|-
! 9
| <code>&</code>
| Bitwise and logical AND
|-
! 10
| <code>^</code>
| Bitwise and logical XOR (exclusive or)
|-
! 11
| <code><nowiki>|</nowiki></code>
| Bitwise and logical OR (inclusive or)
|-
! 12
| <code>&&</code>
| Logical conditional-AND
|-
! 13
| <code><nowiki>||</nowiki></code>
| Logical conditional-OR
|-
! 14
| <code>c ? t : f</code>
| Ternary conditional (see ?:)
| style="vertical-align: center" rowspan="6" | Right-to-left
|-
! rowspan=5| 15
| style="border-bottom-style: none" | <code>=</code>
| style="border-bottom-style: none" | Simple assignment
|-
| style="border-bottom-style: none; border-top-style: none" | <code>+=</code> <code>-=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by sum and difference
|-
| style="border-bottom-style: none; border-top-style: none" | <code>*=</code> <code>/=</code> <code>%=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by product, quotient, and remainder
|-
| style="border-bottom-style: none; border-top-style: none" | <code><<=</code> <code>>>=</code> <code>>>>=</code>
| style="border-bottom-style: none; border-top-style: none" | Assignment by bitwise left shift, signed right shift and unsigned right shift
|-
| style="border-top-style: none" | <code>&=</code> <code>^=</code> <code><nowiki>|</nowiki>=</code>
| style="border-top-style: none" | Assignment by bitwise AND, XOR, and OR
|}
Control structures
Conditional statements
<code>if</code> statement
if statements in Java are similar to those in C and use the same syntax:
<syntaxhighlight lang="java">
if (i == 3) {
doSomething();
}
</syntaxhighlight>
<code>if</code> statement may include optional <code>else</code> block, in which case it becomes an if-then-else statement:
<syntaxhighlight lang="java">
if (i == 3) {
doSomething();
} else {
doSomethingElse();
}
</syntaxhighlight>
Like C, else-if construction does not involve any special keywords, it is formed as a sequence of separate if-then-else statements:
<syntaxhighlight lang="java">
if (i == 3) {
doSomething();
} else if (i == 2) {
doSomethingElse();
} else {
doSomethingDifferent();
}
</syntaxhighlight>
Also, a ?: operator can be used in place of simple if statement, for example
<syntaxhighlight lang="java">
int a = 1;
int b = 2;
int minVal = (a < b) ? a : b;
</syntaxhighlight>
<code>switch</code> statement
Switch statements in Java can use <code>byte</code>, <code>short</code>, <code>char</code>, and <code>int</code> (not <code>long</code>) primitive data types or their corresponding wrapper types. Starting with J2SE 5.0, it is possible to use enum types. Starting with Java SE 7, it is possible to use Strings. Other reference types cannot be used in <code>switch</code> statements.
Possible values are listed using <code>case</code> labels. These labels in Java may contain only constants (including enum constants and string constants). Execution will start after the label corresponding to the expression inside the brackets. An optional <code>default</code> label may be present to declare that the code following it will be executed if none of the case labels correspond to the expression.
Code for each label ends with the <code>break</code> keyword. It is possible to omit it causing the execution to proceed to the next label, however, a warning will usually be reported during compilation.
<syntaxhighlight lang="java">
switch (ch) {
case 'A':
doSomething(); // Triggered if ch == 'A'
break;
case 'B':
case 'C':
doSomethingElse(); // Triggered if ch == 'B' or ch == 'C'
break;
default:
doSomethingDifferent(); // Triggered in any other case
break;
}
</syntaxhighlight>
<code>switch</code> expressions
Since Java 14 it has become possible to use switch expressions, which use the new arrow syntax:
<syntaxhighlight lang="java">
enum Result {
GREAT,
FINE,
// more enum values
}
Result result = switch (ch) {
case 'A' -> Result.GREAT;
case 'B', 'C' -> Result.FINE;
default -> throw new Exception();
};
</syntaxhighlight>
Alternatively, there is a possibility to express the same with the <code>yield</code> statement, although it is recommended to prefer the arrow syntax because it avoids the problem of accidental fall throughs.
<syntaxhighlight lang="java">
Result result = switch (ch) {
case 'A':
yield Result.GREAT;
case 'B':
case 'C':
yield Result.FINE;
default:
throw new Exception();
};
</syntaxhighlight>
Iteration statements
Iteration statements are statements that are repeatedly executed when a given condition is evaluated as true. Since J2SE 5.0, Java has four forms of such statements. The condition must have type <code>boolean</code> or <code>java.lang.Boolean</code>. Java does not implicitly convert integers or class types to Boolean values.
For example, the following code is valid in C but results in a compilation error in Java.
<syntaxhighlight lang="c">
while (1) {
doSomething();
}
</syntaxhighlight>
<code>while</code> loop
In the <code>while</code> loop, the test is done before each iteration.
<syntaxhighlight lang="java">
while (i < 10) {
doSomething();
}
</syntaxhighlight>
<code>do</code>-<code>while</code> loop
In the <code>do</code>-<code>while</code> loop, the test is done after each iteration. Consequently, the code is always executed at least once.
<syntaxhighlight lang="java">
// doSomething() is called at least once
do {
doSomething();
} while (i < 10);
</syntaxhighlight>
<code>for</code> loop
<code>for</code> loops in Java include an initializer, a condition and a counter expression. It is possible to include several expressions of the same kind using comma as delimiter (except in the condition). However, unlike C, the comma is just a delimiter and not an operator.
<syntaxhighlight lang="java">
for (int i = 0; i < 10; i++) {
doSomething();
}
// A more complex loop using two variables
for (int i = 0, j = 9; i < 10; i++, j -= 3) {
doSomething();
}
</syntaxhighlight>
Like C, all three expressions are optional. The following loop never terminates:
<syntaxhighlight lang="java">
for (;;) {
doSomething();
}
</syntaxhighlight>
Foreach loop
Foreach loops have been available since J2SE 5.0. This type of loop uses built-in iterators over arrays and collections to return each item in the given collection. Every element is returned and reachable in the context of the code block. When the block is executed, the next item is returned until there are no items remaining. This for loop from Java was later added to C++11. Unlike C#, this kind of loop does not involve a special keyword, but instead uses a different notation style.
<syntaxhighlight lang="java">
for (int i : intArray) {
doSomething(i);
}
</syntaxhighlight>
Jump statements
Labels
Labels are given points in code used by <code>break</code> and <code>continue</code> statements. While <code>goto</code> is a reserved keyword in Java, it cannot be used to jump to specific points in code (in fact, it has no use at all).
<syntaxhighlight lang="java">
start:
someMethod();
</syntaxhighlight>
<code>break</code> statement
The <code>break</code> statement breaks out of the closest loop or <code>switch</code> statement. Execution continues in the statement after the terminated statement, if any.
<syntaxhighlight lang="java">
for (int i = 0; i < 10; i++) {
while (true) {
break;
}
// Will break to this point
}
</syntaxhighlight>
It is possible to break out of the outer loop using labels:
<syntaxhighlight lang="java">
outer:
for (int i = 0; i < 10; i++) {
while (true) {
break outer;
}
}
// Will break to this point
</syntaxhighlight>
<code>continue</code> statement
The <code>continue</code> statement discontinues the current iteration of the current control statement and begins the next iteration. The following <code>while</code> loop in the code below reads characters by calling <code>getChar()</code>, skipping the statements in the body of the loop if the characters are spaces:
<syntaxhighlight lang="java">
int ch;
while (ch == getChar()) {
if (ch == ' ') {
continue; // Skips the rest of the while-loop
}
// Rest of the while-loop, will not be reached if ch == ' '
doSomething();
}
</syntaxhighlight>
Labels can be specified in <code>continue</code> statements and <code>break</code> statements:
<syntaxhighlight lang="java">
outer:
for (String str : stringsArr) {
char[] strChars = str.toCharArray();
for (char ch : strChars) {
if (ch == ' ') {
/* Continues the outer cycle and the next
string is retrieved from stringsArr */
continue outer;
}
doSomething(ch);
}
}
</syntaxhighlight>
<code>return</code> statement
The <code>return</code> statement is used to end method execution and to return a value. A value returned by the method is written after the <code>return</code> keyword. If the method returns anything but <code>void</code>, it must use the <code>return</code> statement to return some value.
<syntaxhighlight lang="java">
void doSomething(boolean streamClosed) {
// If streamClosed is true, execution is stopped
if (streamClosed) {
return;
}
readFromStream();
}
int calculateSum(int a, int b) {
int result = a + b;
return result;
}
</syntaxhighlight>
<code>return</code> statement ends execution immediately, except for one case: if the statement is encountered within a <code>try</code> block and it is complemented by a <code>finally</code>, control is passed to the <code>finally</code> block.
<syntaxhighlight lang="java">
void doSomething(boolean streamClosed) {
try {
if (streamClosed) {
return;
}
readFromStream();
} finally {
// Will be called last even if readFromStream() was not called
freeResources();
}
}
</syntaxhighlight>
Exception handling statements
<code>try</code>-<code>catch</code>-<code>finally</code> statements
Exceptions are managed within <code>try</code>-<code>catch</code> blocks.
<syntaxhighlight lang="java">
try {
// Statements that may throw exceptions
methodThrowingExceptions();
} catch (Exception e) {
// Exception caught and handled here
reportException(e);
} finally {
// Statements always executed after the try/catch blocks
freeResources();
}
</syntaxhighlight>
The statements within the <code>try</code> block are executed, and if any of them throws an exception, execution of the block is discontinued and the exception is handled by the <code>catch</code> block. There may be multiple <code>catch</code> blocks, in which case the first block with an exception variable whose type matches the type of the thrown exception is executed.
Java SE 7 also introduced multi-catch clauses besides uni-catch clauses. This type of catch clauses allows Java to handle different types of exceptions in a single block provided they are not subclasses of each other. While it appears to resemble a union type, it is actually in fact the most specific common supertype of the alternatives.
<syntaxhighlight lang="java">
import java.net.HttpRetryException;
import java.net.http.HttpTimeoutException;
void makeRequest() throws HttpRetryException, HttpTimeoutException {
// ...
}
void main(String[] args) {
try {
methodThrowingExceptions();
} catch (HttpRetryException | HttpTimeoutException e) {
// Both HttpRetryException and HttpTimeoutException will be caught and handled here
// This in fact reduces to just java.io.IOException.
reportException(e);
}
}
</syntaxhighlight>
If no <code>catch</code> block matches the type of the thrown exception, the execution of the outer block (or method) containing the <code>try</code>-<code>catch</code> statement is discontinued, and the exception is passed up and outside the containing block (or method). The exception is propagated upwards through the call stack until a matching <code>catch</code> block is found within one of the currently active methods. If the exception propagates all the way up to the top-most <code>main</code> method without a matching <code>catch</code> block being found, a textual description of the exception is written to the standard output stream.
The statements within the <code>finally</code> block are always executed after the <code>try</code> and <code>catch</code> blocks, whether or not an exception was thrown and even if a <code>return</code> statement was reached. Such blocks are useful for providing cleanup code that is guaranteed to always be executed.
The <code>catch</code> and <code>finally</code> blocks are optional, but at least one or the other must be present following the <code>try</code> block.
<code>try</code>-with-resources statements
<code>try</code>-with-resources statements are a special type of <code>try</code>-<code>catch</code>-<code>finally</code> statements introduced as an implementation of the dispose pattern in Java SE 7. In a <code>try</code>-with-resources statement the <code>try</code> keyword is followed by initialization of one or more resources that are released automatically when the <code>try</code> block execution is finished. Resources must implement <code>java.lang.AutoCloseable</code>. <code>try</code>-with-resources statements are not required to have a <code>catch</code> or <code>finally</code> block unlike normal <code>try</code>-<code>catch</code>-<code>finally</code> statements.
<syntaxhighlight lang="java">
import java.beans.XMLEncoder;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
try (FileOutputStream fos = new FileOutputStream("filename");
XMLEncoder xEnc = new XMLEncoder(fos)) {
xEnc.writeObject(object);
} catch (IOException e) {
Logger.getLogger(Serializer.class.getName()).log(Level.SEVERE, null, e);
}
</syntaxhighlight>
Since Java 9 it is possible to use already declared variables:
<syntaxhighlight lang="java">
import java.beans.XMLEncoder;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
FileOutputStream fos = new FileOutputStream("filename");
XMLEncoder xEnc = new XMLEncoder(fos);
try (fos; xEnc) {
xEnc.writeObject(object);
} catch (IOException e) {
Logger.getLogger(Serializer.class.getName()).log(Level.SEVERE, null, e);
}
</syntaxhighlight>
<code>throw</code> statement
The <code>throw</code> statement is used to throw an exception and end the execution of the block or method. The thrown exception instance is written after the <code>throw</code> statement.
<syntaxhighlight lang="java">
void methodThrowingExceptions(Object obj) {
if (obj == null) {
// Throws exception of NullPointerException type
throw new NullPointerException();
}
// Will not be called, if object is null
doSomethingWithObject(obj);
}
</syntaxhighlight>
<code>assert</code> statement
<code>assert</code> statements have been available since J2SE 1.4. These types of statements are used to make assertions in the source code, which can be turned on and off during execution for specific classes or packages. To declare an assertion the <code>assert</code> keyword is used followed by a conditional expression. If it evaluates to <code>false</code> when the statement is executed, an exception is thrown. This statement can include a colon followed by another expression, which will act as the exception's detail message.
<syntaxhighlight lang="java">
// If n equals 0, AssertionError is thrown
assert n != 0;
/* If n equals 0, AssertionError will be thrown
with the message after the colon */
assert n != 0 : "n was equal to zero";
</syntaxhighlight>
Threading
Java offers threads of execution through the class <code>java.lang.Thread</code>. This class takes a <code>java.lang.Runnable</code> (such as a lambda), and are started using the <code>start()</code> method, and paused/blocked using <code>sleep()</code>. For notifying other threads, <code>wait()</code> releases the object lock and pauses the thread until another thread calls <code>notify()</code> or <code>notifyAll()</code>.
<syntaxhighlight lang="java">
Thread t = new Thread(() -> {
System.out.println("Running");
});
t.start();
</syntaxhighlight>
Daemon threads are threads which run in the background, for example garbage collectors or monitoring threads. The JVM exits when only daemon threads remain.
The current thread may be accessed with the <code>currentThread()</code> method.
Historically, an error <code>java.lang.ThreadDeath</code> would be thrown upon calling <code>stop()</code> on a thread, however <code>stop()</code> was removed since Java 20, and this error is no longer thrown. The idiomatic way to stop a thread now is with the <code>interrupt()</code> method, where a thread is allowed to finish gracefully rather than immediately stopped without cleanup.
Thread concurrency control
Java has built-in tools for multi-thread programming. For the purposes of thread synchronization the <code>synchronized</code> statement is included in Java language.
To make a code block synchronized, it is preceded by the <code>synchronized</code> keyword followed by the lock object inside the brackets. When the executing thread reaches the synchronized block, it acquires a mutual exclusion lock, executes the block, then releases the lock. No threads may enter this block until the lock is released. Any non-null reference type may be used as the lock.
<syntaxhighlight lang="java">
/* Acquires lock on someObject. It must be of
a reference type and must be non-null */
synchronized (someObject) {
// Synchronized statements
}
</syntaxhighlight>
Concurrency features
Java offers the library <code>java.lang.concurrent</code>, as well as atomicity features (<code>java.lang.concurrent.atomic</code>) and locking features (<code>java.lang.concurrent.locks</code>).
Rather than <code>java.lang.Thread</code>, another class <code>java.lang.concurrent.CompletableFuture<T></code> is provided for asynchronous computation; this may be seen as similar to a <code>System.Threading.Tasks.Task<T></code> in C#, even though Java lacks explicit coroutines like those of C#.
As part of Project Loom in Java 21, virtual threads were introduced, which allow for normal, blocking, synchronous code while scaling to millions of tasks. The classic Java thread is called a "platform thread", corresponding roughly to an OS thread, but Java virtual threads are lightweight and managed by the JVM, scheduled onto a small pool of real OS threads. Virtual threads are still <code>java.lang.Thread</code>, but are created with factory methods like <code>ofVirtual()</code>. Although the code is still synchronous and blocking, the physical OS threads are asynchronous; upon stopping to wait, the JVM detaches the code from the physical thread, running other tasks until the OS sends an asynchronous event notification to pick up the code where it left off to finish.
Primitive types
Primitive types in Java include integer types, floating-point numbers, UTF-16 code units and a Boolean type. Unlike C++ and C#, there are no unsigned types in Java except <code>char</code> type, which is used to represent UTF-16 code units. The lack of unsigned types is offset by introducing unsigned right shift operation (<code>>>></code>), which is not present in C++, methods such as <code>.toUnsignedInt()</code>. Nevertheless, criticisms have been leveled about the lack of compatibility with C and C++ this causes. <!-- This is a terrible citation. If you have a better one, use it. -->
{| class="wikitable"
|-
!colspan="6"|Primitive types
|-
! Type name
! Class Library equivalent
! Value
! Range
! Size
! Default value
|-
|
|
| integer
| −32,768 through +32,767
| 16-bit (2-byte)
| <code>0</code>
|-
|
|
| integer
| −2,147,483,648 through +2,147,483,647
| 32-bit (4-byte)
| <code>0</code>
|-
|
|
| integer
| −9,223,372,036,854,775,808 through<br/> +9,223,372,036,854,775,807
| 64-bit (8-byte)
| <code>0</code>
|-
|
|
| integer
| -128 through 127
| 8-bit (1-byte)
| <code>0</code>
|-
|
|
| floating point number
| ±1.401298E−45 through ±3.402823E+38
| 32-bit (4-byte)
| <code>0.0</code>
|-
|
|
| floating point number
| ±4.94065645841246E−324 through<br/> ±1.79769313486232E+308
| 64-bit (8-byte)
| <code>0.0</code>
|-
|
|
| Boolean
| or
| 8-bit (1-byte)
|
|-
|
|
| single Unicode character
| through
| 16-bit (2-byte)
|
|-
|
|
| N/A
| N/A
| N/A
| N/A
|}
<code>null</code> has no type, neither primitive nor class. Any object type may store <code>null</code>.
The class <code>Void</code> is used to hold a reference to the <code>Class</code> object representing the keyword <code>void</code>. It cannot be instantiated as <code>void</code> cannot be the type of any object. For example, <code>CompletableFuture<Void></code> signifies that a <code>java.util.concurrent.CompletableFuture<T></code> performs a task that does not return a value.
The class <code>java.lang.Number</code> represents all numeric values which can be converted to <code>byte</code>, <code>double</code>, <code>float</code>, <code>int</code>, <code>long</code>, and <code>short</code>.
<code>char</code> does not necessarily correspond to a single character. It may represent a part of a surrogate pair, in which case Unicode code point is represented by a sequence of two <code>char</code> values.
Boxing and unboxing
This language feature was introduced in J2SE 5.0. Boxing is the operation of converting a value of a primitive type into a value of a corresponding reference type, which serves as a wrapper for this particular primitive type. Unboxing is the reverse operation of converting a value of a reference type (previously boxed) into a value of a corresponding primitive type. Neither operation requires an explicit conversion.
Example:
<syntaxhighlight lang="java">
int foo = 42; // Primitive type
Integer bar = foo; /* foo is boxed to bar, bar is of Integer type,
which serves as a wrapper for int */
int foo2 = bar; // Unboxed back to primitive type
</syntaxhighlight>
Reference types
Reference types include class types, interface types, and array types. When the constructor is called, an object is created on the heap and a reference is assigned to the variable. When a variable of an object gets out of scope, the reference is broken and when there are no references left, the object gets marked as garbage. The garbage collector then collects and destroys it some time afterwards.
A reference variable is <code>null</code> when it does not reference any object.
Arrays
Arrays in Java are created at runtime, just like class instances. Array length is defined at creation and cannot be changed.
<syntaxhighlight lang="java">
int[] numbers = new int[5];
numbers[0] = 2;
numbers[1] = 5;
int x = numbers[0];
int len = numbers.length; // len = 5
</syntaxhighlight>
C-style declarations of arrays are also accepted in Java, but are not commonly used.
<syntaxhighlight lang="java">
int arr[] = new int[10];
</syntaxhighlight>
Arrays are represented in the JVM with an opening left squared bracket for every dimension of the array, with the following letters:
- , for <code>byte</code>
- , for <code>char</code>
- , for <code>double</code>
- , for <code>float</code>
- , for <code>int</code>
- , for <code>long</code>
- , for <code>short</code>
- , for <code>boolean</code>
- , for any reference (non-primitive) type, followed by the fully qualified class name and then a closing semicolon
For example, <code>int[][]</code> becomes <code>[[I</code> and <code>String[]</code> becomes <code>[Ljava.lang.String;</code>.
Arrays are limited to a maximum of 255 dimensions. The theoretical maximum length of an array is <math>2^{31} - 1</math> (2,147,483,647, equivalent to <code>Integer.MAX_VALUE</code>), because arrays can only be addressed with 32-bit indices, but this limit is typically system and virtual machine dependent.
The classes <code>java.util.Arrays</code> and <code>java.lang.reflect.Array</code> are provided for array manipulation and reflective operations on arrays, respectively.
Arrays appear to have a field <code>length</code>, but it is not a real field; it is a built-in property evaluated natively by the <code>arraylength</code> bytecode instruction. Despite this, in the JVM bytecode, a constructor appears under the name <code><init></code> and is called an "instance initialization method", and are marked <code>void</code> (and therefore do not push any resulting object onto the JVM's operand stack upon completing execution).
Initializers are blocks of code that are executed when a class or an instance of a class is created. There are two kinds of initializers, static initializers and instance initializers.
Static initializers initialize static fields when the class is created. They are declared using the <code>static</code> keyword:
<syntaxhighlight lang="java">
class Foo {
static {
// Initialization
}
}
</syntaxhighlight>
A class is created only once. Therefore, static initializers are not called more than once. On the contrary, instance initializers are automatically called before the call to a constructor every time an instance of the class is created. Unlike constructors instance initializers cannot take any arguments and generally they cannot throw any checked exceptions (except in several special cases). Instance initializers are declared in a block without any keywords:
<syntaxhighlight lang="java">
class Foo {
{
// Initialization
}
}
</syntaxhighlight>
Since Java relies on a garbage collection mechanism, there are no destructors <code>~X()</code> or <code>delete</code> operator. However, every object has a <code>finalize()</code> method called prior to garbage collection, which can be overridden to implement finalization. However, there are no guarantees which thread will invoke the <code>finalize()</code> method for any given object, or even that it will be invoked at all. Finalizers are non-deterministic (unlike destructors in C++), and releasing critical resources in a <code>finalize()</code> call may lead to unpredictable behavior. Another class, <code>java.lang.ref.Cleaner</code>, introduced in Java 9, is additionally provided for lower-level safety nets for reclaiming non-memory resources, such as native pointers, file descriptors, or sockets.
Methods
All the statements in Java must reside within methods. Methods are similar to functions except they belong to classes. A method has a return value, a name and usually some parameters initialized when it is called with some arguments. Similar to C++, methods returning nothing have return type declared as <code>void</code>. Unlike in C++, methods in Java are not allowed to have default argument values and methods are usually overloaded instead.
<syntaxhighlight lang="java">
class Foo {
int bar(int a, int b) {
return (a * 2) + b;
}
// Overloaded method with the same name but different set of arguments
int bar(int a) {
return a * 2;
}
}
</syntaxhighlight>
A method is called using <code>.</code> notation on an object, or in the case of a static method, also on the name of a class.
<syntaxhighlight lang="java">
Foo foo = new Foo();
int result = foo.bar(7, 2); // Non-static method is called on foo
int finalResult = Math.abs(result); // Static method call
</syntaxhighlight>
The <code>throws</code> keyword indicates that a method throws an exception. All checked exceptions must be listed in a comma-separated list.
<syntaxhighlight lang="java">
import java.io.File;
import java.io.IOException;
import java.util.zip.DataFormatException;
// Indicates that IOException and DataFormatException may be thrown
void operateOnFile(File f) throws IOException, DataFormatException {
// ...
}
</syntaxhighlight>
Modifiers
- <code>abstract</code> - Abstract methods can be present only in abstract classes, such methods have no body and must be overridden in a subclass unless it is abstract itself.
- <code>static</code> - Makes the method static and accessible without creation of a class instance. However static methods cannot access non-static members in the same class.
- <code>final</code> - Declares that the method cannot be overridden in a subclass.
- <code>native</code> - Indicates that this method is implemented through JNI in platform-dependent code. Actual implementation happens outside Java code, and such methods have no body.
- <code>strictfp</code> - Declares strict conformance to IEEE 754 in carrying out floating-point operations. Now obsolete.
- <code>synchronized</code> - Declares that a thread executing this method must acquire monitor. For <code>synchronized</code> methods the monitor is the class instance or <code>java.lang.Class</code> if the method is static.
- Access modifiers - Identical to those used with classes.
Final methods
Variadic parameters
This language feature was introduced in J2SE 5.0. The last argument of the method may be declared as a variable arity parameter, in which case the method becomes a variable arity method (as opposed to fixed arity methods) or simply variadic method. This allows one to pass a (possibly zero) variable number of values, of the declared type, to the method as parameters. These values will be available inside the method as an array.
<syntaxhighlight lang="java">
// numbers represents varargs
void printReport(String header, int... numbers) {
// numbers appears as int[]
System.out.println(header);
for (int num : numbers) {
System.out.println(num);
}
}
// Calling varargs method
printReport("Report data", 74, 83, 25, 96);
</syntaxhighlight>
Fields
Fields, or class variables, can be declared inside the class body to store data.
<syntaxhighlight lang="java">
class Foo {
double bar;
}
</syntaxhighlight>
Fields can be initialized directly when declared.
<syntaxhighlight lang="java">
class Foo {
double bar = 2.3;
}
</syntaxhighlight>
Modifiers
- <code>static</code> - Makes the field a static member.
- <code>final</code> - Allows the field to be initialized only once in a constructor or inside initialization block or during its declaration, whichever is earlier.
- <code>transient</code> - Indicates that this field will not be stored during serialization.
- <code>volatile</code> - If a field is declared <code>volatile</code>, it is ensured that all threads see a consistent value for the variable.
Inheritance
Classes in Java can only inherit from one class. A class can be derived from any class that is not marked as <code>final</code>. Inheritance is declared using the <code>extends</code> keyword. A class can reference itself using the <code>this</code> keyword and its direct superclass using the <code>super</code> keyword.
<syntaxhighlight lang="java">
class Foo {
}
class Foobar extends Foo {
}
</syntaxhighlight>
If a class does not specify its superclass, it implicitly inherits from <code>java.lang.Object</code> class. Thus all classes in Java are subclasses of <code>Object</code> class.
If the superclass does not have a constructor without parameters the subclass must specify in its constructors what constructor of the superclass to use. For example:
<syntaxhighlight lang="java">
class Foo {
public Foo(int n) {
// Do something with n
}
}
class Foobar extends Foo {
private int number;
// Superclass does not have constructor without parameters
// so we have to specify what constructor of our superclass to use and how
public Foobar(int number) {
super(number);
this.number = number;
}
}
</syntaxhighlight>
Overriding methods
Unlike C++, all non-<code>final</code> methods in Java are virtual and can be overridden by the inheriting classes.
<syntaxhighlight lang="java">
class Operation {
public int doSomething() {
return 0;
}
}
class NewOperation extends Operation {
@Override
public int doSomething() {
return 1;
}
}
</syntaxhighlight>
Abstract classes
An abstract class is a class that is incomplete, or is to be considered incomplete, and cannot be instantiated (directly).
A class <code>X</code> has abstract methods if any of the following is true:
- <code>X</code> explicitly contains a declaration of an abstract method.
- Any of <code>X</code>'s superclasses in the inheritance chain have an abstract method and <code>X</code> neither declares nor inherits a method that implements it.
- A direct superinterface of <code>X</code> declares or inherits a method (which is therefore necessarily abstract) and <code>X</code> neither declares nor inherits a method that implements it.
- A subclass of an abstract class that is not itself abstract may be instantiated, resulting in the execution of a constructor for the abstract class and, therefore, the execution of the field initializers for instance variables of that class.
<syntaxhighlight lang="java">
package org.wikipedia.examples;
public class MyAbstractClass {
private static final String hello;
static {
System.out.printf("%s: static block runtime%n", MyAbstractClass.class.getName());
hello = String.format("hello from %s", MyAbstractClass.class.getName());
}
{
System.out.printf("%s: instance block runtime%n", MyAbstractClass.class.getName());
}
public AbstractClass() {
System.out.printf("%s: constructor runtime%n", MyAbstractClass.class.getName());
}
public static void hello() {
System.out.println(hello);
}
}
</syntaxhighlight>
<syntaxhighlight lang="java">
package org.wikipedia.examples;
public class MyCustomClass extends MyAbstractClass {
static {
System.out.printf("%s: static block runtime%n", MyCustomClass.class.getName());
}
{
System.out.printf("%s: instance block runtime%n", MyCustomClass.class.getName());
}
public CustomClass() {
System.out.printf("%s: constructor runtime%n", MyCustomClass.class.getName());
}
public static void main(String[] args) {
MyCustomClass nc = new MyCustomClass();
hello();
MyAbstractClass.hello(); // also valid
}
}
</syntaxhighlight>
Output:
<syntaxhighlight lang="text">
org.wikipedia.examples.MyAbstractClass: static block runtime
org.wikipedia.examples.MyCustomClass: static block runtime
org.wikipedia.examples.MyAbstractClass: instance block runtime
org.wikipedia.examples.MyAbstractClass: constructor runtime
org.wikipedia.examples.MyCustomClass: instance block runtime
org.wikipedia.examples.MyCustomClass: constructor runtime
hello from org.wikipedia.examples.MyAbstractClass
</syntaxhighlight>
Enumerations
This language feature was introduced in J2SE 5.0. Technically enumerations are a kind of class containing enum constants in its body. Each enum constant defines an instance of the enum type. Enumeration classes cannot be instantiated anywhere except in the enumeration class itself.
<syntaxhighlight lang="java">
enum Season {
WINTER,
SPRING,
SUMMER,
AUTUMN,
}
</syntaxhighlight>
Enum constants are allowed to have constructors, which are called when the class is loaded:
<syntaxhighlight lang="java">
public enum Season {
WINTER("Cold"),
SPRING("Warmer"),
SUMMER("Hot"),
AUTUMN("Cooler");
Season(String description) {
this.description = description;
}
private final String description;
public String getDescription() {
return description;
}
}
</syntaxhighlight>
Enumerations can have class bodies, in which case they are treated like anonymous classes extending the enum class:
<syntaxhighlight lang="java">
public enum Season {
WINTER {
@Override
String getDescription() {
return "cold"
}
},
SPRING {
@Override
String getDescription() {
return "warmer";
}
},
SUMMER {
@Override
String getDescription() {
return "hot";
}
},
FALL {
@Override
String getDescription() {
return "cooler";
}
};
abstract String getDescription();
}
</syntaxhighlight>
Interfaces
Interfaces are types which contain no fields and usually define a number of methods without an actual implementation. They are useful to define a contract with any number of different implementations. Every interface is implicitly abstract. Interface methods are allowed to have a subset of access modifiers depending on the language version, <code>strictfp</code>, which has the same effect as for classes, and also <code>static</code> since Java SE 8.
<syntaxhighlight lang="java">
interface ActionListener {
int ACTION_ADD = 0;
int ACTION_REMOVE = 1;
void actionSelected(int action);
}
</syntaxhighlight>
Implementing an interface
An interface is implemented by a class using the <code>implements</code> keyword. It is allowed to implement more than one interface, in which case they are written after <code>implements</code> keyword in a comma-separated list. A class implementing an interface must override all its methods, otherwise it must be declared as abstract.
<syntaxhighlight lang="java">
interface RequestListener {
int requestReceived();
}
class ActionHandler implements ActionListener, RequestListener {
public void actionSelected(int action) {
// ...
}
public int requestReceived() {
// ...
}
}
// Calling method defined by interface
// ActionHandler can be represented as RequestListener...
RequestListener listener = new ActionHandler();
// ...and thus is known to implement requestReceived() method
listener.requestReceived();
</syntaxhighlight>
Functional interfaces and lambda expressions
These features were introduced with the release of Java SE 8. An interface automatically becomes a functional interface if it defines only one method (excluding those which are inherited from <code>java.lang.Object</code>). In this case an implementation can be represented as a lambda expression instead of implementing it in a new class, thus greatly simplifying writing code in the functional style. Functional interfaces can optionally be annotated with the <code>java.lang.FunctionalInterface</code> annotation, which will tell the compiler to check whether the interface actually conforms to a definition of a functional interface.
<syntaxhighlight lang="java">
// A functional interface
@FunctionalInterface
interface Calculation {
int calculate(int someNumber, int someOtherNumber);
}
// A method which accepts this interface as a parameter
int runCalculation(Calculation calculation) {
return calculation.calculate(1, 2);
}
// Using a lambda to call the method
runCalculation((number, otherNumber) -> number + otherNumber);
// Equivalent code which uses an anonymous class instead
runCalculation(new Calculation() {
@Override
public int calculate(int someNumber, int someOtherNumber) {
return someNumber + someOtherNumber;
}
})
</syntaxhighlight>
The parameter types of a lambda do not have to be fully specified and can be inferred from the interface it implements, but there is no place to explicitly specify the lambda's return type (which is inferred). The body of the lambda can be written without a body block and a <code>return</code> statement if it is only an expression. Also, for those interfaces which only have a single parameter in the method, round brackets can be omitted.
<syntaxhighlight lang="java">
// Same call as above, but with fully specified types and a body block
runCalculation((int number, int otherNumber) -> {
return number + otherNumber;
});
// A functional interface with a method which has only a single parameter
interface StringExtender {
String extendString(String input);
}
// Initializing a variable of this type by using a lambda
StringExtender extender = input -> input + " Extended";
</syntaxhighlight>
Lambdas cannot specify <code>throws</code> clauses. Lambdas implement some target functional interface (such as <code>java.lang.Runnable</code>, etc.). Lambdas themselves are not complete expressions of concrete types, but rather poly expressions, whose type are influenced by their surrounding target type rather than entirely by their own content.
<syntaxhighlight lang="java">
import java.util.function.Function;
// Invalid, as lambda expressions themselves have no standalone type
var f = (int x) -> x + 1;
// Should instead be:
Function<Integer, Integer> f = x -> x + 1;
</syntaxhighlight>
If a lambda implements a functional interface which extends <code>java.lang.Serializable</code>, then when the lambda is serialized, it becomes a <code>java.lang.invoke.SerializedLambda</code>, capturing information about the lambda itself, called its "serialized form". This is necessary, because lambda implementation classes are synthetic and JVM-specific, and must therefore be reconstructed exactly in another JVM invocation. Much like in C++, lambda type names are synthetic and compiler-generated with unspecified implementation details.
<syntaxhighlight lang="java">
Runnable r = () -> System.out.println("Hello");
// Could print something like:
// class org.wikipedia.examples.Main$$Lambda$1/0x0000000800b00440
System.out.println(r.getClass());
</syntaxhighlight>
Lambdas do not generate class files, but rather emit an <code>invokedynamic</code> instruction on the JVM, which the runtime uses to create a suitable implementation class.
Capturing lambdas are typically distinct objects, due to requiring stored captured state, however non-capturing lambdas may potentially be singleton.
<syntaxhighlight lang="java">
Runnable r1 = () -> {};
Runnable r2 = () -> {};
System.out.println(r1 == r2); // May print true or false; determined by the JVM
</syntaxhighlight>
However, unlike C++, lambda types are not part of the language model; nothing about its runtime class may be depended upon, whereas in C++ the lambda type may still be extracted with <code>decltype</code> to obtain a real unique closure type; a lambda's <code>java.lang.Class<?></code> cannot be referenced in code.
Method references
Java allows method references using the operator <code>::</code> (it is not related to the C++ namespace qualifying operator <code>::</code>). It is not necessary to use lambdas when there already is a named method compatible with the interface. This method can be passed instead of a lambda using a method reference. There are several types of method references:
{| class="wikitable"
|-
! Reference type !! Example !! Equivalent lambda
|-
| Static || <syntaxhighlight lang="java" inline>Integer::sum</syntaxhighlight> || <syntaxhighlight lang="java" inline>(m, n) -> m + n</syntaxhighlight>
|-
| Bound || <syntaxhighlight lang="java" inline>"LongString"::substring</syntaxhighlight> || <syntaxhighlight lang="java" inline>i -> "LongString".substring(i)</syntaxhighlight>
|-
| Unbound || <syntaxhighlight lang="java" inline>String::isEmpty</syntaxhighlight> || <syntaxhighlight lang="java" inline>s -> s.isEmpty()</syntaxhighlight>
|-
| Class constructor || <syntaxhighlight lang="java" inline>ArrayList<String>::new</syntaxhighlight> || <syntaxhighlight lang="java" inline>len -> new ArrayList<String>(len)</syntaxhighlight>
|-
| Array constructor || <syntaxhighlight lang="java" inline>String[]::new</syntaxhighlight> || <syntaxhighlight lang="java" inline>len -> new String[len]</syntaxhighlight>
|}
The code above which calls <code>runCalculation</code> could be replaced with the following using the method references:
<syntaxhighlight lang="java>
runCalculation(Integer::sum);
</syntaxhighlight>
Note that in Java, constructors are not considered methods, and thus <code>X::X</code> does not exist; a constructor reference is retrieved with <code>X::new</code>.
Inheritance
Interfaces can inherit from other interfaces just like classes. Unlike classes it is allowed to inherit from multiple interfaces. However, it is possible that several interfaces have a field with the same name, in which case it becomes a single ambiguous member, which cannot be accessed.
<syntaxhighlight lang="java">
/* Class implementing this interface must implement methods of both
ActionListener and RequestListener */
interface EventListener extends ActionListener, RequestListener {
}
</syntaxhighlight>
Default methods
Java SE 8 introduced default methods to interfaces which allows developers to add new methods to existing interfaces without breaking compatibility with the classes already implementing the interface. Unlike regular interface methods, default methods have a body which will get called in the case if the implementing class does not override it.
<syntaxhighlight lang="java">
interface StringManipulator {
String extendString(String input);
// A method which is optional to implement
default String shortenString(String input) {
return input.substring(1);
}
}
// This is a valid class despite not implementing all the methods
class PartialStringManipulator implements StringManipulator {
@Override
public String extendString(String input) {
return String.format("%s Extended", input);
}
}
</syntaxhighlight>
Static methods
Static methods is another language feature introduced in Java SE 8. They behave in exactly the same way as in the classes.
<syntaxhighlight lang="java">
interface StringUtils {
static String shortenByOneSymbol(String input) {
return input.substring(1);
}
}
StringUtils.shortenByOneSymbol("Test");
</syntaxhighlight>
Private methods
Private methods were added in the Java 9 release. An interface can have a method with a body marked as private, in which case it will not be visible to inheriting classes. It can be called from default methods for the purposes of code reuse.
<syntaxhighlight lang="java">
interface Logger {
default void logError() {
log(Level.ERROR);
}
default void logInfo() {
log(Level.INFO);
}
private void log(Level level) {
SystemLogger.log(level.id);
}
}
</syntaxhighlight>
Annotations
Annotations in Java are a way to embed metadata into code. This language feature was introduced in J2SE 5.0.
Annotation types
Java has a set of predefined annotation types, but it is allowed to define new ones. An annotation type declaration is a special type of an interface declaration. They are declared in the same way as the interfaces, except the <code>interface</code> keyword is preceded by the <code>@</code> sign. Java annotations are interfaces and are implicitly extended from <code>java.lang.annotation.Annotation</code> and cannot be extended from anything else. Unlike C++ annotations, which may be any object, Java annotations cannot be instantiated into a real runtime object.
<syntaxhighlight lang="java">
@interface BlockingOperations {
}
</syntaxhighlight>
Annotations may have the same declarations in the body as the common interfaces, in addition they are allowed to include enums and annotations. The main difference is that abstract method declarations must not have any parameters or throw any exceptions. Also they may have a default value, which is declared using the <code>default</code> keyword after the method name:
<syntaxhighlight lang="java">
@interface BlockingOperations {
boolean fileSystemOperations();
boolean networkOperations() default false;
}
</syntaxhighlight>
Usage of annotations
Annotations may be used in any kind of declaration, whether it is package, class (including enums), interface (including annotations), field, method, parameter, constructor, or local variable. Also they can be used with enum constants. Annotations are declared using the <code>@</code> sign preceding annotation type name, after which element-value pairs are written inside brackets. All elements with no default value must be assigned a value.
<syntaxhighlight lang="java">
@BlockingOperations(
fileSystemOperations, // mandatory
networkOperations = true // optional
)
void openOutputStream() {
// annotated method
}
</syntaxhighlight>
Besides the generic form, there are two other forms to declare an annotation, which are shorthands. Marker annotation is a short form, it is used when no values are assigned to elements:
<syntaxhighlight lang="java">
@Unused // Shorthand for @Unused()
void travelToJupiter() {
}
</syntaxhighlight>
The other short form is called single element annotation. It is used with annotations types containing only one element or in the case when multiple elements are present, but only one elements lacks a default value. In single element annotation form the element name is omitted and only value is written instead:
<syntaxhighlight lang="java">
/*
Equivalent for @BlockingOperations(fileSystemOperations = true).
networkOperations has a default value and
does not have to be assigned a value
- /
@BlockingOperations(true)
void openOutputStream() {
}
</syntaxhighlight>
Generics
Generic programming (generics), or parameterized types, or parametric polymorphism, is one of the major features introduced in J2SE 5.0. Before generics were introduced, it was required to declare all the types explicitly. With generics, it became possible to work in a similar manner with different types without declaring the exact types. The main purpose of generics is to ensure type safety and to detect runtime errors during compilation. Unlike C++ templates, generics do not instantiate a specialization for each type, and unlike C#, generics are not reified, and information on the used parameters is not available at runtime due to type erasure (while Kotlin, another JVM language, offers reified generics, it achieves this by inlining the type at the call site). This is because historically, Java lacked generics entirely; thus generics are erased at runtime to allow a generic class <code>X<T></code> to be compatible with <code>X</code>.
For instance, before generics were introduced, the Java collections framework stored everything as <code>java.lang.Object</code>.
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
List names = new ArrayList();
names.add("Alice");
names.add("Bob");
String first = (String)names.get(0); // first = "Alice"
names.add(1); // Allowed
</syntaxhighlight>
At runtime, generics appear as the lower bound of the type parameter (if no lower bound is specified, this is implicitly <code>java.lang.Object</code>). Because generics are erased, it is not possible to construct a generic type (such as <syntaxhighlight lang="java" inline>new T()</syntaxhighlight>) or check types against a specialization (such as <syntaxhighlight lang="java" inline>if (names instanceof List<String>)</syntaxhighlight>).
Also, generic classes may not extend <code>java.lang.Throwable</code>. This is because a generic exception <code>FooException<T></code> would erase <code>FooException<String></code> and <code>FooException<Integer></code> to the same type at runtime, making the two indistinguishable at runtime. However, a generic type may be thrown (as long as it is bounded below by <code>java.lang.Throwable</code>). For example, the following pattern, sometimes known as the generic rethrow or sneaky throw, is used to bypass checked exceptions entirely, such as in the Project Lombok <code>@SneakyThrows</code> attribute.
<syntaxhighlight lang="java">
static <T extends Throwable> void sneakyThrow(Throwable t) throws T {
throw (T)t;
}
</syntaxhighlight>
This erases <code>T</code> down to <code>java.lang.Throwable</code>, bypassing the checked exception mechanism (which enforces only on types which do not descend from <code>java.lang.Error</code> or <code>java.lang.RuntimeException</code>) entirely.
Generic classes
Classes can be parameterized by adding a type variable inside angle brackets (<code><</code> and <code>></code>) following the class name. It makes possible the use of this type variable in class members instead of actual types. There can be more than one type variable, in which case they are declared in a comma-separated list.
It is possible to limit a type variable to a subtype of some specific class or declare an interface that must be implemented by the type. In this case the type variable is appended by the <code>extends</code> keyword followed by a name of the class or the interface. If the variable is constrained by both class and interface or if there are several interfaces, the class name is written first, followed by interface names with <code>&</code> sign used as the delimiter (in a way which resembles an intersection type).
<syntaxhighlight lang="java">
/* This class has two type variables, T and V. T must be
a subtype of ArrayList and implement Formattable interface */
public class Mapper<T extends ArrayList & Formattable, V> {
public void add(T array, V item) {
// array has add method because it is an ArrayList subclass
array.add(item);
}
}
</syntaxhighlight>
When a variable of a parameterized type is declared or an instance is created, its type is written exactly in the same format as in the class header, except the actual type is written in the place of the type variable declaration.
<syntaxhighlight lang="java">
/* Mapper is created with CustomList as T and Integer as V.
CustomList must be a subclass of ArrayList and implement Formattable */
Mapper<CustomList, Integer> mapper = new Mapper<CustomList, Integer>();
</syntaxhighlight>
Since Java SE 7, it is possible to use a diamond (<code><></code>) in place of type arguments, in which case the latter will be inferred. This is sometimes called the "diamond operator", even though it is not actually an operator, but rather an empty type list. The following code in Java SE 7 is equivalent to the code in the previous example:
<syntaxhighlight lang="java">
Mapper<CustomList, Integer> mapper = new Mapper<>();
</syntaxhighlight>
When declaring a variable for a parameterized type, it is possible to use wildcards instead of explicit type names. Wildcards are expressed by writing <code>?</code> sign instead of the actual type. It is possible to limit possible types to the subclasses or superclasses of some specific class by writing the <code>extends</code> keyword or the <code>super</code> keyword correspondingly followed by the class name.
<syntaxhighlight lang="java">
/* Any Mapper instance with CustomList as the first parameter
may be used regardless of the second one.*/
Mapper<CustomList, ?> mapper;
mapper = new Mapper<CustomList, Boolean>();
mapper = new Mapper<CustomList, Integer>();
/* Will not accept types that use anything but
a subclass of Number as the second parameter */
void addMapper(Mapper<?, ? extends Number> mapper) {
}
</syntaxhighlight>
Generic methods and constructors
Usage of generics may be limited to some particular methods, this concept applies to constructors as well. To declare a parameterized method, type variables are written before the return type of the method in the same format as for the generic classes. In the case of constructor, type variables are declared before the constructor name.
<syntaxhighlight lang="java">
class Mapper {
// The class is not generic, the constructor is
<T, V> Mapper(T array, V item) {}
}
/* This method will accept only arrays of the same type as
the searched item type or its subtype */
static <T, V extends T> boolean contains(T item, V[] arr) {
for (T currentItem : arr) {
if (item.equals(currentItem)) {
return true;
}
}
return false;
}
</syntaxhighlight>
Generic interfaces
Interfaces can be parameterized in the similar manner as the classes.
<syntaxhighlight lang="java">
interface Expandable<T extends Number> {
void addItem(T item);
}
// This class is parameterized
class MyArray<T extends Number> implements Expandable<T> {
void addItem(T item) {
}
}
// And this is not and uses an explicit type instead
class IntegerArray implements Expandable<Integer> {
void addItem(Integer item) {
}
}
</syntaxhighlight>
See also
- Java Platform, Standard Edition
- C# syntax
- C++ syntax
- C syntax
- JavaScript syntax
Notes
References
External links
- The Java Language Specification, Third edition Authoritative description of the Java language
<!-- Hidden categories below -->
