# JAVA Tutorial II – Data Types (Part IV – Double)

What’s A Double?

Doubles are variables that have an increased storage capacity. They use up twice as much memory as a float, but our computer being the way they are, it really doesn’t make much of a difference. When performing functions such as Logarithms and Square Roots, you have to use a Double because Floats and Ints generally aren’t capable to hold that amount of data.

What’s In Store?

We’ll take a good look at what would be “cmath” in C++. We’ll construct the program for Square Roots, but I’ll give you the functions for changing it to compute Logarithms, Cube Roots, Powers…

Code for Square Roots

Here is the code you’d need for the square roots.

import java.util.Scanner;public class Double {</p>

public static void main(String[] args) {

//Collecting the Data Here

System.out.println(“Enter a number you’d like the square root of “);

Scanner scan = new Scanner(System.in);

double x = scan.nextDouble();

//Actual Square Root

System.out.print(“The Square Root of ” + x + ” is ” + answer);

}

}</td> </tr> </tbody> </table>

And here is the color code:

scan.nextDouble();

If you have noticed, you change this function slightly depending on the data type you are using. We have seen Ints and Floats in place of the Double.

java.lang.Math.sqrt();

This function completes the mathematics function. You place the variable you want to use in the parenthesis. In the code, we used the variable from the Scanner input. Here is a table containing different and widely used java.lang.Math.insertfunctionhere();

 Function Name Code of the Function Exponentiation double x = java.lang.Math.pow(insertbasehere, insertpowerhere); Cube Root double x = java.lang.Math.cbrt(cubednumber); Logarithm double x = java.lang.Math.log(insertdesirednumberhere)/Math.log(insertlognumberhere); Absolute Value int x = java.lang.Math.abs(insertnumberhere); Square Root double x = java.lang.Math.sqrt(insertnumberhere); Show the Minimum of 2 Numbers double answer = java.lang.Math.min(number1, number2); Show the Maximum of 2 Numbers double answer = java.lang.Math.max(number1, number2); Convert Radians to Degrees double answer = java.lang.Math.toDegrees(insertradianshere); Convert Degrees to Radians double answer = java.lang.Math.toRadians(insertradianshere);

So now you have a taste of Double. Since most computers can utilize large amounts of doubles, most programmers like to use doubles instead of float because of the accuracy and the large numbers that can be held on them.

Now that you’ve “doubled” your knowledge (I know, I’m hilarious), let’s go onto char

Tags:

Categories:

Updated: