• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

round()

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Mike Cunningham
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic