(i += 1)
is equivalent to i = i + 1
is it possible to have something like above by using multiplication like:
(i *= 1)
, i = i * 1
I have try it by declare as double but I keep get 0.0 value in my result?
(i += 1)
is equivalent to i = i + 1
is it possible to have something like above by using multiplication like:
(i *= 1)
, i = i * 1
I have try it by declare as double but I keep get 0.0 value in my result?
It sounds like you're multiplying zero by one.
0 * 1 = 0
First declare double i = 1.0;
(or int i = 1;
if no decimal values are needed.) It seems that you're multiplying by zero. Of course, then i
times 1
will always be 1
unless you're modifying the value of i
somewhere else.
Other than that, be aware that i *= 1
is almost equivalent to i = i * 1
. The devil is in the details, as the first form will perform an implicit conversion as per the Java Language Specification, section §5.1.3:
compound assignment expressions automatically cast the result of the computation they perform to the type of the variable on their left-hand side. If the type of the result is identical to the type of the variable, the cast has no effect. If, however, the type of the result is wider than that of the variable, the compound assignment operator performs a silent narrowing primitive conversion
If i
has no assigned value (other than zero) before multiplying then it will be 0*1 equals 0.
Its taking the default value of double, as you have declared it in the class scope...
Try this...
class Test implements TestInterface {
public static void main(String[] args){
double i = 1;
System.out.println(i *= 1);
}
}