Variables — Declaration & Initialization
1. What is a Variable?
● A variable is a named storage location in memory that holds data (number,
text, etc.) which your program can use and change.
2. Syntax
java
type variableName; // Declaration
type variableName = value; // Declaration + Initialization
● Declaration: Tells Java the variable’s type and name (reserves space).
● Initialization: Assigns an initial value.
3. Common Data Types
● int – integer numbers: int age = 20;
● double – decimal numbers: double price = 99.99;
● char – single character: char grade = 'A';
● String – text: String name = "Priya";
● boolean – true/false: boolean isAdmin = false;
4. Examples
java
// Declaration and initialization together
int count = 10;
double pi = 3.14;
char letter = 'M';
String city = "Delhi";
boolean passed = true;
// Separate declaration and assignment
int x; // declaration
x = 5; // initialization (assignment)
5. Multiple Variable Declaration
● You can declare several variables of the same type:
java
int a = 1, b = 2, c = 3;
6. Default Values (Class Scope Only)
● If a variable is declared in a class but not initialized, it gets a default value:
● int, double → 0 or 0.0
● boolean → false
● String/reference → null
● Local variables (inside methods) must be initialized before use.
Java Variable Naming Rules and Conventions
1. Valid Variable Names
● Must start with a letter (a-z, A-Z), underscore _, or dollar sign $.
● Can’t begin with a digit, but digits can follow.
● Can include letters, digits, _, and $.
Examples:
● Valid: score, _count, $number, marks2
● Invalid: 2a, my-count (hyphen not allowed), name! (exclamation not allowed)
2. Reserved Keywords
● You cannot use Java’s reserved words as variable names (int, class, static,
public, etc.).
● Examples of reserved keywords: for, if, while, null, true, false.
3. Naming Conventions
Follow these best practices for clear code (important for professional and exam use):
● camelCase: Start with lowercase, capitalize each new word:
studentScore, maxValue, itemPrice
● Descriptive names: Use meaningful words, not just letters:
Good: totalAmount
Bad: t or temp (unless very short scope)
● Avoid single letters except for temporary values:
Common: i, j, k for loops.
● Do not start with underscore/dollar unless special purpose.
● No spaces, hyphens, or special symbols (except _, $).
4. Examples of Good Variable Names
java
int numberOfStudents;
double averageScore;
String firstName;
boolean isComplete;
5. Examples of Invalid Variable Names
java
int 8thPlace; // Starts with digit (invalid)
double total#; // '#' not allowed
String class; // 'class' is reserved
6. Special Notes
● Variable names are case-sensitive—score, Score, and SCORE are all different.
● Try to keep variable names concise but clear.
Quick Reference Table
Rule/Type Example Valid/Invalid
Starts with letter totalMarks Valid
Starts with digit 7up Invalid
Uses keyword static Invalid
camelCase studentAge Valid
Has hyphen my-name Invalid
Summary
● Declare and initialize variables by giving type, name, and value.
● Follow naming rules—letters, digits (not at start), no spaces/hyphens, not
keywords.
● Use camelCase and descriptive names for clarity.