aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array question - enthuware 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 - enthuware" Watch "Array question - enthuware" New topic
Author

Array question - enthuware

Daniel Martins
Ranch Hand

Joined: Oct 22, 2009
Posts: 45
Hi

I do not understand this syntax...

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Daniel Martins wrote:Hi

I do not understand this syntax...




why ?




SCJP 6
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Sep 06, 2009
Posts: 531


what do you think will be the answer?

100 or 90?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

See this part of the JLS...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankur kothari wrote:

what do you think will be the answer?

100 or 90?


90
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Sep 06, 2009
Posts: 531
yes punit you were right but what explains x*(x=y) expression?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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

Joined: Sep 06, 2009
Posts: 531
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

Joined: Oct 16, 2008
Posts: 952
Great healthy discussion of topic is good thing to remember the concepts for long terms.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
if we have to learn a language we have to speak it....that is why i love javaranch
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

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

Joined: Sep 06, 2009
Posts: 531
hahaha.....
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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

Joined: Sep 23, 2008
Posts: 83


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

Thanks
Chintan.


SCJA 1.0
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
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.


Sage of The Monstrous Toad of Mount Myoboku
harish gowda Java green
Greenhorn

Joined: Nov 30, 2009
Posts: 4
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

Joined: Aug 03, 2008
Posts: 9313
    
  17

"harish gowda Java green" please check your private messages for some administrative matter.
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Thanks Rohan.

Regards,
Chintan
jeetendra Choudhary
Ranch Hand

Joined: Jun 02, 2009
Posts: 33
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().


Preparing for SCJP...!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Array question - enthuware