Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array question - enthuware

 
Daniel Martins
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I do not understand this syntax...

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martins wrote:Hi

I do not understand this syntax...




why ?



 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey here doesnt assigning b to a will change the value of a...i mean
a=b will be evaluated thus a will be {2,3,1,0}

thus the expression would be {2,3,1,0}[{2,3,1,0}[3]]...isnt it.

i know there is something wrong

please correct me
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur kothari wrote:hey here doesnt assigning b to a will change the value of a...i mean
a=b will be evaluated thus a will be {2,3,1,0}

thus the expression would be {2,3,1,0}[{2,3,1,0}[3]]...isnt it.

i know there is something wrong

please correct me


That was the main funda in this question, everybody in starting thinks like you think, even I thought that way, but after running the example I got it that it is evaluating in different way, means left a evaluated before a=b is evaluated. It has some relationship with array expression evaluation. We need to discuss it further.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what do you think will be the answer?

100 or 90?
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this part of the JLS...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur kothari wrote:

what do you think will be the answer?

100 or 90?


90
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the answer is 90...what i meant is that this works not only for arrays.....gotto look at the jls link ankit gave
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur kothari wrote:yes the answer is 90...what i meant is that this works not only for arrays.....gotto look at the jls link ankit gave


But the JLS topic itself is saying
15.13.1 Runtime Evaluation of Array Access


And I had also said the it has relation with Array expression evaluation.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes punit you were right but what explains x*(x=y) expression?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur kothari wrote:yes punit you were right but what explains x*(x=y) expression?


That is a simple expression that follows left to right evaluation. Array expression are bit complex.
But why did you say this?

hey here doesnt assigning b to a will change the value of a...i mean
a=b will be evaluated thus a will be {2,3,1,0}

thus the expression would be {2,3,1,0}[{2,3,1,0}[3]]...isnt it.

i know there is something wrong

please correct me

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yes left to write....i remember it said first all the operands are calculated from left to right and then operations are done.....thanks a lot....

hey when i wrote that i had not read the jls document
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great healthy discussion of topic is good thing to remember the concepts for long terms.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we have to learn a language we have to speak it....that is why i love javaranch
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur kothari wrote:oh yes left to write....

Left to Right

(I hope you don't think I'm trying to piss you off, I try to find any opportunity to make a joke )...
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hahaha.....
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur kothari wrote:if we have to learn a language we have to speak it....that is why i love javaranch


Good you are getting enough time to discuss it, in between from March to October I did not get time to discuss anything on javaranch due to my cumbersome job, now after changing my job I am getting enough time.
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why would this print 90? Wouldn't expression within brackets(x=y) have precedence over *?

Thanks
Chintan.
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chintan writes:
Why would this print 90? Wouldn't expression within brackets(x=y) have precedence over *?


Hi chintan, printf statement evaluates from left to right, hence first value of x is 9 and after assignment x is 10 hence answer is 90.
 
harish gowda Java green
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit Singh wrote:
Ankur kothari wrote:hey here doesnt assigning b to a will change the value of a...i mean
a=b will be evaluated thus a will be {2,3,1,0}

thus the expression would be {2,3,1,0}[{2,3,1,0}[3]]...isnt it.

i know there is something wrong

please correct me


That was the main funda in this question, everybody in starting thinks like you think, even I thought that way, but after running the example I got it that it is evaluating in different way, means left a evaluated before a=b is evaluated. It has some relationship with array expression evaluation. We need to discuss it further.


" a [ (a = b)[3] ]
a [ b[3] ]
since b[3]=0
hence a[0] i.e 1 will be the answer
"

 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"harish gowda Java green" please check your private messages for some administrative matter.
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rohan.

Regards,
Chintan
 
jeetendra Choudhary
Ranch Hand
Posts: 41
Eclipse IDE Mac OS X Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohan yadav wrote:
Chintan writes:
Why would this print 90? Wouldn't expression within brackets(x=y) have precedence over *?


Hi chintan, printf statement evaluates from left to right, hence first value of x is 9 and after assignment x is 10 hence answer is 90.


Hey From Where this printf came here..?? I think It was println().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic