Q: In the following code, what are the legal type types for the variable vari, which is declared on line 2? 1. class C { 2. public ??? vari; 3. public static void main(String[] args) { 4. foo(vari); 5. } 6. static void foo(float f){ 7. System.out.println("f=" + f); 8. } 9. } a) boolean b) byte c) char d) double e) float f) int g) long h) short I think the answers are b,c,e,f,h. but the correct answers are b,c,e,f,g,h. I compile the g answer, it is really ok, I don't know why. Can someone help me?
I want to know
Bin Wang
Greenhorn
Joined: Mar 04, 2002
Posts: 5
posted
0
in g, type long to float is implicit windening conversion,even they have same length. so it is correct.
Bin Wang
Greenhorn
Joined: Mar 04, 2002
Posts: 5
posted
0
sorry, type long to float is implicit windening conversion,even length of long is 64 and float is 32.
yuelan
Greenhorn
Joined: Apr 25, 2002
Posts: 2
posted
0
The conversion is legal because the 32 bits for a float number are divided into two fields: one for magnititude, one for precision and thus the range of a float number is definitely wider than a long number. But note the conversion may result in the loss of accuracy for some large long number.
"bill wang1965", "yub" and "yuelan" Welcome to the JavaRanch! Please adjust your respective displayed names to meet the JavaRanch Naming Policy. You can change it here. Thanks, and again welcome to the JavaRanch!!