java的 x跟x_关于语法:java中的x ++和++ x有区别吗?

本文详细解析了Java中前缀++x与后缀x++的不同之处,通过实例对比了两者的执行顺序及结果差异,对于理解Java中的增量运算符提供了清晰的指导。

java中的++ x和x ++有区别吗?

提示大量相同的答案......

...并提出第一个完全相同的答案...

最快的去战利品,按最旧排序,点击upvote。ohowoho。

我确信我有它! 无论如何,Emils的答案更好。

++ x称为preincrement,而x ++称为postincrement。

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

好的解释,1 ++。 哎呀,++ 1 :)

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

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

例:

x=0;

a=++x;

b=x++;

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

+1很多例子,这是一个例子的解释:)

是的,由于一开始就有明确的散文解释,我也最终赞同这一点。 (嗯,不知道你现在可以在评论中做草书......很酷)

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

不应该是suffix?

是,

int x=5;

System.out.println(++x);

将打印6和

int x=5;

System.out.println(x++);

将打印5。

@Tom,我只是在考虑如何投票,所以我的解释是这样的:一个小理由更喜欢Emil Hs的回答是他的示例代码是/稍微/更多信息。

Jonik。 确实,还包括关键词preincrement和postincrement。

这个"答案"只是告诉你一个测试用例输出,我认为输出不是答案。 相反,通常某些代码执行的(意外)结果导致问题。 因此,我的投票。

我从最近的一个版本登陆这里,虽然这个问题不仅仅是回答,但我还是无法帮助反编译代码并添加"又一个答案":-)

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

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."":()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

在考虑计算机实际上做了什么......

++ 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.这样更简单。

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

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

是。

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

}

}

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

i = 0;

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

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

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

是的,使用++ X,X + 1将在表达式中使用。使用X ++,X将在表达式中使用,并且只有在计算表达式后才会增加X.

因此,如果X = 9,使用++ X,将使用值10,否则使用值9。

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

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

是的,有一个区别,包括x ++(后增量),x的值将在表达式中使用,x将在表达式被评估后递增1,另一方面++ x(preincrement),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);

}

好的,我登陆这里是因为我在检查经典堆栈实现时最近遇到了同样的问题。只是提醒一下,这是在基于数组的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, - 表示递减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

如果附上几句解释,这种反应会更好。

使用i ++,它被称为postincrement,并且该值在任何上下文中使用然后递增; ++ i是preincrement首先递增值然后在上下文中使用它。

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

这是个很大的差异。

由于大多数答案已经指出了这个理论,我想指出一个简单的例子:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值