Java Basic Syntax
A Java program can be thought of as a collection of objects, and these objects work together by calling each other's methods. Below is a brief introduction to the concepts of classes, objects, methods, and instance variables.
- object: An object is an instance of a class, with state and behavior. For example, a dog is an object; its state includes color, name, and breed; its behaviors include wagging its tail, barking, and eating.
- class: A class is a template that describes the behavior and state of a class of objects.
- Method: A method is behavior; a class can have many methods. Logical operations, data modification, and all actions are completed in methods.
- Instance Variables: Each object has unique instance variables, and the object's state is determined by the values of these instance variables.
First Java Program
Below is a simple Java program that will output the string Hello World
Example
public
class
HelloWorld
{
/*
First Java Program * It will output the string Hello World
*/
public
static
void
main
(
String
[
]
args
)
{
System
.
out
.
println
(
"
Hello World
"
)
;
//
Output Hello World
}
}
Run Example »

The following will introduce step by step how to save, compile, and run this program:
- Open a code editor and add the code above into it;
- Save the file name as: HelloWorld.java;
- Open the cmd command window, navigate to the location of the target file, assuming it is C:\
- Type javac HelloWorld.java in the command line window and press Enter to compile the code. If there are no errors, the cmd prompt will move to the next line (assuming the environment variables are all set).
- Then type java HelloWorld and press Enter to run the program
You will see Hello World in the window
$ javac HelloWorld.java
$ java HelloWorld
Hello World
If you encounter encoding problems, we can use the -encoding option to set utf-8 to compile:
javac -encoding UTF-8 HelloWorld.java
java HelloWorld
GIF demonstration:
Basic Syntax
When writing a Java program, you should pay attention to the following points:
- Case Sensitive: Java is case-sensitive, which means the identifiers Hello and hello are different.
- Class Names: For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, for example MyFirstJavaClass .
- Method Names: All method names should start with a lowercase letter. If a method name contains several words, the first letter of each subsequent word is capitalized.
- Source File Names: The source file name must be the same as the class name. When saving the file, you should use the class name as the file name (remember that Java is case-sensitive), and the file name suffix is .java. (If the file name and class name are not the same, it will cause a compilation error).
- Main Method Entry: All Java programs start executing from public static void main(String[] args) method.
Java Identifiers
All components of Java need names. Class names, variable names, and method names are all called identifiers.
Regarding Java identifiers, the following points need attention:
- All identifiers should start with a letter (A-Z or a-z), a dollar sign ($), or an underscore (_)
- After the first character, it can be any combination of letters (A-Z or a-z), dollar sign ($), underscore (_), or digits
- Keywords cannot be used as identifiers
- Identifiers are case-sensitive
- Examples of valid identifiers: age, $salary, _value, __1_value
- Examples of invalid identifiers: 123abc, -salary
Java Modifiers
Like other languages, Java can use modifiers to modify methods and properties in a class. There are mainly two categories of modifiers:
- Access control modifiers: default, public, protected, private
- Non-access control modifiers: final, abstract, static, synchronized
In later chapters we will discuss Java modifiers in depth.
Java Variables
The main types of variables in Java are as follows
- local variables
- Class variables (static variables)
- Member variables (non-static variables)
Java Array
An array is an object stored on the heap that can hold multiple variables of the same type. In later chapters, we will learn how to declare, construct, and initialize an array.
Java Enumeration
Java 5.0 introduced enums, which restrict a variable to only predefined values. Using enums can reduce bugs in the code.
For example, we design a program for a juice shop that restricts juice to small, medium, and large cups. This means it does not allow customers to order juice in sizes other than these three.
Example
class
FreshJuice
{
enum
FreshJuiceSize
{
SMALL
,
MEDIUM
,
LARGE
}
FreshJuiceSize
size
;
}
public
class
FreshJuiceTest
{
public
static
void
main
(
String
[
]
args
)
{
FreshJuice
juice
=
new
FreshJuice
(
)
;
juice
.
size
=
FreshJuice
.
FreshJuiceSize
.
MEDIUM
;
}
}
**Note:** An enum can be declared separately or declared inside a class. Methods, variables, and constructors can also be defined in an enum.
Java Keywords
The Java keywords are listed below. These reserved words cannot be used for constants, variables, or any identifier names.
| Category | The keyword | Description |
|---|---|---|
| Access Control | private | private |
| protected | protected | |
| public | public | |
| default | Default | |
| Class, Method, and Variable Modifiers | abstract | Declare abstract |
| class | class | |
| extends | Extend, inherit | |
| final | Final value, unchangeable | |
| implements | Implement (interface) | |
| interface | an interface | |
| native | Native method (non-Java implementation) | |
| new | creation | |
| static | static | |
| strictfp | Strict floating point, precise floating point | |
| synchronized | Thread, synchronization | |
| transient | transient | |
| volatile | volatile | |
| Program Control Statements | break | Break out of loop |
| case | Define a value for switch selection | |
| continue | continue | |
| do | Run | |
| else | else | |
| for | Loop | |
| if | If both the | |
| instanceof | Example | |
| return | Return | |
| switch | Select execution based on value | |
| while | Loop | |
| Error handling | assert | Assert whether an expression is true |
| catch | Catch exception | |
| finally | Execute regardless of exception | |
| throw | Throw an exception object | |
| throws | Declare an exception may be thrown | |
| try | Catching an Exception | |
| Package Related | import | Introduced in |
| package | package | |
| Basic types | boolean | Boolean |
| byte | byte type | |
| char | Character | |
| double | double precision floating point | |
| float | single precision floating point | |
| int | Integer | |
| long | Long integer | |
| short | Short integer | |
| variable reference | super | parent class, superclass |
| this | this class | |
| void | no return value | |
| Reserved Keywords | goto | is a keyword but cannot be used |
| const | is a keyword but cannot be used |
**Note:** Java's null is not a keyword; like true and false, it is a literal constant and is not allowed as an identifier.
Java Comments
Similar to C/C++, Java also supports single-line and multi-line comments.
The characters in comments will be ignored by the Java compiler.
public
class
HelloWorld
{
/*
This is the first Java program * It will output Hello World * This is an example of a multi-line comment
*/
public
static
void
main
(
String
[
]
args
)
{
//
This is an example of a single-line comment
/*
This is also an example of a single-line comment
*/
System
.
out
.
println
(
"
Hello World
"
)
;
}
}
For more information, refer to:Java Comments.
Java Blank Lines
Blank lines or lines with comments are ignored by the Java compiler.
inheritance
In Java, a class can be derived from other classes. If you want to create a class and there already exists a class with the properties or methods you need, you can make the newly created class inherit from that class.
Using inheritance, you can reuse the methods and properties of an existing class without rewriting that code. The inherited class is called the superclass, and the derived class is called the subclass.
an interface
In Java, an interface can be understood as a protocol for communication between objects. Interfaces play a very important role in inheritance.
An interface only defines the methods to be used by the derived class, but the concrete implementation of the methods depends entirely on the derived class.
Difference Between Java Source Program and Compiled Execution
As shown in the figure below:

The next section introduces classes and objects in Java programming. After that you will have a clearer understanding of classes and objects in Java.