Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting: float to Float

 
Steve Rodgers
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched the forum and nothing quite fit what I was looking for. This is pretty straight forward but I'm missing it. Too tired (or something).

I have a String (XML) that I cast to float (so I can use the absolute value method). Now I need to make that float a type of Float.



steveNumber is type float. How to make it Float? If there are other methods than the Math.abs to drop a (+ or -) sign I'll go straight from String to Float.

Thanks in advance.
[ February 18, 2005: Message edited by: Steve Rodgers ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Float f = new Float(steveNumber);

Once you've created a Float, you can't change its value, so I think this is basically your only choice.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Rodgers:
I have a String (XML) that I cast to float . . . . Now I need to make that float a type of Float.
First a word on terminology. Casting is a specific operation in Java where you change the reference type from one class/interface to another using put "(class/interface)" in front of the reference. It does not affect the object to which the reference points. What you're talking about in both cases is conversion.

Now, to convert a float to a Float, you simply create a new Float object from the float primitive:
 
Steve Rodgers
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for answering so quickly. I read a little more after my posting about Float versus float, etc... I needed the refresher.

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic