在java中x++和++x之间有区别吗?

sf6xfgos  于 2021-07-14  发布在  Java
关注(0)|答案(16)|浏览(643)

在java中,x和x有区别吗?

thigvfpy

thigvfpy1#

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
u3r8eeie

u3r8eeie2#


x增加x的值,然后返回x
x
返回x的值,然后递增
例子:

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

在代码运行之后,a和b都将是1,但是x将是2。

vyu0f0g1

vyu0f0g13#

这些被称为后缀和前缀运算符。两者都将向变量中添加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
hs1ihplo

hs1ihplo4#

对,

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

将打印 6

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

将打印 5 .

anhgbhbe

anhgbhbe5#

我是从最近的一个dup登陆到这里的,虽然这个问题已经回答了很多,但我还是忍不住反编译了代码并添加了“另一个答案”:-)
准确地说(可能有点迂腐),

int y = 2;
y = y++;

编译为:

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

如果你 javac 这个 Y.java 班级:

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

以及 javap -c Y ,您将获得以下jvm代码(我允许我在java虚拟机规范的帮助下注解main方法):

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
zsohkypk

zsohkypk6#

当考虑到电脑的实际功能时。。。
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。简单多了。

ee7vknir

ee7vknir7#

在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'

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

jfgube3f

jfgube3f8#

对。

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);
    }
}
koaltpgm

koaltpgm9#

是的,使用x,表达式中将使用x+1。使用x,x将在表达式中使用,并且x将仅在表达式求值后增加。
因此,如果x=9,使用++x,将使用值10,否则使用值9。

xbp102n0

xbp102n010#

如果它像许多其他语言一样,您可能想简单地尝试一下:

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

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

tct7dpnv

tct7dpnv11#

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

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
6jjcrrmo

6jjcrrmo12#

好吧,我登陆这里是因为我最近在检查经典堆栈实现时遇到了同样的问题。只是提醒一下,这是在基于数组的堆栈实现中使用的,它比链表实现快一点。
下面的代码,检查push和pop功能。

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;
  }
}
czfnxgou

czfnxgou13#

是的,这是有区别的,在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);  
}
nx7onnlm

nx7onnlm14#

这个问题已经回答了,但也请允许我补充一下。
首先,意味着增加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
k0pti3hp

k0pti3hp15#

对于i++,它被称为postincrement,该值在任何上下文中使用,然后递增++i是先递增值,然后在上下文中使用它。
如果你不在任何上下文中使用它,你使用什么并不重要,但是postincrement是按惯例使用的。

相关问题