# Thread: What's the difference in output of these two statements?

1. Member
Join Date
Jan 2011
Posts
2
Rep Power
0

## What's the difference in output of these two statements?

Hi,
I'm really quite new to java and have had a little bit of a problem understanding the difference in output (if any) between these two statements.

public void flip ()
{
face = ((int)(Math.random() * 6)) + 1;
}

and

public void flip ()
{
face = (int) (Math.random() * 6) + 1;
}

As can be seen all that differs is a single set of brackets however, what difference does this make to the output?

Thank you.
Josh

2. Originally Posted by JoshuaNathan
I'm really quite new to java and have had a little bit of a problem understanding the difference in output (if any) between these two statements.

public void flip ()
{
face = ((int)(Math.random() * 6)) + 1;
}

and

public void flip ()
{
face = (int) (Math.random() * 6) + 1;
}

As can be seen all that differs is a single set of brackets however, what difference does this make to the output?
Edit: sorry, I didn't look carefully enough. In both expressions the expression Math.random()*6 is cast to an int value and one is added to the result.

kind regards,

Jos
Last edited by JosAH; 01-05-2011 at 03:36 PM.

3. Member
Join Date
Jan 2011
Posts
2
Rep Power
0

## Thank you.

Thank you for the information, it makes sense looking at it in hind-sight.
One more question if you don't mind answering it.
As part of the test question i've called the dice class in another class that rolls it (which is all fine). However, we were told to do so editing an existing program which i have done ( and it all works well). Whilst editing it i noticed that they code it as follows:

public boolean isTwo ()
{
return (face == 2);
}

public boolean isThree ()
{
return (face == 3);
}

Is there an easier way of coding this so that instead of having to write out each individual number as shown above i could for example have a 20 sided dice which could be cast and give a string value without having to write out each method for each number?

4. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
3
Originally Posted by JoshuaNathan
Hi,
I'm really quite new to java and have had a little bit of a problem understanding the difference in output (if any) between these two statements.

public void flip ()
{
face = ((int)(Math.random() * 6)) + 1;
}

and

public void flip ()
{
face = (int) (Math.random() * 6) + 1;
}

As can be seen all that differs is a single set of brackets however, what difference does this make to the output?

Thank you.
Josh
why didn't you try them out? first get rid of the Maths.random(), insert a number and see the results.

Java Code:
```public class Flip {
public static void main(String[] args){
flip() ;
flip1();
}
public static void flip1 () {
int face = ((int)(0.5 * 6)) + 1;
System.out.println("Flip1: " + face);
}
public static void flip ()
{
int face = (int) ( 0.5 * 6) + 1;
System.out.println( "Flip: " + face );
}
}```

5. Moderator
Join Date
Apr 2009
Posts
10,484
Rep Power
16
They'll give the same result.
Both have the following in:
Java Code:
`(int)(Math.random() * 6);`
Which does the cast after the random has been multiplied by 6.

The only difference is that there's a set of extra brackets around that bit, which will probably be ignored by the compiler.

6. Moderator
Join Date
Apr 2009
Posts
10,484
Rep Power
16
Originally Posted by JoshuaNathan
Thank you for the information, it makes sense looking at it in hind-sight.
One more question if you don't mind answering it.
As part of the test question i've called the dice class in another class that rolls it (which is all fine). However, we were told to do so editing an existing program which i have done ( and it all works well). Whilst editing it i noticed that they code it as follows:

public boolean isTwo ()
{
return (face == 2);
}

public boolean isThree ()
{
return (face == 3);
}

Is there an easier way of coding this so that instead of having to write out each individual number as shown above i could for example have a 20 sided dice which could be cast and give a string value without having to write out each method for each number?
Depends what you want to do with those methods. The devil is in the details, but a simple option would be an isNumber(int) method, which simply checks the parameter against the face. But I have no idea whether that would be good or bad in your particular case.

7. Originally Posted by Tolls
The only difference is that there's a set of extra brackets around that bit, which will probably be ignored by the compiler.
Effectively yes, those parentheses are ignored. Technically no, that stupid compiler generates a ParenthesizedTree for every pair of parentheses around expressions in its AST; I tried to make it crash because of this but failed so far ;-)

kind regards,

Jos

8. Moderator
Join Date
Apr 2009
Posts
10,484
Rep Power
16
Yes, but is it ignored in the final byte code?
I really don't care what it does while it's compiling...the longer it takes the more chance I have for a cup of tea.

9. Originally Posted by Tolls
Yes, but is it ignored in the final byte code?
I really don't care what it does while it's compiling...the longer it takes the more chance I have for a cup of tea.
It is completely gone in the final byte code; I do care what's happening during the compilation of it all though ;-) b.t.w. if you spread your expression over several lines and generate debug code the final byte code does differ; i.e. if you do this:

int a= (
1+2
);

byte code will differ (because of those line directives).

kind regards,

Jos

10. Moderator
Join Date
Apr 2009
Posts
10,484
Rep Power
16
That's debugging...that doesn't count.
;)

11. Originally Posted by Tolls
That's debugging...that doesn't count.
;)
I want to earn a penny from every person who compiles their classes for distribution with debug code in it ;-)

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•