aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array question" Watch "Array question" New topic
Author

Array question

Wu Wen
Greenhorn

Joined: Oct 14, 2011
Posts: 27
I'm completely lost, I don't know why it prints 1.

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

Why? What do you expect it to print?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

There are two key points to this:

- The assignment operation a = b assigns b to a and returns b.
- Expressions are evaluated left-to-right

Does that help it make sense? If not, what are you expecting to happen?
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245


code assigns reference of b to a. Thus both a and b starts referring to array object {2,3,1,0}. Another object {1,2,3,4} losses its reference. Now expression becomes a[a[3]]. Value at a[3] is 0. Expression becomes a[0]. Value at a[0] is 2 since a now refer to array object {2,3,1,0}. Then why it prints 1?


Astha - OCPJP 6 (90%)
Carlos Renato Domingos da Silva
Greenhorn

Joined: Dec 03, 2011
Posts: 4
Hi the object array a, not lost the reference. It is working with dynamic values. When the code get a=b between parentesis not mean the lots the reference.

step1 a = b so, a = 2, 3, 1, 0 (but not change reference a because is a dynamic call.
step2 give a[3] = 0
step3 give a[a[3]] on originally a (before 1, 2, 3, 4).

so, prints 1. If you have a doubts, make a debug on eclipe or netbeans.

best regards;
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Astha Sharma wrote:
code assigns reference of b to a. Thus both a and b starts referring to array object {2,3,1,0}. Another object {1,2,3,4} losses its reference. Now expression becomes ....


No. There is no "now expression becomes". It is a single expression. And the order of evaluation of the expression is from left to right.

This means, at the point of the assignment, at the point that you are referring to, the effect of the changes are only to the evaluations to the right of that point. The dereferencing to the array object, to the left of that point, has already been done, and goes to the original array that a was referencing.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wu Wen
Greenhorn

Joined: Oct 14, 2011
Posts: 27
Thank you, I understand now.

Henry Wong wrote:
Astha Sharma wrote:
code assigns reference of b to a. Thus both a and b starts referring to array object {2,3,1,0}. Another object {1,2,3,4} losses its reference. Now expression becomes ....


No. There is no "now expression becomes". It is a single expression. And the order of evaluation of the expression is from left to right.

This means, at the point of the assignment, at the point that you are referring to, the effect of the changes are only to the evaluations to the right of that point. The dereferencing to the array object, to the left of that point, has already been done, and goes to the original array that a was referencing.

Henry

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

got it........thanks
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I thought that brackets would always be evaluated first. And also that the left to right rule will only be used if the operators have the same precedence. This seems to go against these rules..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

It's slightly more subtle than that. The left-to-right rule means that the first thing that's done is deciding what the first a is referring to. It's referring to the array { 1, 2, 3, 4 }. This will not change, even when a gets reassigned when the rest of the expression is evaluated. In effect the reference of a has been copied for use in the first part of the expression.

The best approach (when not answering certification questions) is to avoid this sort of confusion completely - as you've seen the results can be counter-intuitive. So avoid using expressions where part of it has a side effect - like assigning b to a in this case - unless you're sure it's safe. If you avoid side effects, then it will all work as you currently understand it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

O. Ziggy wrote:I thought that brackets would always be evaluated first. And also that the left to right rule will only be used if the operators have the same precedence. This seems to go against these rules..


What you are describing are the precedence and the associativity. What we are talking about is order of evaluation. These three things are *not* the same things. All three of these rules apply together.

For example....



Precedence says that multiply is higher, hence ...



Associativity says that addition and multiply are both left to right, hence ...




Now... as for evaluation, remember that a computer is *not* human. Most people will resolve this expression from the inside out -- as that is the easiest way to do it. And if you are good, it can even be done without any paper.

Java follows the rules in the JLS for evaluations, and for the most part, it is done from left to right. This means, as a possible implementation detail, lots of temporary variables (or likely a stack), to remember stuff, as the evaluation is moving left to right.

Henry
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

Got more confused now. Would anyone please explain step by step evaluation of this expression...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

It's effectively doing this:
If you follow that through, does that help?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

Or you can evaluate the statement step by step, from left to right:

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

yes...it helped a lot. Now its clear to me. Thanks to all for the explanation.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array question