• 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 resolution

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

public class Test
{
public static void main()
{
int i=0;
int a[]={0,5};
a[i]=i=9;
System.out.println("i = " + i + " a[0] = " + a[0] + " a[1] = " + a[1]) ;
}
}
Answer is , i = 9 a[0] = 0 a[1] =5
Hence, a[i]=i=9 is resolved as
step 1. a[0]=(i=9);
step 2. a[0]=9;
I was wondering if there is a rule that says, array operands are evaluated first and only then the expression to the right is evaluated?
Thanks.
-Jay
 
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 Jay Kay:

public class Test
{
public static void main()
{
int i=0;
int a[]={0,5};
a[i]=i=9;
System.out.println("i = " + i + " a[0] = " + a[0] + " a[1] = " + a[1]) ;
}
}
Answer is , i = 9 a[0] = 0 a[1] =5
Hence, a[i]=i=9 is resolved as
step 1. a[0]=(i=9);
step 2. a[0]=9;
I was wondering if there is a rule that says, array operands are evaluated first and only then the expression to the right is evaluated?
Thanks.
-Jay


Destination = Source
In that order a[0] = 9;

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jay Kay:

// line 1
i = 0

// line 2
a[0] = 0
a[1] = 5
// line 3 -- the tricky one
// a[i]=i=9;
Each operand is resolved before anything is assigned. Therefore a[i] becomes a[0].
Then 9 is assigned to i because the assignment operator (=) is right associative.
Finally the value in i (9) is assigned to a[0]

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
int i=0;
int a[]={0,5};
a[i]=i=9;
System.out.println("i = " + i + " a[0] = " + a[0] + " a[1] = " + a[1]) ;

The statement a[i] = i = 9; will be processed as follows:
a[i] = i = 9; ==> a[0] = i = 9 ; ==> i = 9; a[0] = i ; ==>
a[0] = 9 ;
so it will print i = 9 a[0] = 9 a[1] = 5
Here is what JLS says on this:
1 Evaluate Left-Hand Operand First
2 Evaluate Operands before Operation
3 Evaluation Respects Parentheses and Precedence
4 Argument Lists are Evaluated Left-to-Right
For Arrays: First, the dimension expressions are evaluated, left-to-right. If any of the expression evaluations completes abruptly, the expressions to the right of it are not evaluated.
Hope u got my point.
Thanks,
Priyha.

 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jay,
To make a long story short:
[] . () (method call) highest precedence
...
...
= += -= *= /= %= &= |= ^= <<= >>= >>>= lowest precedence
Regards,
Manfred.
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.
-Jay
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic