注册 登录
编程论坛 J2EE论坛

x+=y和x=x+y有什么不同?

hurtsky 发布于 2008-07-29 20:34, 2843 次点击
x+=y和x=x+y有什么不同?
不知道大家有没有这样的经历,有时候用x+=y不会出现类型转换错误,而x=x+y就会.
13 回复
#2
hwoarangzk2008-07-30 09:07
的确,有一段程序:
byte x = 1, y = 2;
x += y;
x = x + y;
红色的那行就会提示不能从int转换成byte,除非强制转换
因为Java中,整型相加,结果会自动默认为int型的,如果加数是byte和short的,不能自动转换为int型,但是如果是int和long型的就不会出现这种错误了
#3
hurtsky2008-07-30 19:43
上面那位还是没有说明原因,请问为什么用“+=”可以,可不可以具体说明一下?
#4
hwoarangzk2008-07-31 09:30
+= 和 = 的区别,= 右边先计算好结果再给左边,算好结果会默认为int型的,但左边如果是byte和short型的变量就会出现无法转换的问题,但是+=就是直接将结果赋给左边,没有默认为int型这一步
#5
hurtsky2008-07-31 18:49
谢了
#6
YCVSCY2008-07-31 22:36
受教了,以前还不知道那两个东东还有区别.
#7
msi1102008-08-16 19:23
原来如此··········
受教了···
#8
lzrzhao2008-08-19 10:15
类型会自动转换的
也有转换规则的
#9
liun52102008-08-20 18:28
原来如此啊!谢拉!
#10
时空之蕊2008-08-21 21:32
[bo][un]hwoarangzk[/un] 在 2008-7-31 09:30 的发言:[/bo]

+= 和 = 的区别,= 右边先计算好结果再给左边,算好结果会默认为int型的,但左边如果是byte和short型的变量就会出现无法转换的问题,但是+=就是直接将结果赋给左边,没有默认为int型这一步


个人不赞成这种理解方式:
1 无论=还是+=都会发生类型转换
2 应该只是语法检测问题,对于+=实际也是 x += y; <==> x = x+ y;知识jvm在解析

测试代码
public class Test {
    private static byte x,y;
    public static void main(String[] args) {
        x = 2; y = 3;
        x += y;
        x = 2; y = 3;
        x = (byte) (x + y);
    }
}
编译后的代码是:
// Compiled from Test.java (version 1.5 : 49.0, super bit)
public class Test {
  
  // Field descriptor #6 B
  private static byte x;
  
  // Field descriptor #6 B
  private static byte y;
  
  // Method descriptor #9 ()V
  // Stack: 1, Locals: 1
  public Test();
    0  aload_0 [this]
    1  invokespecial java.lang.Object() [11]
    4  return
      Line numbers:
        [pc: 0, line: 1]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: Test
  
  // Method descriptor #18 ([Ljava/lang/String;)V
  // Stack: 2, Locals: 1
  public static void main(java.lang.String[] args);
     0  iconst_2
     1  putstatic Test.x : byte [19]
     4  iconst_3
     5  putstatic Test.y : byte [21]
   [bo]  8  getstatic Test.x : byte [19]
    11  getstatic Test.y : byte [21]
    14  iadd
    15  i2b
    16  putstatic Test.x : byte [19]
[/bo]
    19  iconst_2
    20  putstatic Test.x : byte [19]
    23  iconst_3
    24  putstatic Test.y : byte [21]
    [bo]27  getstatic Test.x : byte [19]
    30  getstatic Test.y : byte [21]
    33  iadd
    34  i2b
    35  putstatic Test.x : byte [19]
[/bo]
    38  return
      Line numbers:
        [pc: 0, line: 4]
        [pc: 8, line: 5]
        [pc: 19, line: 6]
        [pc: 27, line: 7]
        [pc: 38, line: 8]
      Local variable table:
        [pc: 0, pc: 39] local: args index: 0 type: java.lang.String[]
}

x += y; 实际是红色代码
x = (byte) (x + y);  实际是淡红色代码
我们可以看到他们完全一致,可以想象他们都是完全相同的代码只是x = x + y;无法通过编译器检测,不过他的效果确实是x += y <==> x = x + y

[[it] 本帖最后由 时空之蕊 于 2008-8-21 21:36 编辑 [/it]]
#11
时空之蕊2008-08-21 21:40
第二个测试:
public class Test {
    private static byte x;
    private static int y;
    public static void main(String[] args) {
        x = 2; y = 3;
        x += y;
        x = 2; y = 3;
        x = (byte) (x + y);
    }
}

对应的底层代码为:
// Method descriptor #10 ()V
  // Stack: 1, Locals: 1
  public Test();
    0  aload_0 [this]
    1  invokespecial java.lang.Object() [12]
    4  return
      Line numbers:
        [pc: 0, line: 1]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: Test
  
  // Method descriptor #19 ([Ljava/lang/String;)V
  // Stack: 2, Locals: 1
  public static void main(java.lang.String[] args);
     0  iconst_2
     1  putstatic Test.x : byte [20]
     4  iconst_3
     5  putstatic Test.y : int [22]
     8  getstatic Test.x : byte [20]
    11  getstatic Test.y : int [22]
    14  iadd
    15  i2b
    16  putstatic Test.x : byte [20]
    19  iconst_2
    20  putstatic Test.x : byte [20]
    23  iconst_3
    24  putstatic Test.y : int [22]
    27  getstatic Test.x : byte [20]
    30  getstatic Test.y : int [22]
    33  iadd
    34  i2b
    35  putstatic Test.x : byte [20]
    38  return
      Line numbers:
        [pc: 0, line: 5]
        [pc: 8, line: 6]
        [pc: 19, line: 7]
        [pc: 27, line: 8]
        [pc: 38, line: 9]
      Local variable table:
        [pc: 0, pc: 39] local: args index: 0 type: java.lang.String[]
}

效果也是一致!
就我的理解他们的差别也只是在语法表现上而已!
语法通不过当然无法编译通过,但是底层实际效果我想他们没有差别!
以上纯属个人理解,也没有什么理论根据,只是实验结果!
#12
liuguangzong2008-09-01 18:13
x+=y和x=x+y有什么不同?
你出错的原因 关键是:忽略了“类型转换”  (自动转换:高类型---》低类型)
  
double                       
float
long
--------
int            
short
byte
由上至下数据类型是高---》低


+=会让变量直接计算,不会出现类型转换的问题!
而x=x+y 会将x+y的值计算后,再赋值给左边的x,这时若左边x的数据类型比右边x+y的结果的数据类型低,那么就可能会出错了!(除非你把x+y强制转换类型(地类型---》高类型))
#13
汝珂2008-09-02 23:16
JAVA既然是强类型语言,那么肯定是会进行类型检查得,至于+=得问题底层应该也是检查并转换了得,因为楼上已经有仁兄实验证明,最后得到得是一样得,那么转换也就应该是一样得,就像JAVA得错误分为编译期错误和运行期错误一样,所以底层实现和表层检查可能也分为两种标准吧~~个人看法,欢迎指正
#14
hurtsky2008-09-06 21:16
十分感谢各位
1