• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operators

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers
i have few doubts plz any one can help me out.
question 1

according to my knowledge assignment associativity is from right to left just we have

if i am wrong plz make it clear why the above code snippet through an ArrayIndexOutofBoundsException???
Question 2
in Shift right with zero fill operator
if we have
byte b=-42;
b>>>4;
then it will give you an ans=268435453 that is clear to me but in actual exam time is short is there any short method of doing this to save time???
Thanks in advance

------------------
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ragu Sivaraman:
[B]
according to my knowledge assignment associativity is from right to left just we have

if i am wrong plz make it clear why the above code snippet through an ArrayIndexOutofBoundsException???
Question 2
in Shift right with zero fill operator
if we have
byte b=-42;
b>>>4;
then it will give you an ans=268435453 that is clear to me but in actual exam time is short is there any short method of doing this to save time???
Thanks in advance

[/B]


Q1 Doesnt give produce error
Its evaluated from right to left.
So a[i] ie a[0] gets the value 10
so the answer is 10 7
Ragu[/B]
</BLOCKQUOTE>
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1 -> the precedence of [] operator is higher than = so a[i] should been seen as a[0] in this example and not a[10]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RHE explain this in Chapter 2 of their book (see the "Evaluation Order" section).
Basically, operands are evaluated left to right, even if the order of execution of the operations is different. So in the statement in question:
1. a[i] is evaluated: reference to the element a[0]
2. Next, i is evaluated: simply a reference to variable i
3. Next, constant 10 is evaluated.
Then, operations are done:
4. 10 is assigned to i
5. a[0] is assigned 10 (the value of the expression i=10)
HTH
Junilu
[This message has been edited by JUNILU LACAR (edited November 16, 2001).]
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vivek Gupta:
Q1 -> the precedence of [] operator is higher than = so a should been seen as a[0] in this example and not a[10]


Hi Vivek
First of all [] is not operator. check the link below from JLS.
http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#230663
Now as RHE says:
[I] All operands are evaluated left to right, even if the order of execution of the operations is something different
.
here in Q1.
<pre>
a=i=10;-------------explan this line
</pre>
while evaluating opearnds, when we move from left to right. First we get a[i], 'i'(index of a) is evaluated and replaced by it's value 0.
second we get 'i' which is simply reference to variable called i.
third we get '10' which is nothing but a integer litral.
[I]Now evaluation is over
, now operations will take place. and your statement is right now is like this:
a[0] = i = 10;
now u are right as associativity of operator '=' is from RIGHT to LEFT.
(a[0] = (i = 10));
henec a[0]= 10 , this will not throw any error/exception and print 10 7.
Q 2: This might help you : http://www.javaranch.com/campfire/StoryBits.jsp
------------------
Regards
Ravish
 
farrukh mahmud
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kumar it really clear my doubts.
thanks all of you as well

------------------
reply
    Bookmark Topic Watch Topic
  • New Topic