Basic Syntax in Java
In this tutorial, we will learn the basic syntax of Java language. We will go through the key components of a simple Java program.
Java Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output
Hello, World!
Basic Syntax of a Java Program
public class HelloWorld
This line declares a public class namedHelloWorld
. In Java, every application must contain at least one class definition, which encloses the program's entry point.public static void main(String[] args)
This line defines the main method, which is the entry point of the program.public
means the method is accessible from outside the class,static
means it can be called without creating an instance of the class,void
means it does not return a value, andString[] args
is an array of strings that stores arguments passed to the program.{
This opening brace marks the beginning of the main method's body.System.out.println("Hello, World!");
This line prints the string "Hello, World!" to the standard output (usually the screen).System.out
is an output stream, andprintln
is a method that prints the provided string followed by a new line.}
This closing brace marks the end of the main method's body.}
This closing brace marks the end of theHelloWorld
class definition.
Key Points to Remember
- All Java statements must end with a semicolon (
;
). - The main method is the entry point of a Java program.
- Comments can be added using
//
for single-line comments or/* ... */
for multi-line comments. - Code blocks are enclosed in curly braces
{}
. - Java is case-sensitive, meaning that
Main
andmain
would be considered different identifiers.