aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes round() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "round()" Watch "round()" New topic
Author

round()

Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
When I use the round method in a println statement as follows:

...it compiles and runs.
I get a precision compile error when I try to define the integer value for the round method in a variable

....can someone explain why? I've reviewed the API and couldn't find explicit info on the overridden plus (+) operator.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
random() returns a double
round( double ) returns a long
You are trying to assign a long to an int.
Try

and it will work.

What overridden plus (+) operator are you referring to? The one used for String concatenation?

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
Marilyn, thanks for the info. I was coding with the blinders on.
For the println statement, I guess the + operator doesn't convert the math return value until that value is computed. And at that point, it is converted to a string. If the println statement was to read:

...then it should multiple the rounded # by 10 before converting it to a string for Output1 and for Output2 it will convert the rounded value to a string and then append the value of 10 as a string to the end. The problem I was having on the println portion was realizing that the type of operator precedence makes a difference in how the overridden + operator treats values after a string. If I'm off base here...please correct me.
Thanks.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
For the println statement, I guess the + operator doesn't convert the math return value until that value is computed. And at that point, it is converted to a string.

If the println statement was to read:

<pre>class Abstest {
public static void main(String[] args) {
System.out.println("Output1: " + Math.round(Math.random())*(10));
System.out.println("Output2: " + Math.round(Math.random())+(10));
}
}</pre>

...then it should multiply the rounded # by 10 before converting it to a string for Output1 and for Output2 it will convert the rounded value to a string and then append the value of 10 as a string to the end.


All this is true.

The problem I was having on the println portion was realizing that the type of operator precedence makes a difference in how the overridden + operator treats values after a string. If I'm off base here...please correct me.


"the type of operator precedence"? Perhaps I'm just too tired, but I'm not following you.
Java evaluates each expression between the '+' signs before it calls toString() on any of them. Then it goes left to right.

"Output1: " + Math.round(Math.random())*(10)
String + number.toString()

"Output2: " + Math.round(Math.random())+(10)
String + number.toString() + number.toString()

is not the same as

String + (number + number).toString()

In the second case the numbers are added together first because the parens have higher precedence.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: round()