GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Float.valueOf() method and NumberFormatException 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 "Float.valueOf() method and NumberFormatException" Watch "Float.valueOf() method and NumberFormatException" New topic
Author

Float.valueOf() method and NumberFormatException

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066



We don't need to put the post-fix f in the 3rd line. Why is that, What's really happening?

Thanks in Advanced!

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

Below are the lines pasted from java doc

"Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. In general, the two-step sequence of conversions, string to double followed by double to float, is not equivalent to converting a string directly to float. For example, if first converted to an intermediate double and then to float, the string
"1.00000017881393421514957253748434595763683319091796875001d"
results in the float value 1.0000002f; if the string is converted directly to float, 1.0000001f results."


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks ramesh maredu, So the compiler won't check whether the String literal that we passed to the method has post-fixed "f". It convert the String to that Wrapper class object, on which the method being invoked!

Please Confirm!

Thanks in Advanced!

BTW, Why don't I get Notification E-mail for the "Watching Posts"?
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Perhaps you can help me help you (I'm also studying for SCJP 6)

Chapter 3 of Sierra/Bates says:

Float f2 = Float.valueOf("3.14f"); // assigns 3.14 to the Float object f2


A page or two later, the text says:

Float f2 = new Float(3.14f); // make a new wrapper object
short s = f2.shortValue(); // convert f2's value to a short
System.out.println(s); // result is 3 (truncated, not // rounded)


I tried the following code snippit:



And it outputs:

Float.valueOf("3.14f") = 3.14
Float.valueOf(3.14f) = 3.14
Float.valueOf("3.14") = 3.14



However, if I add the following code:




I get a compiler error:

cannot find symbol
symbol : method valueOf(double)
location: class java.lang.Float
Float f5 = Float.valueOf(3.14);
^
1 error



I'm really not sure why this is happening....


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Rene Argento
Greenhorn

Joined: Jun 09, 2010
Posts: 10
Sandra, from what I understood, on the f5 variable you are passing a double parameter for the Float.valueOf() method, while it does not support it.


SCJA | SCJP6 | OCE SQL | OCA DBA 10g
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 444
    
    1

no this is not the case
this is because ValueOf method takes the String as the argument and not others
so


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Rene Argento
Greenhorn

Joined: Jun 09, 2010
Posts: 10
That's what I said. The Float.valueOf() method accepts either a float or a String parameter though.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 444
    
    1

yeah
Rene
nice work
I missed it
Happy preparation
Rene Argento
Greenhorn

Joined: Jun 09, 2010
Posts: 10
Thanks
But I still have a long way to go before getting ready for SCJP..
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Abimaran Kugathasan wrote:

We don't need to put the post-fix f in the 3rd line. Why is that, What's really happening?

Thanks in Advanced!


I think, here, we are using name reference Float, so the method valueOf() takes a String arguments and convert it to the named class, So no need of postfix "f" at the end. it's redundant!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Abimaran Kugathasan wrote:
We don't need to put the post-fix f in the 3rd line. Why is that, What's really happening?


I assume you folks already got the point. Just clear up the things. There in the third line, whether you put a f or not it wouldn't change the type of argument you are passing. That would still be a string. The whole point of passing a f is to make the number float, otherwise a decimal literal is of double type. And there is no method like, Float.valueOf(double), exists.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Adeel Ansari wrote:
Abimaran Kugathasan wrote:
We don't need to put the post-fix f in the 3rd line. Why is that, What's really happening?


I assume you folks already got the point. Just clear up the things. There in the third line, whether you put a f or not it wouldn't change the type of argument you are passing. That would still be a string. The whole point of passing a f is to make the number float, otherwise a decimal literal is of double type. And there is no method like, Float.valueOf(double), exists.


Exactly.

When I post this question, I've this doubt, but I cleared it.

Thanks to All......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Float.valueOf() method and NumberFormatException