+ 7
Why not a compilation error?
Can someone please explain: int n1 = Math.pow(3,4) --> compilation error int n2 = 0; n2 += Math.pow(3,4) --> no error why java only cast to integer in the later expression?
5 Réponses
+ 13
Mind To Machine 💻🕆
might you mean n2 in place of n, It is behaving like implicit conversation, good link for it where it is explained very nicely https://stackoverflow.com/questions/2696812/varying-behavior-for-possible-loss-of-precision
thats interesting I was not knowing about it previously.
+ 10
int n1= (int)Math.pow(3,4);
it will work correctly, as Math.pow() returns a double type value, and we are trying to assign it to integer type variable, so we need to do explicit typecasting.
what is datatype of variable n ?
+ 5
Gaurav Agrawal i understand that part but why you dont get an error for the second part?
int n2 = 0;
n2+= Math.pow(3,4)
edit:
i checked my post and realize it was not what i meant. i meant " why NOT a compilation error?"
+ 5
Gaurav Agrawal me again 😅, i meant n2
+ 5
Gaurav Agrawal thanks for the link now i understand