Consider the following code snippet in Java. I know that the statement temp[index] = index = 0; in the following code snippet is pretty much unacceptable but it may be necessary for some situations:
package arraypkg;
final public class Main
{
public static void main(String... args)
{
int[]temp=new int[]{4,3,2,1};
int index = 1;
temp[index] = index = 0;
System.out.println("temp[0] = "+temp[0]);
System.out.println("temp[1] = "+temp[1]);
}
}
It displays the following output on the console.
temp[0] = 4
temp[1] = 0
I do not understand temp[index] = index = 0;.
How does temp[1] contain 0? How does this assignment occur?
temp[1]to be?indexto first become0makingtemp[index]equivalent totemp[0]so that only the first element is modified.