1. Member Join Date
Jan 2013
Location
New Zealand
Posts
10
Rep Power
0

## 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));
}
}```  Reply With Quote

2. Senior Member Join Date
Feb 2012
Posts
219
Rep Power
9

## 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  Reply With Quote

3. Member Join Date
Jan 2013
Location
New Zealand
Posts
10
Rep Power
0

## 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.  Reply With Quote

4. Senior Member Join Date
Feb 2012
Posts
219
Rep Power
9

## Re: int/double

Math.pow

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

5. Member Join Date
Jan 2013
Location
New Zealand
Posts
10
Rep Power
0

## 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);
}
}```  Reply With Quote

6. Senior Member Join Date
Feb 2012
Posts
219
Rep Power
9

## 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;```  Reply With Quote

#### Posting Permissions

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