๐Ÿ“˜ [3] Java Syntax & Basics โ€” Structure, Statements, and Starting Right!

๐Ÿงฑ Java: A Structured Language

Java is known for being verbose โ€” but also clear and strict. Its syntax borrows from C/C++, but adds safety, structure, and readability. To code effectively in Java, you must understand how files, classes, methods, statements, and variables come together to form a complete program.

๐Ÿ“„ The Minimal Java Program

This is the smallest valid Java program you can write:

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

โš™๏ธ Anatomy of a Java Program

๐Ÿ—‚๏ธ File Structure & Naming Rules

๐Ÿ”ค Variables & Data Types

Java is statically typed, so every variable must have a type. Examples:

int age = 25;
double pi = 3.14;
char grade = 'A';
boolean isJavaFun = true;
String name = "Aelify";

๐Ÿ“‹ Common Data Types

โœ๏ธ Comments & Readability

// This is a single-line comment

/*
 This is a 
 multi-line comment
*/

๐Ÿšง Java Compilation Flow

  1. You write code in Hello.java
  2. Compile with: javac Hello.java โ†’ creates Hello.class
  3. Run with: java Hello (no .class or .java)

Example output:

Hello, Java!

๐Ÿง  Pro Tips for Beginners

๐Ÿงช Try This Exercise

public class PersonalInfo {
  public static void main(String[] args) {
    String name = "Aelify";
    int age = 21;
    boolean learningJava = true;

    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
    System.out.println("Learning Java? " + learningJava);
  }
}

๐Ÿ’ก Try modifying the values and adding a new variable for height!

โœ… Recap

Java is strict, but predictable. Once you understand its structure โ€” classes, main method, semicolons, and variable types โ€” everything starts to click. You've now written your first real Java code and understood how it flows from source file to output.

โœ๏ธ Java Syntax Basics

โœ… Semicolons

Every statement ends with a semicolon ;.

โœ… Case Sensitivity

Java is case-sensitive: System โ‰  system

โœ… Braces { } define blocks

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

๐Ÿงฎ Variables & Data Types

Type Description Example
int Whole numbers int age = 25;
double Decimal numbers double pi = 3.14;
char Single character char grade = 'A';
boolean True/False boolean alive = true;
String Text (non-primitive) String name = "Aelify";

๐Ÿงช Example:

int age = 30;
double height = 5.9;
boolean isStudent = true;
String language = "Java";

โž• Operators

Operator Purpose Example
+ - * / % Arithmetic a + b, x % y
== != > < >= <= Comparison a == b
&& || ! Logical a > 0 && b < 5
= Assignment x = 5

๐Ÿงช Example:

int a = 10, b = 5;
System.out.println("Sum: " + (a + b));
System.out.println("Is A greater? " + (a > b));

๐Ÿ” Conditional Statements

โœ… If-Else

int score = 85;

if (score >= 90) {
  System.out.println("Grade A");
} else if (score >= 75) {
  System.out.println("Grade B");
} else {
  System.out.println("Grade C");
}

โœ… Switch Case

char grade = 'B';

switch (grade) {
  case 'A':
    System.out.println("Excellent");
    break;
  case 'B':
    System.out.println("Good Job");
    break;
  default:
    System.out.println("Keep Trying");
}

๐Ÿ” Loops in Java

โœ… For Loop

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

โœ… While Loop

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

โœ… Do-While Loop

int j = 1;
do {
  System.out.println("j = " + j);
  j++;
} while (j <= 5);

๐Ÿ“š User Input (Scanner)

import java.util.Scanner;

public class InputExample {
  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("Hello, " + name + "!");
  }
}

๐Ÿง  Exercises โ€” Practice Zone with Answers

๐Ÿ’ก Beginner

import java.util.Scanner;

public class AddTwoNumbers {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter first number: ");
    int a = sc.nextInt();
    System.out.print("Enter second number: ");
    int b = sc.nextInt();
    int sum = a + b;
    System.out.println("Sum = " + sum);
  }
}
public class TableOf7 {
  public static void main(String[] args) {
    for (int i = 1; i <= 10; i++) {
      System.out.println("7 x " + i + " = " + (7 * i));
    }
  }
}
import java.util.Scanner;

public class EvenOdd {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a number: ");
    int num = sc.nextInt();
    if (num % 2 == 0)
      System.out.println(num + " is Even");
    else
      System.out.println(num + " is Odd");
  }
}

๐Ÿงช Intermediate

import java.util.Scanner;

public class AgeClassifier {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter your age: ");
    int age = sc.nextInt();

    if (age < 13)
      System.out.println("Child");
    else if (age < 20)
      System.out.println("Teen");
    else if (age < 60)
      System.out.println("Adult");
    else
      System.out.println("Senior");
  }
}
import java.util.Scanner;

public class Factorial {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a number: ");
    int num = sc.nextInt();
    int fact = 1;

    for (int i = 1; i <= num; i++) {
      fact *= i;
    }

    System.out.println("Factorial = " + fact);
  }
}
import java.util.Scanner;

public class SplitSentence {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a sentence: ");
    String sentence = sc.nextLine();

    String[] words = sentence.split(" ");
    for (String word : words) {
      System.out.println(word);
    }
  }
}

๐Ÿ”ฅ Challenge

public class FizzBuzz {
  public static void main(String[] args) {
    for (int i = 1; i <= 100; i++) {
      if (i % 3 == 0 && i % 5 == 0)
        System.out.println("FizzBuzz");
      else if (i % 3 == 0)
        System.out.println("Fizz");
      else if (i % 5 == 0)
        System.out.println("Buzz");
      else
        System.out.println(i);
    }
  }
}

โœ… Recap

You've covered the most essential building blocks of Java:

โš ๏ธ Common Mistakes

๐Ÿ› ๏ธ Example Fix for Scanner Bug

If you use nextInt() followed by nextLine(), always clear the newline buffer:

import java.util.Scanner;

public class ScannerExample {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter your age: ");
    int age = sc.nextInt();
    sc.nextLine(); // โš ๏ธ This clears the \n left behind by nextInt()

    System.out.print("Enter your name: ");
    String name = sc.nextLine();

    System.out.println("Hi " + name + ", you are " + age + " years old.");
  }
}

You're now ready to build interactive, logical Java programs. Up next, we'll explore variables in detail โ€” primitive vs reference, memory, type casting, and more!

In our next post, we'll dive into ๐Ÿงฎ [4] Data Types & Variables โ€” The Core Foundation of Java!

โ€” Blog by Aelify (ML2AI.com)

๐Ÿ“š Documentation Index