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:
Since Java 25, a simplified Hello World program without an explicit class may be written:
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 _). An identifier cannot:
Start with a digit. Be equal to a reserved keyword, null literal or Boolean literal. Since Java 22, the identifier of just an underscore (_) acts as an unnamed placeholder; it can be reused in multiple declarations in the same scope (like int _ = nextValue();), and suppresses compiler warnings of no use (equivalent to the @SuppressWarnings("unused") annotation); trying to read and write to _ later is ill-formed. In pattern matching, it suppresses unused fields.
Keywords
Keywords The following words are keywords and cannot be used as identifiers under any circumstances, of which there are 47.
Reserved identifiers The following words are contextual keywords and are only restricted in certain contexts, of which there are 16.
Reserved words for literal values The following words refer to literal values used by the language, of which there are 3.
Unused The following words are reserved as keywords, but currently have no use or purpose, of which there are 4.
Literals
Integer literals are of int type by default unless long type is specified by appending L or l suffix to the literal, e.g. 367L. Since Java SE 7, it is possible to include underscores between the digits of a number to increase readability; for example, a number 145608987 can be written as 145_608_987.
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.
Multiple variables of the same type can be declared and initialized in one statement using comma as a delimiter.
Type inference
Since Java 10, it has become possible to infer types for the variables automatically by using var.
However, unlike C++, the return type of a function cannot be inferred.
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:
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 /* and end with */, they may span across multiple lines. This type of comment was derived from C and C++.
End-of-line comments start with // and extend to the end of the current line. This comment type is also present in C++ and in modern C.
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 /** 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.
Universal types Classes in the package java.lang (but not its subpackages) are implicitly imported into every program, as long as no explicitly-imported types have the same names. Important ones include:
java.lang.System java.lang.System 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.
java.lang.Object java.lang.Object 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 java.lang.Object). All values can be converted to this type, although for primitive values this involves autoboxing.
java.lang.Record All records implicitly extend java.lang.Record. Thus, a record, while treated as a class, cannot extend any other class.
java.lang.Enum<E> All enumerated types (enums) in Java implicitly extend java.lang.Enum. Thus, an enum, while treated as a class, cannot extend any other class. The type parameter E is constrained as Enum<E extends Enum<E>>.
java.lang.Class<T> java.lang.Class is a class that represents a class or interface 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 .class file. A class literal can be obtained through X.class on such a class X. For instance, String.class returns Class<String>. An unknown class being modeled can be represented as Class<?> (where ? denotes a wildcard). While X.class appears to be accessing a field named "class", there is actually no such field named "class"; it is in fact performing the instruction ldc, which pushes a constant from the class's runtime constant pool into the operand stack.
… excerpt ends here. Continue reading the full article.



