• 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

assignment

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read the example
public class operat{
public static void main(String args[]){
int i=0;
int []a={3,7};
a[i]=i=9;
System.out.println(i + ""+a[0]+""+a[1]);
}
}
since = opearator is right associative,it shud move from right to left and give arrayindex out of bound.But it does not!!Kindly explain
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Puneet
Well, i try to explain
Any exprseeion is sloved in two steps. They are
Step 1) First Recognise ALL the oprands in expression (moving from left to right)
Step 2) Then do the Operations (considering their prcedence and associativity)
So going through these Steps on ur expression, we have
Step 1) Recognise all operands moving from left to right
So, we have three operands
Operand # 1 = a[0] (a refernce to the 1st elemnet of array a)
Operand # 2 = i (simply a refernce to the variable)
Operand # 3 = 9 ( a constant value)
Step 2) Now the expression becomes a[0]=i=9
And the operation of assignment is done.
Thus giving NO out of Bound error
I hope this will help u
Bye

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx naveed!!
u for clearing my doubt!!
 
Naveed Hassan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I serached more and found the following justification
The precedence of [] is higher than =
So, [] will have to be solved first in the expression

 
reply
    Bookmark Topic Watch Topic
  • New Topic