File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes In wrapper class.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "In wrapper class.." Watch "In wrapper class.." New topic
Author

In wrapper class..

nachiket deshpande
Ranch Hand

Joined: Oct 06, 2000
Posts: 114
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
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
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
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).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: In wrapper class..