Sitemap

Java Syntax

2 min readApr 7, 2023

--

public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
} //Output is Hello World!

Every line of code that runs in Java must be inside a class. In the example, we named the class Main. A class should always start with an uppercase first letter.

Press enter or click to view image in full size
Photo by Joshua Aragon on Unsplash

The main Method:

The main() method has to be in every Java program:

public static void main(String[] args){}

Any code inside the main() method will be executed. That every Java program has a class name which must match the filename, and that every program must contain the main() method.

Classes:

A class represents a single concept.

A Java program must have one class whose name is the same as the program filename.

public class Main {}

Print Line

The Println() Method:

Inside the main() method, we can use the println() method to print a line of text to the screen.

System.out.println("Hello World"); //At the end of the typing method, stay on the bottom line

The Print() Method:

There is also a print() method, which is similar to println().

Get Bora Fazli’s stories in your inbox

Join Medium for free to get updates from this writer.

The only difference is that it does not insert a new line at the end of the output.

System.out.print("Hello World"); //At the end of the writing method, stay in the same line

Comments

Comments can be used to explain Java code and to make it more readable. It can also be used to prevent execution when testing alternative code.

Single-line Comments:

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by Java.

// I am a single line comment!

Multi-line Comments:

Multi-line comments start with /* and ends with */.

Any text between /* and */ will be ignored by Java.

/*
And I am a
multi-line comment!
*/

Each code statement must end with a semicolon (;).

--

--