Ugenz Academy/Tutorial

Syntax

5 min read971 words

Reading progress

Keep learning

Java Syntax

Introduction

Java syntax refers to the rules and structure used to write Java programs. A Java program is made up of classes, methods, statements, variables, and other programming elements.

Understanding basic syntax is important before writing Java programs.

Basic Structure of a Java Program

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

Explanation

  • public → An access modifier.
  • class → Used to define a class.
  • Main → Name of the class.
  • main() → The starting point of a Java application.
  • String[] args → Stores command-line arguments.
  • System.out.println() → Displays output.
  • { } → Define the beginning and end of a block.
  • ; → Marks the end of a statement.

1. Class Syntax

Every Java program is generally organized into classes.

class Student { // class members }

A public class can be written as:

public class Student { // class members }

If the class is public, the filename should normally match the class name.

Student.java
public class Student { }

2. Main Method Syntax

The main() method is the entry point for a standard Java application.

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

Components

  • public → Accessible by the JVM.
  • static → Can be called without creating an object.
  • void → Does not return a value.
  • main → Method name recognized as the application entry point.
  • String[] args → Array for command-line arguments.

3. Statements

A statement is an instruction given to the computer.

Most Java statements end with a semicolon ;.

int age = 20; System.out.println(age);

4. Comments

Comments are used to explain code. They are ignored by the compiler.

Single-Line Comment

// This is a single-line comment int age = 20;

Multi-Line Comment

/* This is a multi-line comment. */ int age = 20;

Documentation Comment

/** * This method displays a message. */ public void display() { System.out.println("Hello"); }

5. Variables

Variables are used to store data.

Syntax

dataType variableName = value;

Example

int age = 20; String name = "Anuradha"; double salary = 25000.50;

6. Data Types

Java provides different data types.

int age = 20; double price = 500.50; char grade = 'A'; boolean passed = true; String name = "Anuradha";

Common primitive data types include:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

String is a class used to represent text.

7. Printing Output

Java provides System.out.println() for displaying output.

System.out.println("Hello Java");

Output:

Hello Java

You can also use:

System.out.print("Hello "); System.out.print("Java");

Output:

Hello Java

8. Taking Input

The Scanner class can be used to take input from the user.

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.println("Name: " + name); } }

9. Operators

Java supports different types of operators.

Arithmetic Operators

int a = 10; int b = 5; System.out.println(a + b); System.out.println(a - b); System.out.println(a * b); System.out.println(a / b); System.out.println(a % b);

Relational Operators

a == b a != b a > b a < b a >= b a <= b

Logical Operators

&& // AND || // OR ! // NOT

10. Conditional Syntax

if Statement

if (condition) { // statements }

Example:

int age = 20; if (age >= 18) { System.out.println("Adult"); }

if-else Statement

if (condition) { // statements } else { // statements }

11. Loop Syntax

for Loop

for (initialization; condition; update) { // statements }

Example:

for (int i = 1; i <= 5; i++) { System.out.println(i); }

while Loop

while (condition) { // statements }

do-while Loop

do { // statements } while (condition);

12. Method Syntax

A method is a block of code that performs a specific task.

returnType methodName(parameters) { // statements }

Example:

public static void greet() { System.out.println("Hello Java"); }

A method with a return value:

public static int add(int a, int b) { return a + b; }

13. Object and Class Syntax

A class defines the structure of objects.

class Student { String name; void display() { System.out.println(name); } }

Creating an object:

Student s1 = new Student(); s1.name = "Anuradha"; s1.display();

14. Important Java Syntax Rules

  • Java is case-sensitive.
  • Statements generally end with ;.
  • Code blocks are enclosed in { }.
  • Class names conventionally use PascalCase.
  • Variable and method names conventionally use camelCase.
  • A public class name should match the filename.
  • String values are written inside " ".
  • Character values are written inside ' '.

Example

String name = "Anuradha"; char grade = 'A';

Complete Example

import java.util.Scanner; public class Student { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.print("Enter your age: "); int age = sc.nextInt(); System.out.println("Name: " + name); System.out.println("Age: " + age); sc.close(); } }

Conclusion

Java syntax provides the basic rules for writing Java programs. The most important concepts to understand are classes, methods, variables, data types, statements, operators, conditions, loops, objects, and comments. Once these basic syntax rules are understood, you can start developing Java applications.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy