I am following a book about teaching myself Java, and have this as an exercise: Create an array to store five objects that are of class Integer, String, Boolean, Character, and Double. Displaye the elements. Here is my program:
And here is the output:
7 Hello world! false x 6.0
How do I get the correct answer for the last element of the array?? I thought making it a double would make it an exact answer, i.e. 6.25 but all I can get as an output is 6.0. What's going on?
No, 25 / 4 has always made six. All you are getting from the double is adding a decimal point.
Go and find a table of precedences, and if you are lucky you will have this as the top row:
() . 
for round brackets (parentheses) dot for member access and square brackets for array access. So, whatever is in the () brackets is executed and completed first. What you have in there is the integer division expression 25 / 4. You already know that integer division loses fractions, so you get 6. When you create a Double object from 6, it can't put the 0.25 bit back, because it has disappeared into cyber-limbo, but there is a 1 somewhere (also in cyber-limbo) which represents the remainder of the division, ie 6 % 4.