aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java.lang.Math floor() and ceil() 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 "java.lang.Math floor() and ceil()" Watch "java.lang.Math floor() and ceil()" New topic
Author

java.lang.Math floor() and ceil()

naresh vyas
Greenhorn

Joined: Aug 24, 2012
Posts: 2
hi,
i'm new to javaRanch I have problem understanding this code, the code is as follows:
class SRC119 {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.floor(1.0f)) + m1(Math.floor(1.0d)));
System.out.print(m1(Math.ceil(1.0f)) + m1(Math.ceil(1.0d)));
}}

answer: DDDD

what is exactly happening in this code, floor() method takes the double value but here the float type
value is being passed but still the answer is coming out,
can anyone explain this code to me.

thanks,
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Easy. A copy of the float literal is given to a double. As a double can easily hold a float value, no explicit cast is necessary. As floor and ceil always return a double, the appropriate m1 method is always called.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Welcome to www.javaranch.com. we have something called "code tags" for posting code snippets which formats your code and improves readibility vastly. to use code tags , just write down your code , select it and click on Code button above. this will present your question in a nice readable form , which will help you as well as other ranchers . Regards
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
just fyi - the Math class is no longer a topic in the exam.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.Math floor() and ceil()