java中的++ x和x ++有区别吗?在java中x ++和++ x有区别吗?
回答
++ 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
是,
int x=5;
System.out.println(++x);
将打印6
和
int x=5;
System.out.println(x++);
将打印5
。
是
++ X递增x的值,然后返回X
X ++返回x的值,然后递增
例如:代码运行
x=0;
a=++x;
b=x++;
后a和b都是1,但x将是2.
是的,使用++ X,将在表达式中使用X + 1。使用X ++,将在表达式中使用X,并且仅在表达式被评估后才会增加X.
所以如果X = 9,采用++ X,将使用值10,否则,值9
如果它像许多其他语言你可能希望有一个简单的尝试:
i = 0;
if (0 == i++) // if true, increment happened after equality check
if (2 == ++i) // if true, increment happened before equality check
如果上述情况不是这样发生的,它们可能是等效的
是的。
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);
}
}
是的,返回值分别是增量之前和之后的值。
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
这些被称为后缀和前缀运算符。两者都会将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
我降落在这里从近期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 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
好吧,我在这里降落,因为我最近遇到了同样的问题时检查经典的堆栈实现。只需提醒一下,这是基于数组的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;
}
}
是的,是有差别的,柜面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。
现在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 ++,它被称为后置,并且该值是在任何情况下,然后递增使用; ++我preincrement首先增加值,然后在上下文中使用它。
如果你没有在任何情况下使用它,它使用什么并不重要,但是按照惯例使用后增量。
在考虑什么样的电脑实际上做...
++ 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。它更简单。
有很大的区别。
由于大部分的答案已经指出的理论,我想指出一个简单的例子:
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);
在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'
希望这是清楚的。运行和使用上面的代码应该有助于你的理解。
- 1. Python中的if bool(x)`和if x`有什么区别吗?
- 2. (void **)&x和(void *)x有什么区别?
- 3. “Object [] x”和“Object x []”有什么区别吗?
- 4. 在Ruby中,%w(x y z)和%w [x y z]之间有区别吗?
- 5. $('x> y'),$('y','x')和$('x y')之间有什么区别?
- 6. float x []与float [] x有什么区别?
- 7. x ++在装配中比x = x + 1和x + = 1更有效吗?
- 8. R中的x^2和I(x^2)有什么区别?
- 9. JavaScript中的'var $ x'和'var x'有什么区别?
- 10. emacs中'M-x load-file'和'M-x load-library'有什么区别?
- 11. ruby中`| x,y |`和`| x; y = 0 |`有什么区别?
- 12. OpenGL GLSL中的atan(y/x)和atan2(y,x)有什么区别
- 13. scala中的“Hello”foreach(x = x * _.toLong)和“Hello”foreach(x * = _.toLong)有什么区别?
- 14. 通过e.id = X和e.attr(“id”,X)分配新ID有什么区别吗?
- 15. x |之间的区别y和y <(x)在bash中?
- 16. `typedef struct X {}`和`typedef struct {} X`有什么区别?
- 17. 演员之间有什么区别:(A)x和x为A?
- 18. JavaScript:if(!x)`和if(x == null)`有什么区别?
- 19. Python:“import X”和“from X import *”有什么区别?
- 20. X-XSRF-TOKEN和X-CSRF-TOKEN有什么区别?
- 21. Haskell:(+1)和(\ x-> x + 1)有什么区别?
- 22. object x = null和object x =(object)null有什么区别?
- 23. x = * p ++和* p ++; x = * p in c有什么区别?
- 24. “x is null”和“x == null”有什么区别?
- 25. Y〜X和Y〜X + 1有什么区别?
- 26. Request.ServerVariables [“HTTP_X_FORWARDED_FOR”]和Request.Headers [“X-Forwarded-For”]之间有区别吗?
- 27. 在C++中int和r = x和r = y有什么区别?
- 28. C语言:递归函数中x + 1和x ++的区别?
- 29. Spark 1.x和2.x中各种sparkcontext之间的区别
- 30. 当我<x和范围(x)中的i之间的区别?
提示相同答案的洪流... – skaffman 2009-07-07 21:10:16
...和upvoting的第一个相同的答案进入... – skaffman 2009-07-07 21:12:41
到最快去战利品,按最老排序,点击upvote。 ohowoho。 – dotjoe 2009-07-07 21:16:25