Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
This comment is hidden because it contains spoiler information about the solution
Automatic typecasting during overlapping assignment (+=, *=, ...) :
sum += Math.pow();
Explicit type casting:
sum = sum + (int) Math.pow();
You are trying to declare an int variable with a double value, which obviously won't work.
However, in this case it works, because compilator knows that the type of sum variable is int and it just casts it to int. Since the result of pow is an integer (double), nothing special happens, but if the result were a non-integer, then the decimal part of the method's result would be truncated (100.18 would be 100, 11.254 would be 11, 1.0 would be 1, and so on, and so on...).
I know this comment is 7 months old, but I wanted to clear it up.
That is correct, why does this code work. It shouldn't at all.
e1 += e2 translates into e1 = (T) (e1 + e2), where T is a type of e1. In this case, the implicit cast crops double into int, which might lead to a not-very-easy-to-find bug:
int num = 1;
double d = 9999999999d;
num += d;
Oh I understand now. Thank you very much.!!!
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
thx bro. I appriciate it
https://stackoverflow.com/questions/2696812/varying-behavior-for-possible-loss-of-precision
https://stackoverflow.com/questions/8710619/why-dont-javas-compound-assignment-operators-require-casting
this is strange. When I write this code:
int sum = Math.pow(1,2);
I will get an error message which says: "Cannot convert from double to int".
But why does your notation ->> sum += Math.pow(a ,b); works?
if you rewrite this to -> sum = sum + Math.pow(); <<- it wont work!
Could you please explain?
this is strange. When I write this code:
int sum = Math.pow(1,2);
I will get an error message which says: "Cannot convert from double to int".
But why does your notation ->> sum += Math.pow(); works?
if you rewrite this to -> sum = sum + Math.pow(); <<- it wont work!
Could you please explain?
this is strange. When I write this code:
int sum = Math.pow(1,2);
I will get an error message which says: "Cannot convert from double to int".
But why does your notation ->> sum += Math.pow(); works?
if you rewrite this to -> sum = sum + Math.pow(); <<- it wont work!
Could you please explain?
this is strange. When I write this code:
int num1 = Math.pow(1,2);
I will get an error message which says: "Cannot convert from double to int".
But why does this notation ->> num1+ += Math.powe(); <<-- works?
if you rewrite this to -> num1 = num1 + Math.pow(); <<- it wont work!
Could you please explain?
Loading more items...