Results 1 to 11 of 11
- 01-05-2011, 03:25 PM #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
- 01-05-2011, 03:30 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Last edited by JosAH; 01-05-2011 at 03:36 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-05-2011, 03:35 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?
- 01-05-2011, 03:41 PM #4
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
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 ); } }
- 01-05-2011, 03:42 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
They'll give the same result.
Both have the following in:
Which does the cast after the random has been multiplied by 6.Java Code:(int)(Math.random() * 6);
The only difference is that there's a set of extra brackets around that bit, which will probably be ignored by the compiler.
- 01-05-2011, 03:45 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 01-05-2011, 03:47 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-05-2011, 03:54 PM #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.
- 01-05-2011, 03:57 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-05-2011, 04:03 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
That's debugging...that doesn't count.
;)
- 01-05-2011, 04:13 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,588
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Need help with the output statements
By jayti in forum New To JavaReplies: 9Last Post: 11-26-2010, 04:06 PM -
how to get the resultset output into an output file
By renu in forum New To JavaReplies: 0Last Post: 09-30-2010, 08:16 PM -
if else if statements
By Allspark in forum New To JavaReplies: 9Last Post: 09-28-2010, 06:50 PM -
Help with if-else statements
By porchrat in forum New To JavaReplies: 4Last Post: 03-23-2009, 04:24 PM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks