Syllabus / Java / Phase 1: Java Foundations (Days 1–5)
Beginner

Variables and Data Types

📂 Phase 1: Java Foundations (Days 1–5) · Java

Java is statically typed — every variable must be declared with a type before use, and that type cannot change. This catches many errors at compile time rather than at runtime.

The 8 Primitive Data Types

TypeSizeRange / UseExample
byte1 byte-128 to 127byte b = 100;
short2 bytes-32,768 to 32,767short s = 5000;
int4 bytes~ -2.1B to 2.1Bint age = 21;
long8 bytesVery large whole numberslong pop = 8000000000L;
float4 bytesDecimal, less precisionfloat pi = 3.14f;
double8 bytesDecimal, default for decimalsdouble price = 99.99;
char2 bytesSingle characterchar grade = 'A';
boolean1 bittrue or falseboolean isPassed = true;

Declaring and Initializing Variables

int score = 95;
String name = "Vishwas";       // non-primitive (object) type
double percentage = 87.5;
boolean isActive = true;
char initial = 'V';

Variable Naming Rules

  • Must start with a letter, $ or _ (never a digit)
  • Case-sensitive: age and Age are different variables
  • Convention: camelCase for variables (totalMarks, not total_marks)
  • Cannot use Java reserved keywords (class, int, return, etc.)

Primitive vs Non-Primitive Types

Primitives (int, char, boolean, etc.) store actual values directly in memory. Non-primitive types like String, arrays, and objects store a reference to where the data lives in memory — this distinction becomes important later when learning about pass-by-value behavior in method calls.

Interview tip: Java has no unsigned types (unlike C/C++) — every numeric primitive except char is signed by default.