Java Syntax & Variables

Java program structure, the main method, variables, primitive types, and type inference with var.

Anatomy of a Java program

Java
public class Main {                          // class declaration — must match the filename
    public static void main(String[] args) {  // program entry point
        int age = 21;                         // a statement
        System.out.println("Age: " + age);    // another statement
    }
}
  • Every runnable Java program needs a public static void main(String[] args) method — the JVM calls this first.
  • Statements end with a semicolon ;.
  • Curly braces { } group blocks of code (classes, methods, loops, conditionals).
  • Java is case-sensitive: age and Age are different identifiers.

Variables and type inference

Java is statically typed — every variable has a fixed type, checked at compile time.

Java
int score = 95;
double price = 19.99;
boolean isActive = true;
String name = "Qaisar";

// var infers the type at compile time — still statically typed, just less typing
var city = "Lahore";     // inferred as String
var count = 10;          // inferred as int

var is purely a compiler convenience (since Java 10) — it does not make Java dynamically typed. Once inferred, the type cannot change.

Primitive types

Type Size Example
byte 8-bit byte b = 100;
short 16-bit short s = 30000;
int 32-bit int i = 2_000_000;
long 64-bit long l = 10_000_000_000L;
float 32-bit floating point float f = 3.14f;
double 64-bit floating point double d = 3.14159;
char 16-bit Unicode character char c = 'A';
boolean true/false boolean flag = false;

Everything else — String, arrays, your own classes — is a reference type: the variable holds a reference to an object on the heap, not the raw value itself.

Constants

Java
final double TAX_RATE = 0.15;   // cannot be reassigned after initialization

String basics

Java
String first = "Qaisar";
String last  = "Abbas";
String full  = first + " " + last;      // concatenation
String greeting = String.format("Hello, %s!", full);

System.out.println(full.length());       // 12
System.out.println(full.toUpperCase());  // QAISAR ABBAS
System.out.println(full.contains("Abb")); // true

Strings in Java are immutable — every "modification" (toUpperCase(), concatenation, etc.) actually returns a brand-new String object.

Naming conventions

  • Classes: PascalCaseOrderService, UserRepository.
  • Variables and methods: camelCasetotalPrice, calculateTotal().
  • Constants: UPPER_SNAKE_CASEMAX_RETRIES.

Common mistakes

  • Trying to reassign a final variable.
  • Forgetting Java integer division truncates: 7 / 2 is 3, not 3.5 — you need 7 / 2.0 or a cast.
  • Comparing String objects with == instead of .equals() (== compares references, not content).

Interview questions

Q: What's the difference between == and .equals() for strings? == compares whether two references point to the same object in memory. .equals() compares the actual character content. Always use .equals() (or Objects.equals()) for string content comparison.

Q: Is var the same as JavaScript's var? No — Java's var still infers a single, fixed static type at compile time and is checked exactly like an explicit type. It's local type inference, not dynamic typing.