Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2007, 09:24 PM
Member
 
Join Date: Nov 2007
Posts: 4
naxalyte is on a distinguished road
What does this line of code mean?
Could someone please explain why in the following line of code, int is used within brackets after the equals sign?

Code:
int x = (int)((width/2) + (0.5-Math.random())*width);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-23-2007, 10:28 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
((width/2) + (0.5-Math.random())*width)
Math.random returns a double — we know this is true because we can look up the method in the Math class api and see the return type specified in the first column of the table in the Methods Summary section.
So the expression
(0.5-Math.random())*width)
will evaluate to a double.
For precision the (width/2) term, which may be an int, is grouped into the same expression and the whole thing is cast to an int.
int x = the value over here must be an int so we cast the double expression to the lesser–precision int primitive data type. Going from higher to lower precision requires an explicit cast.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help fixing my code.. or non code? MrHuggykins New To Java 1 03-19-2008 11:12 PM
java in cmd line :( Joe2003 New To Java 2 02-01-2008 09:51 AM
line feed in CSV javaplus New To Java 2 01-24-2008 04:46 PM
Reading in data from file line by line bluekswing New To Java 1 10-02-2007 01:19 AM
Generating Code Automatically Using Custom code Template In Eclipse JavaForums Eclipse 1 04-26-2007 04:52 PM


All times are GMT +3. The time now is 11:44 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org