This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bob Ruth
Ranch Hand
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
swati mittal
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a [ (a = b)[3] ] // (a=b) resolves to b
a [ b[3] ] // b[3] resolves to 0
a [ 0 ]

= 1
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no anderson this is what me and bob are talking about

 
dee anderson
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i am not yet a certified candidate.(preparing for scjp 6)
so correct me if i am wrong.
 
manoj kumar jena
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
Hibernate Linux Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic