According to the specification of Java for mathematical operations ,
If the left-hand operand expression is not an array access expression, then:
- First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.
- Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
- Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.
- Otherwise, the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.
I thought that for example: i += j;
Was just a shortcut for: i = i + j;
But if we try this:
int i = 15; long j = 28;
Then i = i + j; will not compile but i += j; will compile fine. So the question is here:
Does it mean that i += j; is a shortcut for something like this i = (type of i) (i + j)?
After digging out into the Java Language Specification I find out that,
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
After that I also tried some other examples :
A good example of this casting is using *= or /=
byte b = 10;
b *= 5.7;
System.out.println(b); // prints 57
or
byte b = 100;
b /= 2.5;
System.out.println(b); // prints 40
or
char ch = '0';
ch *= 1.1;
System.out.println(ch); // prints '4'
or
char ch = 'A';
ch *= 1.5;
System.out.println(ch); // prints 'a'
References:
https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2
This comment has been removed by a blog administrator.
ReplyDeleteIt has been simply incredibly generous with you to provide openly what exactly many individuals wouldíve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
ReplyDeleteJava Training | best java training institute in chennai | java j2ee training in chennai |
java training chennai | java classes in chennai | java course in chennai | java training with placement in chennai| best java training center in chennai |
java coaching classes in chennai
Casino Tycoon 3D | Mapyro
ReplyDeleteGet directions, reviews and information for Casino Tycoon 3D in Greater Phoenix. 양산 출장마사지 Download the free 세종특별자치 출장마사지 app & get 전라남도 출장안마 directions directly to your location. 2nd 오산 출장샵 Floor. 창원 출장마사지 Casino Tycoon 3D