codingstreets
Search
Close this search box.
java-declare-multiple-variables

Get Started: Java Declare Multiple Variables

In This Article, You Will Learn about Java Declare Multiple Variables.

Java Declare Multiple Variables – Before moving ahead, let’s know a bit about Java Variables.

Table of Contents

Declare Many Variables

To declare more than one variable of same type, Java allows us to use comma-seprated list.

Example: Declare multiple variables of same type.

				
					public class Main{
    public static void main(String[] args){
        int firstvalue = 10; 
        int secondvalue = 20;
        int thirdValue = 30;
        System.out.print(firstvalue + secondvalue + thirdValue);
    }
}
				
			

Alternatively;

Example: Declare multiple variables of same type.

				
					public class Main{
    public static void main(String[] args){
        int firstvalue = 10, secondvalue = 20, thirdValue = 30;
        System.out.print(firstvalue + secondvalue + thirdValue);
    }
}
				
			

One Value To Multiple Variables

Java allows us to assign a same value to the multiple variables in a same line.

Example: Declare same value to multiple variables.

				
					public class Main{
    public static void main(String[] args){
        int firstValue, secondValue, thirdValue;
        firstValue = secondValue = thirdValue = 20;      
        System.out.print(firstValue + secondValue + thirdValue);
    }
}
				
			

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on