• 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

Post increment and assignment

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How the following code works:
public class Test
{
public static void main(String[] args)
{
int a[] = {4,5,9,8,6,6,0};
int i=1;
int x,y;
a[i] = i+= a[i++];
System.out.println(a[i]);
System.out.println(i);
}
}

The output is 0,6...........
Can anyone explain in a detailed manner please?
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your current understaning of this code?
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From your code


Solution is in this way due to operator precedences:



so the output is :
a[6] = 0;
i=6


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



a[1] should be 5 and not 4.

Then, how to interpret the following code?



Thanks,
Lalitha.
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx to correct me



since precedence of array access is more then + operator and precedence of + operator is more then assignment operator(=). Assignment operator(=) works from right to left.

soni.
 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have one more doubt. When will that i++ execute?

Pls. give the stepwise elaboration of the stmt,



Thanks,
Lalitha.
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just after putting i's value in

a[i++];

which is a[1] then i=2

and the value of i is 2 but after that we are again assigning doing i = 1 + 5 = 6 which changes the value of i from 2 to 6.

Now try this :

int i =0;
i = i++;
System.out.println(i);

and check the output.

soni.
[ May 27, 2005: Message edited by: Soni Prasad ]
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still not cleared
 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, what soni says is something like this :

a[i] = i+= a[i++] can be thought as

a[1] = (i = 1 + (a[i++])) // here i is replaced by 1
now suppose, x = a[i++]
then, a[1] = (i = 1 + (x))

now x will have value of a[1]

x = a[i++] implies
i.e, x = a[i] = a[1] = 5 and
i = i + 1 = 1 + 1 = 2 // now i = 2, but never been used any where

now substitute x in the above,

a[1] = (i = 1 + 5)
= (i = 6) // here i value (now 2) is being replaced by 6
= 6

Hence, i = 6 and a[1] = 6.

Let me know if I am wrong.

Thanks,
Lalitha.
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes! you are absolutly right!

soni.
reply
    Bookmark Topic Watch Topic
  • New Topic