0

This is the snippet of Java code.

class Test{  
    public static void main(String[ ] args){
        int[] a = { 1, 2, 3, 4 };       
        int[] b = { 2, 3, 1, 0 };     
        System.out.println( a [ (a = b)[3] ] );  
    }
}

Why does it print 1? This is not a homework! I am trying to understand Java. That is related to OCA Java 7 exam.

11
  • Homework? What have you tried so far? Commented Nov 10, 2012 at 16:23
  • 1
    It also gets you whapped on the head with a rolled up newspaper by your supervisor for using the ridiculous construct in the last line. Commented Nov 10, 2012 at 16:27
  • 1
    @GolezTrol Because it doesn't look like a legitimate problem, nor is there any evidence of own research (like the result you'd expect, or why you'd expect it) before posting the contrived mess on here. It's also a textbook example of "too localized". Commented Nov 10, 2012 at 16:28
  • 5
    The point is that any effects of assignment are delayed until after the whole expression is evaluated. That means that the outer a doesn't see the inner assignment to a. Commented Nov 10, 2012 at 16:28
  • 1
    Interesting question (even if the syntax is bound to cause issues later). You should also consider selecting answers for some of your previous questions to keep encouraging community participation. Commented Nov 10, 2012 at 16:29

2 Answers 2

4

At the moment you refer to a[ ... ], a still points to the first array. When the index itself is evaluated, there's an assignment of b to a. So at that moment, a becomes b, of which the 3rd item is fetched, which is 0.

This 0 is used as an index of the array that was already found before. This is the array that a pointed to, although a itself in the mean time has changed. Therefor it prints the 1, even though you might expect 2.

I think that is what this example is trying to show: The array reference is already evaluated and doesn't change once you modify the array variable during the evaluation of the index.

But I wouldn't use this 'feature' in production code. Very unclear.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe it's for SCJP exam or related.
3
System.out.println( a [ (a = b)[3] ] );

First, the value of a is evaluated ({1, 2, 3, 4}). Next, a = b is executed; this assigns the value of b to a and also returns the value of b. b[3] = { 2, 3, 1, 0 } is 0, so, ultimately, {1,2,3,4}[b[3]] = {1,2,3,4}[0] = 1.


To see this, consider the following:

public static void main(String[] args) throws FileNotFoundException {
    int[] a = { 1, 2, 3, 4 };            
    System.out.println( a() [ (a = b())[c()] ] );
}

public static int[] a() {
    System.out.println('a');
    return new int[]{ 1, 2, 3, 4 };
}

public static int[] b() {
    System.out.println('b');
    return new int[]{ 2, 3, 1, 0 };
}

public static int c() {
    System.out.println('c');
    return 3;
}

Output:

a
b
c
1

4 Comments

According to the tutorial I am referring to first the left part has been executed, i.e. a. And only then goes [(a=b)[3]]
@uml Indeed, the a[x] is executed first, but x = b[3], so a[b[3]] is 1.
This answer is wrong. (a = b) is not executed first. If it would be, the solution would be 2, not 1.
(a = b) not only returns { 2, 3, 1, 0 }, but also assigns that array to a, in which case a[0] would return 2. Therefor, a = b is not what is evaluated first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.