DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java BasicsTopic 3 of 59
←PreviousPrevNextNext→

Java Syntax - Complete Guide

Basic Syntax Rules

Java syntax is influenced by C and C++, but it enforces strict rules to reduce errors and improve readability.

Important rules include:

• **Case Sensitivity**: Java is case-sensitive. For example, `MyClass` and `myclass` are different identifiers.

• **Class Names**: Should begin with an uppercase letter and use CamelCase convention.

• **Method Names**: Should begin with a lowercase letter and use camelCase convention.

• **Source File Name**: Must match the public class name exactly, with a `.java` extension.

• **Main Method**: The entry point must be `public static void main(String[] args)`.

Java Statements and Blocks

Statements are instructions that tell the program what to do. A block is a group of statements enclosed in curly braces `{}`.

Every statement must end with a semicolon (`;`).

Example
// Single statement
int x = 10;

// Block of statements
{
    int y = 20;
    int sum = x + y;
    System.out.println(sum);
}

Comments in Java

Comments are ignored by the compiler and serve to make code easier to understand. Java supports three styles of comments:

Example
// Single-line comment

/*
 * Multi-line comment
 * Spans multiple lines
 */

/**
 * Documentation comment
 * Used by Javadoc to generate docs
 * @param name the name to greet
 */
public void greet(String name) {
    System.out.println("Hello, " + name);
}

Identifiers and Keywords

Identifiers are names for variables, methods, classes, and more. Rules for identifiers:

• May contain letters, digits, underscores, and dollar signs.

• Must start with a letter, underscore, or dollar sign (not a digit).

• Cannot be a Java keyword (e.g., `class`, `public`, `static`).

Keywords are reserved words with special meaning and cannot be used as identifiers.

ℹ️ Note: Choose meaningful identifiers to make your code clear and self-explanatory.

Packages and Imports

Packages group related classes and help avoid name conflicts. The `import` statement lets you use classes from other packages without writing their fully qualified names.

Example
// Package declaration (must be the first line)
package com.example.myapp;

// Import specific classes
import java.util.ArrayList;
import java.util.Scanner;

// Import all classes from a package
import java.util.*;

public class MyClass {
    // Class implementation here
}

Common Syntax Pitfalls

  • ❌ Missing semicolons at the end of statements
  • ❌ Mismatched or missing curly braces `{}`
  • ❌ Wrong case for class or method names
  • ❌ File name not matching the public class name
  • ❌ Main method missing or using wrong signature
  • ❌ Forgetting to import required classes

Best Practices

  • ✅ Follow Java naming conventions consistently
  • ✅ Use proper indentation (commonly 4 spaces)
  • ✅ Write comments for complex or important logic
  • ✅ Keep classes and methods focused on one task
  • ✅ Organize your code into packages logically
  • ✅ Use appropriate access modifiers to limit scope
Test your knowledge: Java Syntax - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java BasicsTopic 3 of 59
←PreviousPrevNextNext→