aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes one question about casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "one question about casting" Watch "one question about casting" New topic
Author

one question about casting

yub huang
Greenhorn

Joined: Jun 08, 2002
Posts: 17
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
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
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
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.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"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!!
 
 
subject: one question about casting