It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Got Confused With OutPut... Can Any One Explain Me.. 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 "Got Confused With OutPut... Can Any One Explain Me.." Watch "Got Confused With OutPut... Can Any One Explain Me.." New topic
Author

Got Confused With OutPut... Can Any One Explain Me..

Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
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] ] );
}
}


Out Put is: 1
Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
The result of assignment is always the value assigned. i.e. the value of right operand.

So, the statement 'a [ (a = b)[3] ]' is equivalent to 'a [b[3]]'.

Because the value of '(a=b)' would be 'b'. You can try printing the value of '(a=b)' and verify yourself.


-Aravind<br />SCJP 5.0 (91%), Preparing for SCBCD.
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };

a = b; //if it is added
System.out.println( a [ (a = b)[3] ] );

output is>>>> 2

on the other hand

int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };

System.out.println( a [ (a = b)[3] ] );

output is >>>>>1

Why it can be ?
Can we think this expression (a = b)[3] evaluates in memory and gives only the result without changing a's content ?


[ October 20, 2008: Message edited by: Anut Walidera ]

blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
I am curious about something on this one.

Doesn't the (a=b) have to be evaluated at the beginning of the execution of this line?

If it does, doesn't it assign the value in b to a?

And, if it did wouldn't we be picking up b[0] instead of a[0] ?

My head is really fuzzy on this one. But it seems like the reassignment of a in the parentheses would hava an effect on the final a[0] since a was set equal to b.

[Update]
I prototyped this and ran it and it does give the answer 1.

However, if you add an extra line that merely prints out a[0] guess what I got? 2.

So the re-assignment DID happen but apparently due to some rule about WHEN the assignment takes place with regard to the evaluation of the REST of the variables on this line, the change does not take place until after the line executes... that is... there is a deferral in actually updating a with the contents of b. I guess as the line was scanned, a was already picked up as a from the left, so the assignment in the middle of the line didn't change it? I dont know but it looks to be so.
[ October 20, 2008: Message edited by: Bob Ruth ]

------------------------
Bob
SCJP - 86% - June 11, 2009
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

What the hell is this

Well these types of questions are not asked in the real exam(I hope so :roll: ) as they are minute details which are given in mock exams just to make you feel that the mock is very good while actually these type of things are irrelevant...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
swati mittal
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
@ Thangaraj Raj
If you are preparing for SCJP exam then these type of questions are worth.
[ DELETED ]

[ Jesper Young: Offer of illegal materials deleted ]
[ October 21, 2008: Message edited by: Jesper Young ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Swati are you talking about real exam questions. If yes then let me tell you that it is illegal to use these type of questions as they are illegally copied thus are illegal. Don't waste your SCJP if you have not given it yet. Using these questions anyone can pass the exam. Then it will be of no use...
dee anderson
Greenhorn

Joined: Oct 10, 2008
Posts: 16
a [ (a = b)[3] ] // (a=b) resolves to b
a [ b[3] ] // b[3] resolves to 0
a [ 0 ]

= 1
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Originally posted by dee anderson:
a [ (a = b)[3] ] // (a=b) resolves to b
a [ b[3] ] // b[3] resolves to 0
a [ 0 ]

= 1


Your explanation is usual, the main question here is why did a[0] resolved to 1 while as Bob said that if you add an extra println after this statement with a[0], then the result is 2....
dee anderson
Greenhorn

Joined: Oct 10, 2008
Posts: 16
If you add the extra println then it assigns the reference of a to b so

int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( (a = b)); //resolves to b
System.out.println( a [ (a = b)[3] ] ); //equivalent to (b [ b[3] ])
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

no anderson this is what me and bob are talking about

dee anderson
Greenhorn

Joined: Oct 10, 2008
Posts: 16
Aaah, sorry got confused with the post by Anut.

Anyway, the reason for this is that the line

System.out.println( a [ (a = b)[3] ] );

is evaluated as an expression. An expression is a syntactic construction that has a value. Expressions are formed by combining variables, constants, and method returned values using operators. Java expressions can have side-effects, which are actions that are executed when the expression is evaluated. The assignment of (a=b) is a side effect of the evaluation of the statement, therefore when you go on to then look at a[0], the assigment has taken place.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Well I really want this thread to end as I think that the question is just a waste as these type of questions are not asked in the exam (I think so as most of the standard mock exams don't have these type of question)

But anderson it's true that the expression a [ (a = b)[3] ] has side effects, but the evaluation order must be like this (as you yourself explained)

a[ (a=b)[3] ] //a and b are now the same arrays. Here I feel that a=b will evaluate to a
a[ a[3] ] // a[3] evaluates to 0
a[0] //how does this evaluates to 1 ??
dee anderson
Greenhorn

Joined: Oct 10, 2008
Posts: 16
a [ (a = b)[3] ] // (a=b) resolves to b!
a [ b[3] ] // b[3] resolves to 0
a [ 0 ] = 1

However, as a Side Effect of this evaluation the reference variable a now points to the array b, hence after this expression when you look at a[0] this is the same as looking at b[0], the answer being 2.
manoj kumar jena
Greenhorn

Joined: Sep 24, 2007
Posts: 25

i am not yet a certified candidate.(preparing for scjp 6)
so correct me if i am wrong.
manoj kumar jena
Greenhorn

Joined: Sep 24, 2007
Posts: 25
in the first System.out.println() statement its not a[1] its a[0].
sorry i was in bit hurry to post the answer.
Thangaraj Selvamani
Ranch Hand

Joined: Sep 20, 2008
Posts: 61
According To all Programming Language First Inner Most "()" Will execute
in this case
(a = b) should execute First so reference b must come to a...so both refer to same object...
so b[3].. which means b reference to index 3 value is 0

now both refer to same object the out put must be 2
(a[0] = b[0])
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 797

I hope the following URL may help all of you guys answer is here in the following link

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.13.2

Regards
Ninad


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
 
jQuery in Action, 2nd edition
 
subject: Got Confused With OutPut... Can Any One Explain Me..