File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In wrapper class..

 
nachiket deshpande
Ranch Hand
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic