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: 9033
10
posted
0
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: 128
posted
0
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: 9033
10
posted
0
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.
...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.