This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float wrapper takes double primitive implicitly.

 
Arnab Saha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it so?
Float f1 = new Float(1.24); //Gives no compilation error

while,
String s1 = Float.toString(1.24);//Gives compilation error

Are there other situations for Wrapper classes where there is
like this anomally.Please let me know.
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toString() is not a static method in Float.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

Float f1 = new Float(1.24);

Float has three constructors..

Float(float value)
Float(double value)
Float(String value)

Yours is the second constructor..


toString() is not a static method in Float.



toString() is not a static method but
toString(float value) is a static method

Here in your program you have passed double....try passing a float value..

Float.toString(1.24f);

See the result

Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic