
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Initializing Multiple Variables to the Same Value in Java
In this article, we will learn to initialize multiple variables to the same value in Java.
What is a variable?
A variable is a name given to a space reserved in the memory. Every variable has a type which specifies what kind of data it holds.
Initializing multiple variables to the same value
Multiple variables can be initialized to the same value in one statement as:
variable1 = variable2 = variable3 = value
This means that the value is assigned to variable3, then variable3 is assigned to variable2, and finally, variable2 is assigned to variable1.
Input 1
a = b = c = 10
Output 1
The value of a:10
The value of b:10
The value of c:10
Input 2
str1 = str2 = str3 = "Hello Java"
Ouput 2
The value of str1: Hello Java
The value of str2: Hello Java
The value of str3: Hello Java
Java has two categories of data types primitive and non primitive. We will discuss the two categories separately.
Initialize multiple variables of primitive type at same time
Following are the steps to initialize multiple variables of primitive type at same time
- First, we will intialize the main class and then we will declare three integer variables a, b, and c are declared at the same time.
- Initialize the variables by assigning all three variables the same value (a = b = c = 10).
- The value 10 is first assigned to c, then b gets the value of c, and finally, a gets the value of b.
- To display the values of a, b, and c we will print the values of it.
Example
Java has eight types of primitive data types. Below is an example of assigning multiple primitive datatypes to the same value.
public class Main { public static void main(String args[]){ //declare multiple integer variables int a, b, c; //initialize multiple integer variables to same value a = b = c = 10; System.out.println("The value of a:" + a); System.out.println("The value of b:" + b); System.out.println("The value of c:" + c); } }
Output
The value of a:10 The value of b:10 The value of c:10
Since the assignment operator is right associative, firstly c is assigned the value of 1, then b is assigned the value of c and finally, a is assigned the value of b.
In this case, three memory locations are created and the value 10 is copied to all three different memory locations.
Initialize multiple variables of non primitive type at same time
Following are the steps to initialize multiple variables of non primitive type at same time
- First, we will intialize the main class and then we will declare the three String variables str1, str2, and str3 are declared in the same statement.
- All three variables are assigned the same value (str1 = str2 = str3 = "Hello Java").
- In this case, the memory for the string "Hello Java" is created once, and all three references (str1, str2, str3) point to the same location in memory.
- To print the values of the string variables we will print the values of it.
Example
Non primitive data types like Strings, Arrays, and Classes can be assigned the same way as primitive datatypes. Below is an example of Strings:
public class Main { public static void main(String args[]) { //we will declare three string variables String str1, str2, str3; //initialize them to the same value str1 = str2 = str3 = "Hello Java"; //output the values System.out.println("The value of str1: " + str1); System.out.println("The value of str2: " + str2); System.out.println("The value of str3: " + str3); } }
Output
The value of str1: Hello Java The value of str2: Hello Java The value of str3: Hello Java
The difference here is that the memory is allocated only once and the text "Hello Java" is copied to it.
Three references are created and all the references point to the same memory location.