2009-07-07 126 views

回答

244

++ x被称为预增量,而x ++被称为后增量。

int x = 5, y = 5; 

System.out.println(++x); // outputs 6 
System.out.println(x); // outputs 6 

System.out.println(y++); // outputs 5 
System.out.println(y); // outputs 6 
11

是,

int x=5; 
System.out.println(++x); 

将打印6

int x=5; 
System.out.println(x++); 

将打印5

50

++ X递增x的值,然后返回X
X ++返回x的值,然后递增

例如:代码运行

x=0; 
a=++x; 
b=x++; 

后a和b都是1,但x将是2.

2

是的,使用++ X,将在表达式中使用X + 1。使用X ++,将在表达式中使用X,并且仅在表达式被评估后才会增加X.

所以如果X = 9,采用++ X,将使用值10,否则,值9

2

如果它像许多其他语言你可能希望有一个简单的尝试:

i = 0; 
if (0 == i++) // if true, increment happened after equality check 
if (2 == ++i) // if true, increment happened before equality check 

如果上述情况不是这样发生的,它们可能是等效的

3

是的。

public class IncrementTest extends TestCase { 

    public void testPreIncrement() throws Exception { 
     int i = 0; 
     int j = i++; 
     assertEquals(0, j); 
     assertEquals(1, i); 
    } 

    public void testPostIncrement() throws Exception { 
     int i = 0; 
     int j = ++i; 
     assertEquals(1, j); 
     assertEquals(1, i); 
    } 
} 
2

是的,返回值分别是增量之前和之后的值。

class Foo { 
    public static void main(String args[]) { 
     int x = 1; 
     int a = x++; 
     System.out.println("a is now " + a); 
     x = 1; 
     a = ++x; 
     System.out.println("a is now " + a); 
    } 
} 

$ java Foo 
a is now 1 
a is now 2 
13

这些被称为后缀和前缀运算符。两者都会将1加到变量中,但是语句的结果有所不同。

int x = 0; 
int y = 0; 
y = ++x;   // result: y=1, x=1 

int x = 0; 
int y = 0; 
y = x++;   // result: y=0, x=1 
8

我降落在这里从近期dup的之一,尽管这个问题多说,我忍不住反编译的代码,并加入‘另一个回答’:-)

为了准确(可能,有点迂腐),

int y = 2; 
y = y++; 

被编译成:

int y = 2; 
int tmp = y; 
y = y+1; 
y = tmp; 

如果javacY.java类:

public class Y { 
    public static void main(String []args) { 
     int y = 2; 
     y = y++; 
    } 
} 

javap -c Y,您会收到以下JVM代码(我已经让我与Java Virtual Machine Specification的帮助评论的主要方法):

public class Y extends java.lang.Object{ 
public Y(); 
    Code: 
    0: aload_0 
    1: invokespecial #1; //Method java/lang/Object."<init>":()V 
    4: return 

public static void main(java.lang.String[]); 
    Code: 
    0: iconst_2 // Push int constant `2` onto the operand stack. 

    1: istore_1 // Pop the value on top of the operand stack (`2`) and set the 
       // value of the local variable at index `1` (`y`) to this value. 

    2: iload_1 // Push the value (`2`) of the local variable at index `1` (`y`) 
       // onto the operand stack 

    3: iinc 1, 1 // Sign-extend the constant value `1` to an int, and increment 
        // by this amount the local variable at index `1` (`y`) 

    6: istore_1 // Pop the value on top of the operand stack (`2`) and set the 
       // value of the local variable at index `1` (`y`) to this value. 
    7: return 

} 

因此,我们终于有:

0,1: y=2 
2: tmp=y 
3: y=y+1 
6: y=tmp 
1

好吧,我在这里降落,因为我最近遇到了同样的问题时检查经典的堆栈实现。只需提醒一下,这是基于数组的Stack实现中使用的,这比链接列表快一点。

下面的代码,检查推和弹出功能。

public class FixedCapacityStackOfStrings 
{ 
    private String[] s; 
    private int N=0; 

    public FixedCapacityStackOfStrings(int capacity) 
    { s = new String[capacity];} 

    public boolean isEmpty() 
    { return N == 0;} 

    public void push(String item) 
    { s[N++] = item; } 

    public String pop() 
    { 
    String item = s[--N]; 
    s[N] = null; 
    return item; 
    } 
} 
1

是的,是有差别的,柜面x的++(后置)中,x的值将在表达和x将通过1中表达后递增可以使用已经评估,在另一方面++ x(预增量),x + 1将用于表达式中。 拿一个例子:

public static void main(String args[]) 
{ 
    int i , j , k = 0; 
    j = k++; // Value of j is 0 
    i = ++j; // Value of i becomes 1 
    k = i++; // Value of k is 1 
    System.out.println(k); 
} 
1

的问题已经回答了,但让我从我的身边加太多。

首先++表示递增1,-表示递减1。

现在X ++意味着递增X这条线之后和++ X意味着递增X此行之前。

检查本实施例

class Example { 
public static void main (String args[]) { 
     int x=17,a,b; 
     a=x++; 
     b=++x; 
     System.out.println(“x=” + x +“a=” +a); 
     System.out.println(“x=” + x + “b=” +b); 
     a = x--; 
     b = --x; 
     System.out.println(“x=” + x + “a=” +a); 
     System.out.println(“x=” + x + “b=” +b); 
     } 
} 

它将给下面的输出:

x=19 a=17 
x=19 b=19 
x=18 a=19 
x=17 b=17 
0

与I ++,它被称为后置,并且该值是在任何情况下,然后递增使用; ++我preincrement首先增加值,然后在上下文中使用它。

如果你没有在任何情况下使用它,它使用什么并不重要,但是按照惯例使用后增量。

4

在考虑什么样的电脑实际上做...

++ X:从内存,增量,利用负载X,存储回内存。

x ++:从内存中加载x,使用,增加,存回内存。

考虑: A = 0 X = F(A ++) Y = F(++ A)

其中函数f(P)返回p + 1

x是1(或2)

y将是2(或1)

并且存在这个问题。编译器的作者是否在检索后,使用后或存储后传递参数?

一般情况下,只需使用x = x + 1。它更简单。

0

有很大的区别。

由于大部分的答案已经指出的理论,我想指出一个简单的例子:

int x = 1; 
//would print 1 as first statement will x = x and then x will increase 
int x = x++; 
System.out.println(x); 

现在让我们看看++x

int x = 1; 
//would print 2 as first statement will increment x and then x will be stored 
int x = ++x; 
System.out.println(x); 
3

在Java x ++和++ x

之间的差异 ++ x是一个前缀形式: 它递增变量表达式,然后使用表达式中的新值。

例如,如果用在代码:

int x = 3; 

int y = ++x; 
//Using ++x in the above is a two step operation. 
//The first operation is to increment x, so x = 1 + 3 = 4 
//The second operation is y = x so y = 4 

System.out.println(y); //It will print out '4' 
System.out.println(x); //It will print out '4' 

X ++是一个后缀形式: 变量值在表达首先使用,然后将其在手术后递增。

例如,如果用在代码:

int x = 3; 

int y = x++; 
//Using x++ in the above is a two step operation. 
//The first operation is y = x so y = 3 
//The second operation is to increment x, so x = 1 + 3 = 4 

System.out.println(y); //It will print out '3' 
System.out.println(x); //It will print out '4' 

希望这是清楚的。运行和使用上面的代码应该有助于你的理解。

相关问题