• 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

About op++ & ++op

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program code is:
class Aaa{
int i = 0;
public int methodA(){
int a = i++ - ++i;
System.out.println(i);
return a;
}
}
class Main{

public static void main(String args[]){
Aaa aaa = new Aaa();
System.out.println(aaa.methodA());
}
}
out: 2
-2
can anyone explain why the output
is 2 -2 not 2 2 ?
Thank you.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this --> int a = i++ - ++i;
1. Now i = 0
2. "i++ -" means "0 -" while the present value of i is increased to 1
3. Now "- ++i" means "- 2". Value of i was 1 and so it is increased to 2.
4. So the net calculation is -> 0 - 2 = -2
Hope I could explain it.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has to do with the way pre- and postincrements work. With i++ (post-increment), first i is evaluated, then it is incremented. With ++i(pre-increment), first i is incremented, then evaluated.
int a = i++ - ++i
This works as follows: Initially, i is 0. When trying to do the substraction, the compiler evaluates the first operand, then the second, then it does the substraction.
So, first operand is evaluated to 0, then i is incremented, so it becomes 1. Then, second operand is evaluated, so first i is incremented, it becomes 2, then follows the evaluation, resulting in the current value of i which is 2. So now we have first operand = 0, second operand = 2, substraction results in -2.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int a = i++ - ++i; ->
a = 0 (operator = 0, but now i = 1) - (operator++, i = 2)
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here is the magic trick from Maha Anna
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
Zhi Hong
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You for all, i get it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic