• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

In wrapper class..

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
reply
    Bookmark Topic Watch Topic
  • New Topic