Easy Tutorial
❮ File Create Java Arraylist Removerange ❯

Java Basic Syntax

A Java program can be considered as a collection of objects that work together by invoking each other's methods. Below is a brief introduction to the concepts of classes, objects, methods, and instance variables.


First Java Program

Here 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
    }
}

Below are the steps 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 issues, you can use the -encoding option to compile with utf-8:

javac -encoding UTF-8 HelloWorld.java 
java HelloWorld

Gif demonstration:


Basic Syntax

When writing Java programs, keep the following points in mind:


Java Identifiers

All components of Java require names. Class names, variable names, and method names are all called identifiers.

Regarding Java identifiers, note the following:


Java Modifiers

Like other languages, Java uses modifiers to modify methods and properties in classes. There are two types of modifiers:

We will delve deeper into Java modifiers in later chapters.


Java Variables


Java Arrays

Arrays are objects stored on the heap and can hold multiple variables of the same type. In later chapters, we will learn how to declare, construct, and initialize arrays.


Java Enums

Java 5.0 introduced enums, which restrict variables to predefined values. Using enums can reduce bugs in the code.

For example, we are designing a program for a juice shop, which limits juice sizes to small, medium, and large. This means customers cannot order juice sizes outside of these three options.

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: Enums can be declared separately or inside a class. Methods, variables, and constructors can also be defined within enums.


Java Keywords

Below is a list of Java keywords. These reserved words cannot be used as constants, variables, or any identifier names.

Category Keyword Description
Access Control private Private
protected Protected
public Public
default Default
Class, Method, and Variable Modifiers abstract Abstract
class Class
extends Extends, Inheritance
final Final, Immutable
implements Implements (Interface)
interface Interface
native Native, Native Method (Non-Java Implementation)
new New, Create
static Static
strictfp Strict, Precise
synchronized Thread, Synchronized
transient Transient
volatile Volatile
Program Control Statements break Break Loop
case Define a Value for Switch Selection
continue Continue
do Run
else Otherwise
for Loop
if If
instanceof Instance
return Return
switch Select Based on Value
while Loop
Error Handling assert Assert Expression is True
catch Catch Exception
finally Execute Regardless of Exceptions
throw Throw an Exception Object
throws Declare an Exception May Be Thrown
try Catch Exception
Package Related import Import
package Package
Basic Types boolean Boolean
byte Byte
char Character
double Double Precision Floating Point
float Single Precision Floating Point
int Integer
long Long Integer
short Short Integer
Variable Reference super Superclass, Super
this This Class
void No Return Value
Reserved Keywords goto Keyword, But Cannot Be Used
const Keyword, But Cannot Be Used

Note: Java's null is not a keyword, similar to true and false, it is a literal constant and cannot be used as an identifier.


Java Comments

Similar to C/C++, Java also supports single-line and multi-line comments. Characters within the comment are 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"); 
    }
}

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 new class inherit from that class.

By using inheritance, you can reuse the methods and properties of an existing class without rewriting the code. The class being inherited from is called the superclass, and the derived class is called the subclass.


Interface

In Java, an interface can be understood as a protocol for communication between objects. Interfaces play a significant role in inheritance.

An interface only defines the methods that the derived class needs to use, but the actual implementation of the methods is left to the derived class.


Java Source Program vs. Compiled Execution

As shown in the diagram: ``` The next section introduces classes and objects in Java programming. Afterward, you will have a clearer understanding of classes and objects in Java.

❮ File Create Java Arraylist Removerange ❯