aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes float or double 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 or double" Watch "float or double" New topic
Author

float or double

Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
Hi,
when 1.0 is passed as parameter to abs method Math.abs(1.0), is 1.0 considered as float value or a double value.
thanks in adv
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
1.0 is by default a double literal. So the method Math.abs(double) will be chosen. And you will get a double as the returned value.
So float f = Math.abs(1.0) will not compile without casting the returned value to a float.
After saying that I better go try it to see if I'm correct.
-Seey you later.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
double
To pass a float, you would have to use 1.0f
Cheers
Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
no no...it will compile bcoz abs() method is overloaded and one of which takes double N returns double value.
Thankz for replying.....
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yep it's gottabe: float f = (float)Math.abs(1.0);
Have a happy new Year.
Ben Ritchie
Ranch Hand

Joined: Nov 18, 2002
Posts: 98
or float f = Math.abs(1.0f);
I don't like casting primitives unless strictly necessary
[ December 20, 2002: Message edited by: Ben Ritchie ]

SCJP1.4, SCJD, SCEA (in progress)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: float or double