Declare Many Variables
To declare more than one variable of the same type, you can use a comma-separated list:
Example
Instead of writing:
int a =5;
int b= 6;
int c=7;
System.out.println(a+b+c);
You can simply write:
int a =5, b= 6,c=7;
System.out.println(a+b+c);
Example
public class Main {
public static void main(String[] args) {
int a=5,b=6,c=7;
System.out.println(a+b+c);
}
}
One Value to Multiple Variables
You can also assign the same value to multiple variables in one line:
Example
int a, b, c;
a= b = c = 25;
System.out.println(a + b + c);
let's see a simple program
public class Main {
public static void main(String[] args) {
int a, b, c;
a= b = c = 25;
System.out.println(a + b + c);
}
}
Output-75
Related articles
No comments:
Post a Comment
Did you find this article helpful?
😁 😀