Summary: in this tutorial, you’ll learn about the basic Java syntax including whitespace, statements, blocks, identifiers, keywords, and comments.
Introduction to Java syntax
Java syntax is a set of rules that you need to follow to write a Java program. They include whitespace, statements, blocks, identifiers, keywords, and comments.
Whitespace
Whitespace are the characters that do not have visible output:
- Carriage return
- Space
- New Line
- Tab
Typically, you use whitespace to format the code to make it readable. Note that the Java compiler ignores whitespace.
For example, the Java compiler will treat the following programs the same even though the first program doesn’t use the new line:
public class App {public static void main(String[] args){System.out.println("Hello, World!");}}
Code language: Java (java)
And:
public class App {
public static void main(String[] args){
System.out.println("Hello, World!");
}
}
Code language: Java (java)
It’s good practice to use whitespace to format the code to make it more readable.
Statements
A statement is an instruction that tells the Java compiler what to do. A statement forms a complete command to be executed and may include one or more expressions.
Java has three main kinds of statements:
Declaration statements that declare variables and initialize them. For example:
int age = 1;
Code language: Java (java)
Expression statements that evaluate an expression and return a value. For example, the following increases the age variable by one and returns it:
age++;
Control flow statements control which statements get executed. For example, the following uses the if statement to show a message if the age is greater than 19:
if(age > 18) {
System.out.println("You are eligible to join the program.");
}
Code language: Java (java)
Note that you’ll learn more about variables and control flow statements in the upcoming tutorials.
Blocks
A block is a group of zero or more statements. A block starts with an opening curly brace { and ends with a closing curly brace }. Java allows you to use a block can be anywhere a single statement is allowed.
For example, the following illustrates a block that contains two statements:
if (isBirthday) {
age++;
System.out.println(age);
}
Code language: Java (java)
Identifiers
Identifiers are used to name elements in a program such as variables, classes, and methods. Java identifiers are case-sensitive. It means that age and Age identifiers are not the same.
For naming identifiers, you follow these rules:
- Identifiers must begin with a letter (A-Z or a-z), an underscore (_), or a dollar sign ($).
- After the initial character, an identifier can contain letters, digits (0-9), underscores, or dollar signs.
- Identifiers cannot be the same as Java’s keywords. More on the Java keywords shortly.
By convention, identifiers should follow the camelCase
or PascalCase
for readability.
camelCase
starts with a lowercase letter and capitalizes the first letter of each subsequent word e.g.,myAge
.PascalCase
capitalizes the first letter of each word without spaces e.g.,SalesPerson
.
Keywords
Keywords in Java are reserved words with predefined meanings. The following table shows the keywords in Java:
abstract | continue | for | new |
switch | assert | default | goto |
package | synchronized | boolean | do |
if | private | this | break |
double | implements | protected | throw |
byte | else | import | public |
throws | case | enum | instanceof |
return | transient | catch | extends |
int | short | try | char |
final | interface | static | void |
class | finally | long | strictfp |
volatile | const | float | native |
super | while |
Comments
Sometimes, you may want to add explanatory notes within your code. To do that, you use comments. When the Java compiler encounters comments, it ignores them. It means that the comments don’t affect the program’s logic. They are purely for human readability and documentation purposes.
Java supports three types of comments:
Single-Line Comments: Single-line comments start with two forward slashes //
and continue until the end of the line. It means the Java compiler treats anything that follows //
on the same line as a comment and will not execute it.
For example:
// This is a single-line comment
int x = 5; // You can also place comments at the end of a line of code
Code language: Java (java)
Multi-Line Comments: Multi-line comment begins with /*
and end with */
. It can span multiple lines. Multi-line comments are also known as block comments.
In practice, you use multi-line comments for longer explanations or to temporarily disable a code block. For example:
/* This is a multi-line comment.
It can span multiple lines, and everything
between the opening and closing delimiters is commented out. */
int y = 10; /* You can also use multi-line comments for a single line of code. */
Code language: Java (java)
Documentation Comments: Documentation comments are special comments that are used to generate Java documentation automatically via documentation generation tools like JavaDoc.
Documentation comments begin with /**
and end with */
. These comments are often placed before classes, methods, or fields and can include information about the code.
For example:
/**
* The App class
*/
public class App {
/**
* The main entry of the program
* @param args arguments passed to the program
*/
public static void main(String[] args){
System.out.println("Hello, World!");
}
}
Code language: Java (java)
Summary
- Use whitespace to format the code to make it readable.
- Use statements to instruct the Java compiler what to do.
- Use blocks to group one or more statements.
- Use identifiers to name variables, classes, methods, etc. Identifiers follow certain rules and conventions. Do not use keywords for naming identifiers.
- Use comments to provide documentation to code.