■ JAVA NOTES
Topic 1 — Variables in Java
Class 9 ICSE Computer Applications | Aadarsh Kumar
1■ What is a Variable?
■ A variable is a named memory location that stores a value which can change during the program.
■ Real Life Analogy — Think of your School Bag!
• School bag has different compartments
• Each compartment stores a specific type of thing
• Each compartment has a name so you know what's inside
• Similarly — a variable has a TYPE, a NAME and a VALUE!
2■ 3 Parts of Every Variable
Part Example Meaning
Type int What kind of data to store
Name age What to call the variable
Value 14 What is stored inside
3■ How to Declare a Variable
// Full declaration in one line
int age = 14;
// Declare first, assign value later
int age;
age = 14;
// Value can change anytime — that's why it's called VARIABLE!
age = 15; // now age is 15
age = 16; // now age is 16
// Using value in expression
age = age + 1; // age = 16 + 1 = 17
4■ Variable Naming Rules
Rule Example Valid?
Start with a letter age, name, marks ■ YES
Numbers after letter allowed age1, num2 ■ YES
Underscore allowed my_name ■ YES
Cannot start with number 1age ■ NO
No spaces allowed my name ■ NO
No special characters age@, mark# ■ NO
No Java keywords int, class, if ■ NO
5■ camelCase Convention ■
In Java, variable names follow camelCase — first word in small letters, next words start with Capital
letter!
// ■ Good — camelCase
int myAge = 14;
String studentName = "Aadarsh";
double totalMarks = 92.5;
// ■ Bad — avoid these
int MyAge = 14; // starts with capital — not preferred
int my_age = 14; // valid but not Java convention
int MYAGE = 14; // all caps — not preferred
6■ Java Reserved Keywords
■■ These words are used by Java itself. You CANNOT use them as variable names!
int double char boolean String if
else for while do switch class
public static void return new import
7■ Example Program
public class Variables
public static void main(String args[])
{
int age = 14;
double marks = 92.5;
String name = "Aadarsh";
char grade = 'A';
[Link]("Name -> " + name);
[Link]("Age -> " + age);
[Link]("Marks -> " + marks);
[Link]("Grade -> " + grade);
■ Golden Rules to Remember!
■ Variable = Named Memory Location that stores a value
■ Every variable has 3 parts — Type, Name, Value
■ Value of a variable CAN change during program
■ Always use meaningful names — age not a, x, y
■ Follow camelCase convention — myAge not MyAge
■ Never start variable name with a number
■ Never use spaces in variable names
■ Never use Java keywords as variable names
■ Class 9 ICSE Java Notes | Topic 1 — Variables | Made with ❤■ for Aadarsh Kumar