Java += compiler/jre bug?

This question already has an answer here:

  • Why don't Java's +=, -=, *=, /= compound assignment operators require casting? 11 answers

  • This is the way += has worked from C . It's "feature" not a bug

    See Java's +=, -=, *=, /= compound assignment operators

    BTW You can try this

    char ch = '0';
    ch *= 1.1; // ch = '4'
    

    Also see a post of mine http://vanillajava.blogspot.com/2012/11/java-and-implicit-casting.html


    int a = a + (3/2.0); doesn't compile since:

  • int + double leads to a double
  • a is referenced as an int
  • cast from double to int missing
  • int a += 3/2.0; compiles since:

  • int + double leads to a double
  • a is referenced as an int
  • double is castable to an int and luckily, compiler adds an implicit cast similar to:

    int a = (int)(a+ 3/2.0); . This is due to the special op= notation that is interpreted more cleverly from compiler than the basic assignment operator: = .


  • This is not a bug , its a implicit conversion happened.

    for eg.

    Byte b=3;
    
    b+=5;  //compiles
    
    b=b+5; //doesnt compiles
    

    Here what happens is

    a=a+3/2.0;   // 3/2.0 produces double , it add int to double but cant convert it implicitly to int.
    
    a += 3/2.0;  // here implicit conversion after addition of a to 3/2.0 happends from double to int
    
    链接地址: http://www.djcxy.com/p/73652.html

    上一篇: Java字符串真的不可变吗?

    下一篇: Java + =编译器/ jre错误?