File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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

naresh vyas

Joined: Aug 24, 2012
Posts: 2
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.

James Boswell

Joined: Nov 09, 2011
Posts: 1051

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

Welcome to 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

Joined: Oct 14, 2002
Posts: 8898
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:
subject: java.lang.Math floor() and ceil()
jQuery in Action, 3rd edition