Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
naresh vyas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just fyi - the Math class is no longer a topic in the exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic