Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Basic Syntax

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.


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:

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:


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:


Java Modifiers

Like other languages, Java can use modifiers to modify methods and properties in a class. There are mainly two categories of modifiers:

In later chapters we will discuss Java modifiers in depth.


Java Variables

The main types of variables in Java are as follows


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.