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

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

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

thigvfpy

thigvfpy1#

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

  1. int x = 5, y = 5;
  2. System.out.println(++x); // outputs 6
  3. System.out.println(x); // outputs 6
  4. System.out.println(y++); // outputs 5
  5. System.out.println(y); // outputs 6
u3r8eeie

u3r8eeie2#


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

  1. x=0;
  2. a=++x;
  3. b=x++;

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

vyu0f0g1

vyu0f0g13#

这些被称为后缀和前缀运算符。两者都将向变量中添加1,但语句的结果存在差异。

  1. int x = 0;
  2. int y = 0;
  3. y = ++x; // result: y=1, x=1
  4. int x = 0;
  5. int y = 0;
  6. y = x++; // result: y=0, x=1
hs1ihplo

hs1ihplo4#

对,

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

将打印 6

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

将打印 5 .

anhgbhbe

anhgbhbe5#

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

  1. int y = 2;
  2. y = y++;

编译为:

  1. int y = 2;
  2. int tmp = y;
  3. y = y+1;
  4. y = tmp;

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

  1. public class Y {
  2. public static void main(String []args) {
  3. int y = 2;
  4. y = y++;
  5. }
  6. }

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

  1. public class Y extends java.lang.Object{
  2. public Y();
  3. Code:
  4. 0: aload_0
  5. 1: invokespecial #1; //Method java/lang/Object."<init>":()V
  6. 4: return
  7. public static void main(java.lang.String[]);
  8. Code:
  9. 0: iconst_2 // Push int constant `2` onto the operand stack.
  10. 1: istore_1 // Pop the value on top of the operand stack (`2`) and set the
  11. // value of the local variable at index `1` (`y`) to this value.
  12. 2: iload_1 // Push the value (`2`) of the local variable at index `1` (`y`)
  13. // onto the operand stack
  14. 3: iinc 1, 1 // Sign-extend the constant value `1` to an int, and increment
  15. // by this amount the local variable at index `1` (`y`)
  16. 6: istore_1 // Pop the value on top of the operand stack (`2`) and set the
  17. // value of the local variable at index `1` (`y`) to this value.
  18. 7: return
  19. }

因此,我们终于有了:

  1. 0,1: y=2
  2. 2: tmp=y
  3. 3: y=y+1
  4. 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是一个前缀形式:它递增变量表达式,然后在表达式中使用新值。
例如,如果在代码中使用:

  1. int x = 3;
  2. int y = ++x;
  3. //Using ++x in the above is a two step operation.
  4. //The first operation is to increment x, so x = 1 + 3 = 4
  5. //The second operation is y = x so y = 4
  6. System.out.println(y); //It will print out '4'
  7. System.out.println(x); //It will print out '4'

x++是一种后缀形式:变量值首先在表达式中使用,然后在操作后递增。
例如,如果在代码中使用:

  1. int x = 3;
  2. int y = x++;
  3. //Using x++ in the above is a two step operation.
  4. //The first operation is y = x so y = 3
  5. //The second operation is to increment x, so x = 1 + 3 = 4
  6. System.out.println(y); //It will print out '3'
  7. System.out.println(x); //It will print out '4'

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

展开查看全部
jfgube3f

jfgube3f8#

对。

  1. public class IncrementTest extends TestCase {
  2. public void testPreIncrement() throws Exception {
  3. int i = 0;
  4. int j = i++;
  5. assertEquals(0, j);
  6. assertEquals(1, i);
  7. }
  8. public void testPostIncrement() throws Exception {
  9. int i = 0;
  10. int j = ++i;
  11. assertEquals(1, j);
  12. assertEquals(1, i);
  13. }
  14. }
展开查看全部
koaltpgm

koaltpgm9#

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

xbp102n0

xbp102n010#

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

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

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

tct7dpnv

tct7dpnv11#

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

  1. class Foo {
  2. public static void main(String args[]) {
  3. int x = 1;
  4. int a = x++;
  5. System.out.println("a is now " + a);
  6. x = 1;
  7. a = ++x;
  8. System.out.println("a is now " + a);
  9. }
  10. }
  11. $ java Foo
  12. a is now 1
  13. a is now 2
6jjcrrmo

6jjcrrmo12#

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

  1. public class FixedCapacityStackOfStrings
  2. {
  3. private String[] s;
  4. private int N=0;
  5. public FixedCapacityStackOfStrings(int capacity)
  6. { s = new String[capacity];}
  7. public boolean isEmpty()
  8. { return N == 0;}
  9. public void push(String item)
  10. { s[N++] = item; }
  11. public String pop()
  12. {
  13. String item = s[--N];
  14. s[N] = null;
  15. return item;
  16. }
  17. }
展开查看全部
czfnxgou

czfnxgou13#

是的,这是有区别的,在x++(后增量)的情况下,x的值将用于表达式中,并且在表达式求值后x将递增1,另一方面,++x(预增量),x+1将用于表达式中。举个例子:

  1. public static void main(String args[])
  2. {
  3. int i , j , k = 0;
  4. j = k++; // Value of j is 0
  5. i = ++j; // Value of i becomes 1
  6. k = i++; // Value of k is 1
  7. System.out.println(k);
  8. }
nx7onnlm

nx7onnlm14#

这个问题已经回答了,但也请允许我补充一下。
首先,意味着增加1,--意味着减少1。
现在x
表示这行之后的增量x,++x表示这行之前的增量x。
检查这个例子

  1. class Example {
  2. public static void main (String args[]) {
  3. int x=17,a,b;
  4. a=x++;
  5. b=++x;
  6. System.out.println(“x=” + x +“a=” +a);
  7. System.out.println(“x=” + x + b=” +b);
  8. a = x--;
  9. b = --x;
  10. System.out.println(“x=” + x + a=” +a);
  11. System.out.println(“x=” + x + b=” +b);
  12. }
  13. }

它将提供以下输出:

  1. x=19 a=17
  2. x=19 b=19
  3. x=18 a=19
  4. x=17 b=17
展开查看全部
k0pti3hp

k0pti3hp15#

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

相关问题