Results 1 to 6 of 6

Thread: int/double

  1. #1
    Droid_X is offline Member
    Join Date
    Jan 2013
    Location
    New Zealand
    Posts
    10
    Rep Power
    0

    Default int/double

    What is the difference? If I use this to calculate the square root of a number using Math.pow(), I get a decimal. I am guessing it has to do with int/double.

    Java Code:
    class hi{
    	public static double squareRoot(int a){
    		return Math.pow(a, 0.5);
    	}
    	public static void main(String args[]){
    		System.out.println(squareRoot(16));
    	}
    }

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: int/double

    Data Types

    Basically, a double is a floating point number i.e: 2.3
    An int is a whole number integer i.e: 5

  3. #3
    Droid_X is offline Member
    Join Date
    Jan 2013
    Location
    New Zealand
    Posts
    10
    Rep Power
    0

    Default Re: int/double

    Ah ok.

    So how would I make it return an int?

    If I replace this:

    Java Code:
    public static double squareRoot(int a)
    with:

    Java Code:
    public static int squareRoot(int a)

    I get an error.

  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: int/double

    Math.pow

    See if you can figure out why it won't let you return an int.

  5. #5
    Droid_X is offline Member
    Join Date
    Jan 2013
    Location
    New Zealand
    Posts
    10
    Rep Power
    0

    Default Re: int/double

    Oh, Math.pow only takes a double. I just converted the double to an int.

    Java Code:
    class apples{
    	public static double squareRoot(double a){
    		return Math.pow(a, 0.5);
    	}
    	public static void main(String args[]){
    		int num = (int)squareRoot(16);
    		System.out.println(num);
    	}
    }

  6. #6
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: int/double

    Just a small correction, it returns a double. You can convert an int to a double easily, the reverse is not as easy. That is why it lets you give it an int as an option because it converts it to an integer.

    Java Code:
    		double a = 5.4;
    		int b = (int)a; 
    		
    		int c = 6;
    		double d = c;

Similar Threads

  1. Replies: 4
    Last Post: 05-10-2012, 08:03 AM
  2. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  3. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 03:51 PM
  4. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

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