Introduce Java Declare Multiple Variables and give examples
In Java, you can declare multiple variables of the same type in a single statement, which can help make your code more concise and readable. This is particularly useful when you need to initialize several variables that share the same data type.
The general syntax for declaring multiple variables of the same type is as follows:
dataType variable1, variable2, variable3, ...;
int a, b, c;
a = 5;
b = 10;
c = 15;
You can also initialize them at the time of declaration:
int x = 1, y = 2, z = 3;
double length, width, height;
length = 5.5;
width = 3.2;
height = 4.8;
Or initializing them directly:
double radius = 2.5, area = 3.14 * radius * radius, circumference = 2 * 3.14 * radius;
String firstName, lastName, fullName;
firstName = "John";
lastName = "Doe";
fullName = firstName + " " + lastName;
Or initializing them directly:
String city = "New York", country = "USA", continent = "North America";
boolean isActive, isVerified, isAdmin;
isActive = true;
isVerified = false;
isAdmin = true;
Or initializing them directly:
boolean isAvailable = true, isOnSale = false, isFeatured = true;
Declaring multiple variables in Java can help streamline your code and improve its readability. However, it's essential to balance conciseness with clarity to ensure that your code remains understandable.