ArticleslgStudy

science

Java syntax

Java syntax is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Java syntax rather than just read about it. In short: 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++.

Java syntax — main illustration
Java syntax — illustration

Key takeaways

  • Java syntax belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Java syntax to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Java syntax from memory before moving on to harder problems.

Reference excerpt

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.

Illustrations

Java syntax: A snippet of Java code.
A snippet of Java code.
Java syntax illustration
Java syntax: This image describes the class member scope within classes and packages.
This image describes the class member scope within classes and packages.

Worked examples

Example 1 — a first encounter with Java syntax

Start with the simplest possible case. Write down what Java syntax claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Java syntax before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Java syntax ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Java syntax

In research
Java syntax appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Java syntax in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Java syntax is common in secondary-school and first-year university syllabi. It links to neighbouring topics Java (programming language), Programming language syntax, so understanding it makes those chapters shorter.
In everyday life
Look for Java syntax outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Java syntax” →

How to study Java syntax in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Java syntax means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Java syntax out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Java syntax in simple terms?

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++.

Why does Java syntax matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Java syntax?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Java syntax.

Tags

  • Java (programming language)
  • Programming language syntax

Keep exploring