Please look at the following Float f=new Float(32D); System.out.println(f); the above code compiles,but Integer i=new Integer(32D) does'nt compiler error"incompaitable type can/t convert double to int.but in the first code double gets converted to float,which also is a narrowing conversion??
Lam Thai
Ranch Hand
Joined: Apr 02, 2001
Posts: 117
posted
0
Originally posted by nachiket deshpande: Please look at the following Float f=new Float(32D); System.out.println(f); the above code compiles,but Integer i=new Integer(32D) does'nt compiler error"incompaitable type can/t convert double to int.but in the first code double gets converted to float,which also is a narrowing conversion??
Hello Nachiket, If you look at Sun's java API, you will find that Float has three constructors Float(double), Float(float), and Float(String). Meanwhile Integer has only two, namely Integer(int) and Integer(String). So I guess double literal does not go well with Integer, does it? Regards, Lam
Angela Narain
Ranch Hand
Joined: Apr 14, 2001
Posts: 327
posted
0
Originally posted by Lam Thai: Hello Nachiket, If you look at Sun's java API, you will find that Float has three constructors Float(double), Float(float), and Float(String). Meanwhile Integer has only two, namely Integer(int) and Integer(String). So I guess double literal does not go well with Integer, does it? Regards, Lam
Basically it the point of using the appropriate constructor. So the following code works for Integer wrapper class: So the narrowing conversion requires explicit cast . Integer i=new Integer((int) 32D) ; Correct me if wrong.. thanks
Lam Thai
Ranch Hand
Joined: Apr 02, 2001
Posts: 117
posted
0
Originally posted by Angela Narain: Basically it the point of using the appropriate constructor. So the following code works for Integer wrapper class: So the narrowing conversion requires explicit cast . Integer i=new Integer((int) 32D) ; Correct me if wrong.. thanks
Hello Angela, You are right! But how did your compilation react to your question? Take care, Lam
[This message has been edited by Lam Thai (edited April 21, 2001).]