Friday, April 24, 2009

Java operators game

Given the following code:
   int var = 2;
   var+= 2.5;
   System.out.println("var="+var);
the result will be var=4
Now we change that to
   int var = 2;
   var=var + 2.5;
   System.out.println("var="+var);
The result should be the same at first sight but it's not. Compound operators do implicit casting whereas the line "var=var + 2.5;" does not. Explicit casting is required in this situation.
       Have a nice day!

No comments: