Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning multiple values to an array in same statement

I have a java code snippet below:

int arr[] = new int[5];
int index = 0;
arr[index] = index = 3;

System.out.println("arr[0] = " + arr[0]);
System.out.println("arr[3] = " + arr[3]);

Output is:

arr[0] = 3
arr[3] = 0

What exactly is happening in line 3?

like image 741
P K Avatar asked Nov 01 '14 15:11

P K


People also ask

How do you assign multiple values to an array in C++?

Here is what you can do: #include <algorithm> int array [] = {1,3,34,5,6}; int newarr [] = {34,2,4,5,6}; std::copy(newarr, newarr + 5, array); However, in C++0x, you can do this: std::vector<int> array = {1,3,34,5,6}; array = {34,2,4,5,6};

How do you assign multiple values to an array in Java?

Java does not provide a construct that will assign of multiple values to an existing array's elements. The initializer syntaxes can ONLY be used when creation a new array object. This can be at the point of declaration, or later on.

Can an array hold multiple values?

Arrays are a type of data that store multiple values in a structured and ordered fashion. Or, in other words, arrays are a type of data that can be stored in variables.

How to assign values to an array of variables?

While creating variables first of all we will declare them, initialize them, assign/re-assign values to them. You can initialize the array by assigning values to all the elements one by one using the index − When we assign primitive values of one type to a variable of other (datatype) implicitly they are converted.

How to get the same value from another array in JavaScript?

To get the same value from another array and insert it into an object of the array we need to. Before jumping into the code, you can read the following articles. It will help you to understand it better, Method 1: Using forEach () and push (), includes () method of array. Method 2: filter (), push () and includes () of array.

How to assign/re-assign arrays in Java?

Assigning arrays in Java. While creating variables first of all we will declare them, initialize them, assign/re-assign values to them. You can declare an array just like a variable − You can create an array just like an object using the new keyword − You can initialize the array by assigning values to all the elements one by one using the index −

What happens if you assign incompatible types to an array?

Therefore while assigning values to the elements of an array you can assign any value which will be cast implicitly. But, if you try to assign incompatible types (higher types) to a variable a compile-time error will be generated saying “possible lossy conversion”.


1 Answers

Java language specification:

15.7 The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

In this case, arr[index] is evaluated first, producing a reference to element zero. This is because index is still 0. Now index = 3 gets evaluated, and 3 is stored in the index. Now the first assignment is ready to complete, so 3 is assigned to element zero.

Java language designers recommend to avoid relying on this rule for clarity, making sure that there is only one side effect to an expression. Your expression, however, has two side effects, one of which relies on the completion of the other.

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

like image 157
Sergey Kalinichenko Avatar answered Sep 22 '22 13:09

Sergey Kalinichenko