• 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

combination of += nad ++ operator

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class TestOperators {
public static void main ( String argv []) {
int i = 2, j = 2;
i += i++;
j += j--;
System.out.println( "i = " + i );
System.out.println( "j = " + j );
}
}
 
H Gokulam
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is,
The output was i = 4 and j = 4.
Can somebody explain how is it arrived ?
Hari.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow that is a good one...
I was already to explain it, then I ran it...
int i = 2;
i += i++;
should be the same as:
i += i;
i++;
Right?
Actually it is the same as:
i = i + i++;
My guess would be:
i + i is evaluated and i++ is evaluated. Because of the order of operations the result of i + i is assigned to i and as a side effect the result of i++ is lost.
If some one could confirm my guess or corect me that would be great.
[ November 13, 2003: Message edited by: Jeff Swenson ]
 
H Gokulam
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,
I was also expecting five.
i += i gives 4 and then i++ menas it should be 5.
Hari.
 
H Gokulam
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another one!
what will be the output if we change that line to
i = i++ + i++;
it is giving me 5.
Can somebody explain this!
Hari
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Let' try to make the code more readable to understand it.

Hope this helps a bit
Tom
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this thread very helpful:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
Cheers
Harwinder
 
reply
    Bookmark Topic Watch Topic
  • New Topic