Multiple Variables
Creating and managing several variables efficiently
📊 What are Multiple Variables?
Multiple variables allow you to declare several variables of the same type in one line, making your code more concise and organized when handling related data together.
public class MultipleVariables {
public static void main(String[] args) {
// Declare multiple variables in one line
int x = 5, y = 10, z = 15;
String firstName = "John", lastName = "Doe";
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
System.out.println("Name: " + firstName + " " + lastName);
}
}
Output:
x = 5, y = 10, z = 15
Name: John Doe
Ways to Declare Multiple Variables
Same Type, Same Line
Multiple variables of same type
int a = 1, b = 2, c = 3;
Same Value
Assign same value to multiple variables
int x, y, z;
x = y = z = 50;
Mixed Declaration
Some with values, some without
int num1 = 10, num2, num3 = 30;
Related Data
Group related information
double length = 5.0, width = 3.0, height = 2.0;
🔹 Declaring Multiple Variables
You can declare multiple variables of the same type in a single line:
public class DeclareMultiple {
public static void main(String[] args) {
// Multiple integers in one line
int age = 25, year = 2024, count = 100;
// Multiple strings
String city = "New York", country = "USA", continent = "North America";
// Multiple doubles
double price = 19.99, tax = 1.60, total = 21.59;
// Multiple booleans
boolean isActive = true, isComplete = false, isValid = true;
// Print all variables
System.out.println("Age: " + age + ", Year: " + year + ", Count: " + count);
System.out.println("Location: " + city + ", " + country + ", " + continent);
System.out.println("Price: $" + price + ", Tax: $" + tax + ", Total: $" + total);
System.out.println("Active: " + isActive + ", Complete: " + isComplete + ", Valid: " + isValid);
}
}
Output:
Age: 25, Year: 2024, Count: 100
Location: New York, USA, North America
Price: $19.99, Tax: $1.60, Total: $21.59
Active: true, Complete: false, Valid: true
🔹 Assigning Same Value
You can assign the same value to multiple variables:
public class SameValue {
public static void main(String[] args) {
// Method 1: Declare first, then assign same value
int a, b, c;
a = b = c = 10;
// Method 2: Chain assignment in declaration
int x = y = z = 25; // Note: This won't compile! Use method 1 instead
// Correct way for method 2:
int p, q, r;
p = q = r = 50;
// Same value for different calculations
double radius1, radius2, radius3;
radius1 = radius2 = radius3 = 5.0;
System.out.println("a = " + a + ", b = " + b + ", c = " + c);
System.out.println("p = " + p + ", q = " + q + ", r = " + r);
System.out.println("All radii = " + radius1);
// Calculate areas (they'll all be the same)
double area1 = 3.14 * radius1 * radius1;
double area2 = 3.14 * radius2 * radius2;
System.out.println("Area 1: " + area1 + ", Area 2: " + area2);
}
}
Output:
a = 10, b = 10, c = 10
p = 50, q = 50, r = 50
All radii = 5.0
Area 1: 78.5, Area 2: 78.5
🔹 Mixed Declaration
You can mix variables with and without initial values:
public class MixedDeclaration {
public static void main(String[] args) {
// Some variables with values, some without
int score1 = 85, score2, score3 = 92;
String name1 = "Alice", name2, name3 = "Charlie";
double grade1, grade2 = 88.5, grade3;
// Assign values to uninitialized variables
score2 = 78;
name2 = "Bob";
grade1 = 91.2;
grade3 = 87.8;
// Print all values
System.out.println("Scores: " + score1 + ", " + score2 + ", " + score3);
System.out.println("Names: " + name1 + ", " + name2 + ", " + name3);
System.out.println("Grades: " + grade1 + ", " + grade2 + ", " + grade3);
// Calculate average
double average = (grade1 + grade2 + grade3) / 3;
System.out.println("Average grade: " + average);
}
}
Output:
Scores: 85, 78, 92
Names: Alice, Bob, Charlie
Grades: 91.2, 88.5, 87.8
Average grade: 89.16666666666667
🔹 Practical Examples
Real-world examples of using multiple variables:
public class PracticalExamples {
public static void main(String[] args) {
// Student information
String firstName = "Emma", lastName = "Johnson", studentId = "ST12345";
int age = 20, year = 2, semester = 1;
double gpa = 3.75, credits = 15.0, tuition = 5000.0;
// Rectangle dimensions
double length = 10.5, width = 7.2, area, perimeter;
area = length * width;
perimeter = 2 * (length + width);
// Shopping cart
int item1Qty = 2, item2Qty = 1, item3Qty = 3;
double item1Price = 15.99, item2Price = 25.50, item3Price = 8.75;
double total = (item1Qty * item1Price) + (item2Qty * item2Price) + (item3Qty * item3Price);
// Display student info
System.out.println("=== Student Information ===");
System.out.println("Name: " + firstName + " " + lastName);
System.out.println("ID: " + studentId + ", Age: " + age);
System.out.println("Year " + year + ", Semester " + semester);
System.out.println("GPA: " + gpa + ", Credits: " + credits + ", Tuition: $" + tuition);
// Display rectangle info
System.out.println("\n=== Rectangle Calculations ===");
System.out.println("Length: " + length + ", Width: " + width);
System.out.println("Area: " + area + ", Perimeter: " + perimeter);
// Display shopping info
System.out.println("\n=== Shopping Cart ===");
System.out.println("Items: " + item1Qty + " + " + item2Qty + " + " + item3Qty);
System.out.println("Total: $" + total);
}
}
Output:
=== Student Information === Name: Emma Johnson ID: ST12345, Age: 20 Year 2, Semester 1 GPA: 3.75, Credits: 15.0, Tuition: $5000.0 === Rectangle Calculations === Length: 10.5, Width: 7.2 Area: 75.6, Perimeter: 35.4 === Shopping Cart === Items: 2 + 1 + 3 Total: $84.23