Why: float c = a / (float) b; and not: float c = (float) a / b; The result is the same, but it's not correct (7th condition).
Musisz się zalogować, aby dodać komentarz
11 lipca 2022, 13:54
I solved mine the second way and it passed. In Java either place you put the cast in those examples will end in the same result. When 2 or more different number types are operated on Java will automatically cast the narrower type to the wider type. In this case casting 'a' or 'b' to a 'float' will force the other to be cast to a 'float' before the division operator is processed. It is important to realize that the automatic cast will not happen until the operator is about to be processed, which means that you have to be aware of the order of operations. Take this example:This will output 10 because the conversion to a 'float' doesn't automatically happen until the '+' operator is about to process. Following the order of operations the division will process before the addition so the '3 / 4' will process before the 10 becomes a float. If you simply change the equation to: The results is 10.75, which is what would be expected.