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

round method

sarathy rambha
Greenhorn

Joined: Mar 26, 2003
Posts: 2
Hi,
I see compilation while compiling below program. But since round(float) returns int, I am not sure why it thinks return type is double.

float ddd = 3.5;
int j = Math.round(ddd);


Math1.java:84: possible loss of precision
found : double
required: float
float ddd = 3.5;
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42031
    
  64
The message is talking about the first line of code, not the second. "3.5" is a double, and it gets assigned to a float.


Ping & DNS - my free Android networking tools app
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

The Math class is not part of the exam by the way. As for the error Ulf is right, its on the first line


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

For float literals, you need to append a letter f to the number, like this:

float ddd = 3.5f;


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
GeeCON Prague 2014
 
subject: round method