Introduction
A .java file holds source code in Java, the language behind the Java
platform, whose reference implementation is OpenJDK.
The javac compiler turns each class and interface in it into a
.class file of bytecode for the Java
Virtual Machine. Since Java 11 a program in a single file can also be run directly with
java Hello.java, and since Java 22 a program spread over several files.
Programs and libraries are usually shipped as
.jar archives of the compiled classes
rather than as source.
A .java file dropped on any page of this site opens in the explorer as
text, with its compilation unit, package and imports; every class and interface with
its modifiers, supertypes and members; its module directives, annotations with their
element values, comments and documentation tags; every literal with its value and
type; and, on a Code tab, every statement, local variable, lambda, switch, pattern and
constant value. It checks the rules a single file decides, such as definite
assignment and unreachable code, the types of expressions over primitives,
String, Object and the file’s own classes, names and members
of the file’s own classes, and checked exceptions where every exception type is known.
Types from other files, the class library and inference need the class path, so it
does not work those out and says nothing about them.
The pages listed below are a reference to the language, chapter by chapter. They follow The Java Language Specification, Java SE 27 Edition (August 2026), the edition for JDK 27, and give the JLS section for each topic. Features that are still in preview in Java 27 are marked as preview.
Structure
A Java source file is plain text made of Unicode characters; since Java 18 the compiler reads it as UTF-8 unless told otherwise. The language calls one file a compilation unit, and there are three kinds:
| Kind | Contents | Usual file name |
|---|---|---|
| Ordinary | An optional package declaration, then import declarations, then any number of top-level classes, interfaces, enums, records and annotation interfaces | Named after its public class: Hello.java |
| Modular | import declarations, then one module declaration | module-info.java |
| Compact (Java 25) | import declarations, then fields, methods and nested types with no class around them; at least one method, normally main | Any name; the file name becomes the class name |
In an ordinary file only one top-level type may be public, and
javac requires the file to be named after it: public class
Hello lives in Hello.java. The folders normally follow the
package name, so com.example.Hello is in
com/example/Hello.java. Annotations on a package go in a
package-info.java file in the package’s folder.
package com.example;
import java.util.List;
public class Hello {
public static void main(String[] args) {
List<String> names = List.of("world", "Java");
for (String name : names) {
System.out.println("Hello, " + name);
}
}
}
com/example/Hello.javavoid main() {
IO.println("Hello, world");
}
Hello.java, run with java Hello.java (Java 25 and later)The File structure page covers the encoding, line endings, file names and each kind of compilation unit in full.
Language reference
The reference is split into these pages, in roughly the order of the specification’s chapters.
- File structure – encoding, line terminators, Unicode escapes, file names, the three kinds of compilation unit,
module-info.javaandpackage-info.java - Lexical structure – white space, comments, identifiers, every keyword, every kind of literal including text blocks, separators and operators
- Types, values and variables – the eight primitive types, reference types,
null, kinds of variable,final, effectively final andvar - Conversions and contexts – widening, narrowing, boxing and the rest, and where each is allowed
- Names and scope – declarations, scope, shadowing, hiding, obscuring and access control
- Packages and modules – package declarations, every kind of import, and module declarations
- Classes – class declarations, fields, methods, constructors, initialisers, nested, inner, local and anonymous classes, and sealed classes
- Interfaces – abstract, default, static and private methods, constants, functional and sealed interfaces
- Enum classes – constants, constant bodies, constructors and the implicit methods
- Record classes – components, accessors, canonical and compact constructors
- Annotations – declaring and using annotations, the predefined ones, targets and type annotations
- Generics – type parameters, bounds, wildcards, erasure, raw types and inference
- Arrays – array types, creation, initialisers, access and covariance
- Exceptions – checked and unchecked exceptions,
throws,try, multi-catch and try-with-resources - Statements – blocks, local declarations and every statement, with labels, loops and jumps
- Switch – switch statements and switch expressions, both label forms,
yield, patterns and exhaustiveness - Expressions and operators – the precedence table, every operator, evaluation order and constant expressions
- Lambdas and method references – lambda syntax, functional interfaces, capture and the four kinds of method reference
- Pattern matching – type patterns, record patterns, the unnamed pattern, guards, dominance and exhaustiveness
- Definite assignment – the rules that make sure every variable is set before use and finals are set once
- Execution and initialisation – start-up and
main, class loading and initialisation, and object creation - Binary compatibility – what a class file must contain, and which changes keep compiled code linking
- Threads and the memory model –
synchronized,waitandnotify,volatile, happens-before andfinalfields - Type inference – inference variables, constraints and bounds; reduction, incorporation and resolution
- Syntax – every production of the Java grammar, as in chapter 19 of the specification
- Versions – every release from JDK 1.0 in 1996 to Java 27 in 2026, with the language features each added and the changes that broke old source
Not described
Viewer: out of scope. One file cannot be checked against other files
or the class library (a call of a library method, a type variable of another file,
var from such a call), and the viewer does not try. Type inference over
the types the file declares is implemented in part, see below. It attributes types
only where the file decides them: literals, locals, parameters and fields of a
primitive type, String, Object, the boxes or a type declared
in the file, and calls of the file’s own methods and constructors. A type it does not
know propagates as unknown, and it reports nothing on it.
Viewer: out of scope: Java preview features. Primitive types in
patterns and in instanceof, and boolean, long,
float and double as switch selectors, are the preview
language feature of Java 27 (JEP 532, fifth preview), not part of the standard
language until finalised. When it meets one the viewer says it is a preview feature
and reports no other error on it.
Viewer: partly inferred: the type arguments of an anonymous class
created with a diamond (new H<>() { … }). The viewer takes them from
the target type when the creation is the whole initialiser of a field or local, the
whole right side of an =, or the value of a return in a
method that has no type parameters, and the target is a parameterised type of the
file (or Comparable or Iterable) with no wildcard and a
one-to-one mapping to the class’s own parameters. It also infers them for the argument
of the one method or constructor of the file that takes that many arguments (no type
parameters, a parameter type without type variables), for each branch of a
conditional that is the whole value, and inside parentheses. Where several methods take
that many arguments it infers only when every other method certainly cannot take the
creation at that place (a primitive, an array, a final class of java.lang
or an unrelated class of the file as the parameter type). Everywhere else (a
wildcard or raw target, var, a lambda body, an argument where several
methods or a generic method could apply, an unknown type) it compares no method
signature of that class against the generic supertype and says nothing, which cannot
give a false error.
Viewer: partly compared: members inherited through a chain of generic
supertypes of the file. The type arguments are composed along every
extends and implements (H<U> extends
B<U> seen as H<String> gives B<String>),
for the abstract-method, override, return-type, throws, name-clash and
lambda and method-reference checks. A raw, wildcard or unknown link, or two different
answers, leaves the comparison silent. Type arguments nested inside a parameter type
(List<V> against List<String>) are compared where
every part is known, so @Override on put(String, List<String>)
against an inherited put(K, List<V>) with V = Integer is
reported; a wildcard, a type variable of a method or an unknown type leaves the pair
alone, and the name-clash rules compare erased forms, as the language defines them.
Viewer: partly typed: the type arguments of expressions. A parameterised
type of the file (Box<String>, Out<String>.In,
Out.In<String>) keeps its arguments. A field, a method result, a
parameter or a thrown type of a member is seen through the receiver’s arguments, composed
along the supertype chain, so int x = val in a class that extends
B<String>, Integer i = c.get() on a
C<String> and an unreported throws E are reported. Two
parameterisations are the same type only with the same arguments (4.10.2): assignment, a
call argument and a cast between A<String> and
A<Integer> are reported, a cast only where the arguments are provably
distinct (5.5.1). A raw type gives the erasure of a member (String s = raw.get()
is an error), and a raw implements I must implement the erased method. A
type variable of an in-file bound is checked for members and conversions, explicit
type arguments against their bounds (also bounds that are other variables) and against
the parameters.
Viewer: partly implemented: wildcard capture (5.1.10) and type inference
(chapter 18) for the types the file declares and the small JDK table. A member of a
wildcard-parameterised type is read through its capture: each wildcard becomes a fresh
type variable with the wildcard’s bound and the parameter’s declared bound as upper
bounds and the ? super bound as lower bound, so
String s = box.get() on a Box<?>,
box.set(1) on a Box<? extends Number> and
Integer i = box.get() on a Box<? super Integer> are
reported, and box.set(5) on a Box<? super Integer> is
not. Generic method invocations and diamonds are inferred as the JLS states: inference
variables, reduction (18.2), incorporation (18.3), resolution (18.4), applicability
(18.5.1) and the target type of an assignment, return or argument (18.5.2.1), nested
generic calls sharing one bound set (18.5.2.2), and lambdas with implicit parameters
(18.2.1). Reported: incompatible bounds (two(Box<String>, Box<Integer>)
for Box<T> twice, a bound T extends Number against a
String), an inferred type that does not fit the target
(String s = id(1), Box<String> b = new Box<>(1)), a lambda
whose result fits no instantiation. Also done: the type of a record accessor
(r.b() on a Pair<A, B>, also through
var r = new Pair<>(…)); the upward projection of a var
whose initialiser mentions capture variables (4.10.5, var l = b.items() on a
B<? extends Number> is an L<? extends Number>); the
three phases of overload resolution with variable arity candidates and the most specific
method (15.12.2.2 to 15.12.2.5), with an ambiguity error where it is certain, and
generic candidates dropped when the arguments that are not lambdas or method references
certainly rule them out; the least upper bound (4.10.4) wherever the common supertypes
reduce to one class or interface, with the least containing type argument of
parameterised ones; lambdas with explicit parameter types (18.2.1), block bodies with
several returns (every result is constrained, and a void function type
against a value return, or the reverse, is reported); method references as arguments,
exact and inexact, T[]::new and constructor references (18.2.1, 15.13.1) for
a qualifier that is a class of the file; and the unchecked conversion of 18.5.2.1: a raw
argument for a parameterised parameter makes the invocation’s type the erasure of the
instantiated return type. Also done: a generic call used as a statement is inferred
without a target, and the body of every lambda argument is read with the instantiated
parameter types (so a wrong member inside it is reported) when nothing in the inference
had to be skipped; String and the boxes are Comparable of
themselves, so max("a", 1) for T extends Comparable<T>
is reported; a variable bounded by an F-bound takes its proper upper bounds as the
candidate when only the target determines it; lambdas are checked for potential
compatibility among overloads of one arity (parameter count, void or value expression
body, a parameter type that is no functional interface), and a call that no generic
candidate can take is reported; an explicitly typed lambda against a
wildcard-parameterised function type uses the ground type of 18.5.3;
super::m is read like a method reference on the superclass type. Also
done: the most specific of several applicable generic candidates (15.12.2.5 with the
more-specific test of 18.5.4: the other method is applied to this one’s parameter types,
its own variables standing as variables with their bounds), for arguments that are
ordinary expressions of known reference types, with ref.ambiguous where
every pair is decided and none is more specific; method references whose qualifier is a
written parameterisation (Box<String>::get, Box<String>::new)
or the raw name of a generic class of the file (Box::get searches the
parameterisation that is a supertype of the first parameter, after capture, 15.13.1),
and referenced methods of variable arity (15.12.2.4); two unrelated classes among the
upper bounds of one inferred variable are an error (4.9, 18.4). Also done: overloads
whose arguments are lambdas or method references (15.12.2.5): an implicitly typed lambda
or an inexact method reference is not pertinent to applicability, an explicitly typed
lambda (also one with no parameters) or an exact reference is read against the function
type, and the more specific interface wins by the functional interface rule (the
same parameter types, and a void, subtype, primitive-against-reference or
reference-against-primitive result by the bullets of 15.12.2.5), with
ref.ambiguous where it is certain; the chosen method then types the
lambda body. Intersection types are values (A & B, from the least upper
bound of classes of the file with several minimal supertypes, and from the greatest lower
bound of upper bounds): a subtype of each part, members and fields of the part that has
them, so pick(new C(), new D()).b() is typed. Generic referenced methods
(P::id, p::conv, P::pself): their inference
variables join the call’s bound set (15.13.1, 18.2.1), and a reference to which no method
fits is reported when neither search of 15.13.1 finds an applicable method. A capture
variable is kept as the instantiation when it is the one equal or lower bound
(Integer i = map(b, Box::get) on a Box<? extends Number> is
reported). Also done: a return type with a wildcard (Box<? extends T> w(T))
against a target uses fresh variables for the captured wildcards (18.5.2.1, 18.3.2: such a
variable equals no other type, and a bound on it gives a constraint on the wildcard’s bound),
so Box<? extends String> b = w(1) and String s = first(bs) on a
Box<? super Integer> are reported (a class and a capture variable that
are not subtypes of each other cannot both bound one variable, 5.1.10); a lambda parameter keeps a
capture variable in its type; a block lambda is value-compatible only where it cannot
complete normally (15.27.2, 14.22), which decides overloads such as
m(x -> { println(); }) and a body that only throws; when every overload is
certainly inapplicable to a lambda or reference the call is reported; a cast of an
intersection type to a type that one part cannot be cast to is reported; method references
to a constructor of a generic class named raw (Box::new, read like a diamond), to a
generic constructor, with explicit type arguments (P::<String>id), to a
generic method of variable arity and to a generic method beside non-generic ones that
cannot take the argument (15.13.1); generic and non-generic overloads whose lambda
arguments are implicitly typed or inexact references (the functional rule of 18.5.4 is
false for them); Comparable<K> of a class of the file in a bound
such as T extends Comparable<T>. Also done: the more-specific test of
18.5.4 for an explicitly typed lambda or an exact method reference against a generic candidate
(the result expressions against the candidate’s own variable, which appears nowhere else
and has no bound or bounds that are plain types: each result type must fit the bounds, a void
result is more specific than a variable, a primitive result is
more specific than a variable where every result is primitive, a reference result
must be within the variable’s bounds); the most specific test of generic candidates that
are all of variable arity (15.12.2.5: the first n variable arity parameter types
compared, m(T...) against m(T, T...) is ambiguous); every type
argument within the bounds of its parameter (JLS 4.5, 4.10, 5.1.10; javac
not.within.bounds) where the argument or the bound is a type variable or a parameterised
type of the file: Q<U> for Q<T extends Number>,
Q<B> for Q<T extends Comparable<T>>,
Pair<A, B extends Pair<B, A>>,
Node<Gen<Gen<Q>>> for Node<N extends Node<N>>,
in field, parameter, return, throws and local variable types, casts, instanceof
and patterns, new, extends and implements, the bounds of type
parameters, record components, explicitly typed lambda parameters and the explicit type
arguments of calls; wildcard arguments are checked the way javac does (? extends X:
the bound must be castable to X; ? super X: X a subtype of the bound; a type variable as
X for the cases that follow from the glb of 5.1.10); Comparable found
through the superclasses and interfaces of the file, also through a generic class of the file
(class B extends A, A implements Comparable<A>, so
max(new B(), new B()) is reported); a library class against a type of the file as
a bound; a reference conditional and a switch expression as poly expressions (each result
checked against the parameter type); primitive arguments and parameters of generic candidates
(the strict phase rules them out, 18.5.1; both phases for the most specific test);
two variable-arity methods that are each more specific than the other are ambiguous;
generic variable-arity candidates with primitive arguments, an array of primitives or a
candidate with one more parameter (m(1, 2) and m("a") against
m(T...) and m(T, T...): the extra subtype constraint of 18.5.4);
the phases of 15.12.2 told apart for a null or an array argument (the strict and loose
phases read a variable-arity candidate with its array type, so m(null) against
m(Integer...) and m(String...), or m(int...) and
m(long...), is ambiguous, and an int[] meets int... but
no T...), with fixed-arity and variable-arity candidates compared together; a
bounded variable of a generic candidate in several parameters (a lambda’s result and a plain
parameter, two function types, T x, T y, Box<T>,
T[], a bound that is another variable of the method): the constraints of all
arguments meet in the variable (equalities from the declared types of explicit lambdas and
the arguments of class types, lower bounds from plain arguments and lambda results) and are
resolved against its bounds, for applicability and for the more-specific test, an implicitly
typed lambda adding no constraint; the bounds of a generic candidate that have the variable
inside and are not Comparable (T extends Box<T>,
Comparable<? super T>, Box<U>) in the more-specific test;
Comparable<R> and R[] as parameter types of a function type;
Enum<E> as the JLS states it (8.9: an enum class E extends Enum<E>, nothing
else extends Enum) in 4.5 (Foo<String> for T extends Enum<T>,
T extends Enum<U> with an unbounded U, ? extends String, a raw
Enum bound, explicit type arguments); an enclosing instance with a ? super
argument (the lower bound; a wildcard replaces the outer variable in the bound as javac
substitutes it); a bounded variable of a generic candidate against an explicitly typed lambda or an exact
method reference when the bound has type arguments or the variable itself in it
(T extends Comparable<T>, also with another variable of the method in the
bound), and when the variable is also a parameter type of the function type, bare or inside
the type arguments of a class of the file (the lambda’s declared type is equal to it, 18.2.1);
a type argument of an enclosing instance type within its bounds
(Out<String>.In<X>, with the outer arguments replacing the outer
variables in the bound and a wildcard outer argument as a capture variable); a raw type as the
argument of a bound that is parameterised (a raw type is no subtype of a parameterised one,
4.10.2); and a
lambda or method reference returned by a lambda (18.2.1, 15.27.3), against a functional
interface that is a return type or an inference variable resolved first. Not done,
and silent (the call’s type is then unknown): a library type anywhere in the signature
or the arguments (the JDK table has no List or Stream, no
Constable, and no members of String or Integer, so a lambda body that calls one,
or a least upper bound of two library classes such as that of String and
Integer, cannot be typed; only Comparable of String and
the boxes has its argument), the most
specific method (15.12.2.5) of generic candidates where a variable has several lower bounds
(two lambda results, or a lambda result and an argument of different types) and its bound has
the variable inside but is not Comparable<T> (that needs the least upper
bound of 4.10.4 of the lower bounds); a function type whose parameter or return type is a
library type with the candidate’s variable inside, R a(List<R> x),
R a(Class<R> x) or R a(Enum<R> x): the table keeps no type
arguments of a library type (only Comparable<X>, which the JLS states, is
read), so a candidate with such a function type is not compared, although the equality of
the arguments alone would decide it; a type argument within its bounds where the bound or the
argument is a library type other than Enum, Comparable, the boxes,
String, Number and Object (List,
Thread.State: whether such a class is an enum or a subtype of the bound needs the
JDK); an intersection type with a library type as a part; a
resolution that fails the first approach of 18.4 where the second approach (the fresh
type variables Y) is not modelled: it is reported only for the captured wildcards of a return
type and where the incorporation already fails; a method reference whose qualifier is a
library type; and class bounds that mention a type variable.
Because of these gaps the viewer is not claimed to check generic types
completely, and it stays partial.
Viewer: partly checked: whether a switch over a generic sealed type
covers it. A permitted subtype that cannot be a subtype of the selector’s type (for
non-sealed interface Q<X> extends S<List<X>> against
S<String>) is not required, at any depth and inside record patterns;
a wildcard, raw or unknown argument keeps it required, so a missing case may go
unreported but a covered switch is never reported.
Links
Software
- OpenJDK – the reference Java platform, with the
javaccompiler andjavaruntime (Windows, macOS, Linux) - IntelliJ IDEA – JetBrains’ development environment for Java and Kotlin (Windows, macOS, Linux)
- Eclipse – free development environment for Java, with its own compiler (Windows, macOS, Linux)
- Apache NetBeans – free development environment for Java (Windows, macOS, Linux)
- Visual Studio Code – free code editor with Java extensions (Windows, macOS, Linux)
Developers
- JavaParser – parses Java source into a syntax tree (Java)
- tree-sitter-java – Java grammar for the Tree-sitter parser used by editors
Reference
- The Java Language Specification, Java SE 27 Edition – the official description of the language, which these pages follow
- Java Language and Virtual Machine Specifications – every edition of the JLS and the JVM specification, from Java SE 6
- JEP 0, the JEP index – the JDK Enhancement Proposals that describe each language change since Java 8