R
Rishtaara
Java Fundamentals
Lesson 2 of 36Article14 minFREE

Syntax, Output & Comments

Java is case-sensitive — Main and main are different. Every statement usually ends with a semicolon (;). Code lives inside classes and methods.

Syntax — rules of Java code

Java is case-sensitive — Main and main are different. Every statement usually ends with a semicolon (;). Code lives inside classes and methods.

Curly braces { } define blocks. Indentation is for humans — the compiler cares about braces, not spaces.

Real-life example: Syntax is like grammar in English. Missing a semicolon is like forgetting a full stop — the sentence (program) may not make sense.

Basic syntax
public class Demo {
    public static void main(String[] args) {
        int count = 5;
        count = count + 1;
        System.out.println(count);
    }
}

Output — System.out.println

System.out.println() prints text to the console and moves to the next line. System.out.print() stays on the same line.

Use println for debugging and showing results while learning.

Real-life example: println is like writing one line on a whiteboard. print is writing without pressing Enter — the next text continues on the same line.

Print and println
System.out.print("Hello ");
System.out.println("World");
System.out.println(42);
System.out.println("Sum: " + (10 + 5));

Comments — notes for humans

Comments are ignored by the compiler. Use // for one line and /* ... */ for multiple lines.

Good comments explain why, not what obvious code already shows.

Real-life example: Comments are sticky notes on your homework — they help you and teammates, but the teacher (compiler) ignores them.

Single-line and multi-line comments
// This is a single-line comment
int price = 100;

/*
  Multi-line comment
  explains a bigger idea
*/
price = price + 50;