⬅ Previous Next ➡

Basic Java Syntax

Java Tokens, Keywords, Identifiers, Variables, Data Types, Literals, Type Casting & I/O (Scanner, System.out)
  • Tokens are the smallest units in Java code: keywords, identifiers, literals, operators, and separators.
  • Keywords are reserved words with special meaning (cannot be used as variable names): class, public, static, void, int, if, for, return, etc.
  • Identifiers are user-defined names for classes, methods, variables.
  • Identifier Rules: start with letter/_/$, no spaces, cannot start with number, case-sensitive, not a keyword.
  • Variables store data values; must be declared with a data type: int age = 20;
  • Primitive Data Types: byte, short, int, long, float, double, char, boolean.
  • Non-Primitive: String, arrays, classes, interfaces.
  • Literals are constant values: 10 (int), 10.5 (double), 'A' (char), "Hello" (String), true (boolean).
  • Type Casting: converting one type to another.
  • Widening (Implicit): smaller to larger type (int → double).
  • Narrowing (Explicit): larger to smaller type (double → int) using cast operator.
  • Output: System.out.print(), System.out.println(), System.out.printf().
  • Input using Scanner: nextInt(), nextDouble(), next(), nextLine().
  • Scanner Tip: after nextInt()/nextDouble(), use an extra nextLine() to consume the newline before reading full line input.
import java.util.Scanner;

class TokensDemo {
    public static void main(String[] args) {

        // Variables + Data Types
        int age = 20;
        double price = 99.50;
        char grade = 'A';
        boolean isPassed = true;
        String name = "Sourav";

        // Output using System.out
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Price: " + price);
        System.out.println("Grade: " + grade);
        System.out.println("Passed: " + isPassed);

        // Type Casting
        int a = 10;
        double b = a;          // Widening (int -> double)
        double x = 12.75;
        int y = (int) x;       // Narrowing (double -> int)

        System.out.println("Widening: " + b);
        System.out.println("Narrowing: " + y);

        // Input using Scanner
        Scanner sc = new Scanner(System.in);

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

        System.out.print("Enter your marks: ");
        int marks = sc.nextInt();

        System.out.print("Enter percentage: ");
        double percent = sc.nextDouble();

        System.out.println("--- Output ---");
        System.out.println("Full Name: " + fullName);
        System.out.println("Marks: " + marks);
        System.out.println("Percentage: " + percent);

        // Formatted output
        System.out.printf("Formatted: %s scored %d (%.2f%%)%n", fullName, marks, percent);

        sc.close();
    }
}
// Examples of literals
// int num = 100;          // integer literal
// double pi = 3.14;       // floating literal
// char ch = 'Z';         // character literal
// String msg = "Hi";     // string literal
// boolean ok = false;     // boolean literal
⬅ Previous Next ➡