codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

3 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Syntax
☕Java Programming

Java Syntax

Updated 2026-05-12
15 min read

Java Syntax

Welcome to the third chapter of our Java tutorial! In this section, we will dive into the fundamental building blocks of Java programming. Understanding Java syntax is crucial as it forms the basis for writing any Java program. We'll cover essential elements like public classes, the main method, and statement termination.

Introduction

Java syntax refers to the rules that govern how you write code in Java. Just like a language has grammar rules, Java has its own set of rules that dictate how statements should be structured. Mastering these basics will allow you to write clear, readable, and error-free Java programs.

In this tutorial, we'll explore the core syntax elements necessary for getting started with Java programming. By the end of this section, you'll have a solid understanding of how to structure your Java code correctly.

Basic Syntax

Java is a case-sensitive language, meaning that myVariable and MyVariable are considered different identifiers. This sensitivity applies to class names, method names, variables, and other identifiers in Java.

Public Class

In Java, every application must have at least one class. The main entry point of any Java program is the main method, which resides within a class. By convention, the name of the source file should match the public class name. For example, if your public class is named Main, the source file should be named Main.java.

Here's an example of a simple Java program with a public class:

Main.java
1public class Main {
2 // Class body
3};

Main Method

The main method is the entry point for any Java application. It must be defined within a class and have the following signature:

Java
1public static void main(String[] args)
  • public: The method can be accessed from outside the class.
  • static: The method belongs to the class rather than an instance of the class.
  • void: The method does not return any value.
  • main: The name of the method. Java looks for this specific method as the entry point.
  • String[] args: An array of strings that can be passed to the program when it is run.

Here's a complete example with a public class and the main method:

Main.java
1public class Main {
2 // Entry point of the application
3 public static void main(String[] args) {
4 System.out.println("Hello, World!");
5 }
6}

When you run this program, it will output:

Output
Hello, World!

Statement Termination

In Java, every statement must end with a semicolon (;). This rule applies to all types of statements, including declarations and control flow statements.

For example:

Main.java
1public class Main {
2 public static void main(String[] args) {
3 int x = 10; // Declaration statement
4 if (x > 5) { // Control flow statement
5 System.out.println("x is greater than 5");
6 }
7 }
8}

If you forget to include a semicolon, the Java compiler will throw an error.

Practical Example

Let's put together a simple program that demonstrates basic syntax elements. This program will calculate the sum of two numbers and print the result.

SumCalculator.java
1public class SumCalculator {
2 public static void main(String[] args) {
3 int num1 = 5;
4 int num2 = 7;
5 int sum = num1 + num2;
6
7 System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
8 }
9}

When you run this program, it will output:

Output
The sum of 5 and 7 is: 12

Summary

  • Public Class: Every Java application must have at least one class. The source file name should match the public class name.
  • Main Method: The entry point for any Java application. It must be defined with the signature public static void main(String[] args).
  • Statement Termination: All statements in Java must end with a semicolon (;).

What's Next?

Now that you have a good grasp of basic Java syntax, including public classes and the main method, it's time to learn how to output data to the console. In the next section, we'll explore different ways to display information using System.out.println() and other methods.

Stay tuned for more tutorials on Java programming!


PreviousJava Get StartedNext Java Output

Recommended Gear

Java Get StartedJava Output