Java: Prefix - Postfix issue

﹥>﹥吖頭↗ 提交于 2019-12-10 23:27:05

问题


I have a small issue performing a subtraction on numbers using prefix and postfix operators. This is my program:

public class postfixprefix  
{  
  public static void main (String args[])  
  {  
    int a = 5;  
    int b;  
    b = (a++) - (++a);  
    System.out.println("B = " +b);  
  }  
}  

Doing this, theoretically I am supposed to get 0 as the answer, but however, I am getting a -2.

When I try to individually try to increment the values like in this program:

public class postfixprefix  
{  
  public static void main (String args[])  
  {  
    int a = 5;  
    int b, c;  
    b = a++;  
    c = ++a;  
    System.out.println("B = " +b);  
    System.out.println("C = " +c);  
  }  
}

I get the values as B = 5, C = 7.

So i get the idea that 'c' takes the value of 'a' from 'b' (Please correct me if i am wrong), but what I want to know is

  1. How can I have it not take the value of 'a' from 'b', and
  2. using prefix - postfix, can I have 0 as an answer when they're subtracted.

回答1:


b = a++; means:

  1. assign to b the value of a
  2. increase a by 1

c = ++a means:

  1. increase a by 1
  2. assign to c the value of a

b = (a++) - (++a) means:

  1. get the value of a (5) (a without the ++)
  2. increase the value of a by 1 (thus making it 6) (the result of a++)
  3. increase a by 1 (++a) (thus making it 7)
  4. assign to b thae value 5-7=-2 (5 from step 1, 7 from step 3)



回答2:


If you go through this step by step, you can see what happens:

b = (a++) - (++a); //a is now 5
b = 5 - (++a);     //read a, then increment it. a is now 6
b = 5 - 7;         //increment a, then read it. a is now 7
b = -2

If you do it the other way, you get:

b = (++a) - (a++); //a is now 5
b = 6 - (a++);     //increment a, then read it. a is now 6
b = 6 - 6;         //read a, then increment it. a is now 7
b = 0



回答3:


So i get the idea that 'c' takes the value of 'a' from 'b' (Please correct me if i am wrong), but what I want to know is 1) How can I have it not take the value of 'a' from 'b'

Its not like this, in c = ++a; value is taken from a only, in b = a++; statement, a was incremented but after assigning value to b, and then while c = ++a; a is again incremented and assigned to c (as this is pre-increment now)

2) using prefix - postfix, can I have 0 as an answer when they're subtracted.

you can have like: b = (++a) - (a++); as first a increments first and then the second a (which is now 6) is substracted from first a (still 6). And then the final value of a is 7




回答4:


int a = 5;  
int b, c;  
b = a++;  
c = ++a;  

About this code b has a value 5 because posting fix increment/decrement happens after assingment is completed. So the value is 5.

c has a value 7 because prefix increment/decrement happens before assingment is completed. So the value is 7 beause previous statement made the value of a as 6.

About this code

int a = 5;  
int b;  
b = (a++) - (++a);  
System.out.println("B = " +b);

when brackets are applied, your prefix/postfix operations will be completed first in (a++) - (++a); from left to right fashion.

So firstly if we go left to right

(a++) -(++a)
1. (a++) -- Take 5 from a.
2. (++a) -- 5 becomes 6 with ++a take 6.
3. (a++) - (++a) -- Subtract results of (a++) - (++a) operations which makes it -2.

Solutions for you first query -- How can I have it not take the value of 'a' from 'b', and

int a = 5; 
int temp = a; 
int b, c;  
b = a++;  
c = ++temp;  
System.out.println("B = " +b);  
System.out.println("C = " +c);

**Solutions for you first query has been well explained by Sir @Keppil **



来源:https://stackoverflow.com/questions/16460879/java-prefix-postfix-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!