Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By jim829

Thread: Need help understanding how to use java data types such as int and doubles

  1. #1
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    0

    Default Need help understanding how to use java data types such as int and doubles

    Here's an example:

    package rootscan;
    public class Rootscan
    {
    public static void main(String[] args)
    {
    int x1
    double x1root;

    x1 = 2;
    x1root = Math.sqrt(x1);
    System.out.println(" the root of " + x1 + " is " + x1root);

    I know that double has a size of 8 bytes or 64 bits but why did we use double in particular?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,934
    Rep Power
    4

    Default Re: Need help understanding how to use java data types such as int and doubles

    Because it has greater precision than float which is only 32 bits wide. For arbitrary
    precision you can use BigDecimal and its supporting methods.

    Regards,
    Jim
    csanch11 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    csanch11 is offline Member
    Join Date
    Oct 2013
    Posts
    33
    Rep Power
    0

    Default Re: Need help understanding how to use java data types such as int and doubles

    I think I kind of get it now, thanks!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Need help understanding how to use java data types such as int and doubles

    It's also because that's what the sqrt() method returns, and Java will put up a warning about loss of precision if you were to stick the result in a float.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Java Data Types ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 04:43 PM
  2. Understanding limitations of int data-type
    By JONCOM in forum New To Java
    Replies: 11
    Last Post: 01-30-2011, 09:57 PM
  3. primitive Data types
    By Manfizy in forum New To Java
    Replies: 2
    Last Post: 07-07-2009, 08:29 PM
  4. Primitive data types of Java
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 07:29 PM

Posting Permissions

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