+ 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 Answers

+ 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

Hot today

Please help me with HTML

1 Votes

What is the mistake here?

3 Votes

Unable to overload +

2 Votes

Hey! [SOLVED]

0 Votes

Pls rectify the errors.

1 Votes