Java Basic Syntax

 

When you start learning Java, the first thing you’ll encounter is its basic syntax. Syntax in programming refers to the set of rules that define how code must be written so the compiler can understand it. If you’re new to Java, understanding its syntax will give you a strong foundation for writing clean and correct programs.

Let’s break down the basic Java syntax step by step.

1. Structure of a Java Program

 

Every Java program follows a specific structure. Here’s a simple example:

public class HelloWorld {
  public static void main(String[] args) {
  System.out.println(“Hello, Java!”);
  }
}

Key elements in the structure:

 

      1. Class Declaration

  • Every Java program must have at least one class.
  • The class name must match the filename (case-sensitive).
  • Example: public class HelloWorld should be saved as HelloWorld.java

     2. main Method

  • The entry point of any Java program i s the main() method.
  • Syntax: public static void main(String[] args)
  • Without this, your program won’t run.

     3. Statements inside the method

  • Code inside { } (curly braces) belongs to that block.
  • Example: System.out.println(“Hello, Java!”);

         prints text to the console.

2. Case Sensitivity

 

Java is case-sensitive. This means:

System and system are not the same.

Variable names like age and Age are treated as two different identifiers.

3. Class Names

 

Class names should start with an uppercase letter.

If a class name has multiple words, each word should begin with a capital letter (PascalCase).

Example:

public class StudentDetails { }

4. Method Names

 

Method names should start with a lowercase letter.

For multiple words, use camelCase.

Example:

public void calculateTotalMarks() { }

5. Identifiers

 

Identifiers are the names you give to variables, methods, classes, etc.

Rules for identifiers:

Must begin with a letter, underscore _, or dollar sign $.

Cannot start with a digit.

Cannot use keywords (like int, class, public) as identifiers.

Example:

int age; // valid
String _name; // valid
double $price; // valid
int 2ndValue; // invalid

6. Java Keywords

 

Java has reserved words that cannot be used as identifiers.
Examples: class, public, static, void, if, else, new, return.

A few commonly used ones:

public, static, void, class, int, double, if, else, while, for, return, break, continue

7. Comments in Java

 

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

Types of comments:

Single-line comment

// This is a single-line comment

Multi-line comment

/* This is a
multi-line comment */

Documentation comment (used for generating documentation with Javadoc)

/**
* This is a documentation comment
*/

8. Whitespace and Indentation

 

Java ignores extra spaces, tabs, and newlines.

However, proper indentation makes code readable.

Example:

public class Example {
  public static void main(String[] args) {
    int x = 10; // good indentation
    System.out.println(x);
  }
}

9. Statements

 

A statement is a single line of code that ends with a semicolon (;).

Example:

int number = 5;
System.out.println(number);

10. Blocks

 

A block in Java is a group of statements enclosed in curly braces { }.

Example:

if (true) {
  System.out.println(“Inside block”);
}

11. Naming Conventions

 

Following conventions makes code professional and easier to understand:

Classes → PascalCase → StudentDetails

Methods → camelCase → calculateMarks

Variables → camelCase → studentName

Constants → UPPERCASE → PI = 3.14

12. Example Program Putting It All Together

 

public class Student {
  // main method – program entry point
  public static void main(String[] args) {
  int age = 20; // variable declaration
  String name = “Alice”;

  System.out.println(“Student Name: ” + name);
  System.out.println(“Student Age: ” + age);
  }
}

Output:

Student Name: Alice
Student Age: 20

Java Program Structure

Key Takeaways

 
  1. Java syntax defines how we write programs.
  2. Programs are organized into classes and start from the main() method.
  3. Java is case-sensitive.
  4. Use proper identifiers, keywords, and conventions.
  5. Always end statements with a semicolon (;).
Scroll to Top