April 18, 2026
After setting up the development environment and writing our first program in the previous tutorial, the next step is to understand a few more basic concepts of Java programming, including Java syntax. Every programming language has its own syntax and rules that determine how programs should be written. Java is known for having a clear and structured syntax, which helps developers build reliable and scalable applications.
In this tutorial, you will learn the basic structure of a Java program, including how classes work, what the main method does, and how Java source code is organized and interpreted internally. Understanding these fundamental concepts will help you read and write Java programs confidently.
By the end of this tutorial, you will understand the way Java programs are structured and how the Java compiler interprets your code.
Table of Contents
Java syntax refers to the rules that define how Java programs are written and interpreted by the compiler. These rules determine how keywords, classes, methods, and statements should be written.
Just like grammar rules in a natural language, syntax rules ensure that the code is written correctly so that the computer can understand it.
Java syntax is designed to be simple, readable, and structured. Some important characteristics include:
For example, the following code is a valid Java program:
public class Hello{
public static void main(String[] args){
System.out.println("Hello World");
}
} The structure contains:
A class in Java is a blueprint used to create objects. It defines the data (Attributes) and behaviors (Methods) that the objects created from it will have. In other words, a class describes what data an object will store and what actions it can perform.
Suppose a house is to be built by an architect, then:
Example:
class Hello{
// Your code here
} Here, the class name is ‘Hello’.
Key Points:
In the upcoming tutorials of this series, we will learn in detail about classes and objects.
The main method is the entry point of a Java program. when you run a Java program, the JVM looks specifically for this method to start execution.
public static void main(String[] args){
//Your code here
}
To understand the main() method in Java in more detail, refer to the main() method in Java documentation by Oracle.
In Java syntax, a statement is a single instruction written to perform a specific task, and it usually ends with a semicolon.
int x = 10;
System.out.println(x); In Java Syntax, code blocks are defined using curly braces {}, which group multiple statements together. they are generally used in classes, methods, loops and conditions.
{
// block of code
} In Java syntax, the language is case-sensitive, meaning it treats uppercase and lowercase letters differently in identifiers and keywords
.
Example:
‘System’ is not the same as ‘system’.
Therefore, System.out.println() is correct, while system.out.println() results in an error.
Identifiers are names of variables, classes, and methods in Java syntax.
Rules:
Example:
Valid identifiers: totalMarks, _studentName_
Invalid identifiers: 1value, class
A comment is a non-executable java syntax used to improve readability and understanding, which are ignored by the compiler during program execution.
public class Hello{
public static void main(String[] args){
System.out.println("Hello World"); //This syntax prints 'Hello World'
}
} // This is a single-line comment /* This is a multi-line comment
And multiple line comments can be added at once */ /** This is a ducumentation comment */ To display output in Java, we commonly use the System.out.println() method.
There are common two methods to print outputs in java:
Method 1:
System.out.println("Hello");
System.out.println("World");This method prints text on the console and automatically moves the cursor to the next line.
Output:
Hello
WorldMethod 2:
System.out.print("Hello");
System.out.print("World");This method prints the text without moving the cursor to the next line.
Output:
HelloWorld The following steps are followed to execute a Java program:
STEP 1: Compilation -> The compiler converts code into bytecode using the following command and produces a .class file.
javac Example.java STEP 2: Class Loading -> JVM loads the .class file into memory.
STEP 3: Main Method Execution -> To execute, we use the command
java Example JVM searches for the main method and execution process starts.
public static void main(String[] args) public class Test{
public static void main(String[] args){
system.out.println("Hello World");
}
} public class Demo{
public static void main(String[] args){
system.out.print("Java");
system.out.println("Programming");
}
} public class Sample{
public static void main(String[] args){
system.out.println("Hello")
}
} Understand the Stack, Master the System.
Clear explanations of programming fundamentals and problem-solving strategies.
© 2026 deepinthestack.com. All rights reserved.