  Get Started: Java Math

### Java Math

Java Math has many methods that allow us to perform various mathematical tasks on numbers.

### Java Methods

#### abs() Method

Definition – abs() method returns the absolute (positive)  value of the number.

Syntax

abs() method has the following syntax –

```				```
public static double abs(double number)
public static float abs(float number)
public static int abs(int number)
public static long abs(long number)

```
```

Returns – A double, float, int or long value represents the absolute value of the specified number.

Parameter Values

Parameter – numbers

Description – A number that is required.

Example: Use the abs() method to return the numbers’ absolute (positive) value.

```				```
public class Main {
public static void main(String[] args) {
System.out.println(Math.abs(7.3));
System.out.println(Math.abs(-1.7));
System.out.println(Math.abs(9));
}
}

```
```

#### cbrt() Method

Definition – cbrt() method returns the cube root value of the number.

Syntax

```				```
public static int cbrt(int number)
```
```

Returns – A double value represents the cube root of the specified number.

Parameter Values

Parameter – number

Description – A number that is required.

Example: Use the cbrt() method to return the cube root of the following numbers.

```				```
public class Main {
public static void main(String[] args) {
System.out.println(Math.cbrt(27));
System.out.println(Math.cbrt(64));
System.out.println(Math.cbrt(125));
}
}

```
```

#### ceil() Method

Definition – ceil() method returns the rounded-up nearest integer value of the number.

Syntax

```				```
public static double ceil(double number)
```
```

Returns – A double value represents the rounded-up nearest integer of the specified number.

Parameter Values

Parameter – number

Description – A number that is required.

Example: Use the ceil() method to return the rounded-up nearest integer value of the following numbers.

```				```
public class Main {
public static void main(String[] args) {
System.out.println(Math.ceil(27.2));
System.out.println(Math.ceil(64.7));
System.out.println(Math.ceil(12.5));
}
}
```
```

#### floor() Method

Definition – floor() method returns the rounded-down nearest integer value of the number.

Syntax

```				```
public static double floor(double number)
```
```

Returns – A double value represents the rounded-down nearest integer of the specified number.

Parameter Values

Parameter – number

Description – A number that is required.

Example: Use the floor() method to return the rounded-down nearest integer value of the following numbers.

```				```
public class Main {
public static void main(String[] args) {
System.out.println(Math.floor(27.2));
System.out.println(Math.floor(64.7));
System.out.println(Math.floor(-12.5));
}
}
```
```

#### max() Method

Definition – max() method returns the max (highest) value from the parameter x and y.

Syntax

max() method has the following syntax –

```				```
public static double max(double number)
public static float max(float number)
public static int max(int number)
public static long max(long number)
```
```

Returns – A double, float, int, long value represents the max value of the specified number.

Parameter Values

Parameter – x & y and both are numbers.

Description – A number that is required.

Example: Use the max() method to return the highest value from x and y.

```				```
public class Main {
public static void main(String[] args) {
System.out.println(Math.max(10, 15));
System.out.println(Math.max(-8.6, 2));
System.out.println(Math.max(-2.9. -8));
}
}
```
```

#### min() Method

Definition – min() method returns the min (lowest) value from the parameter x and y.

Syntax

lowest() method has the following syntax –

```				```
public static double min(double number)
public static float min(float number)
public static int min(int number)
public static long min(long number)
```
```

Returns – A double, float, int, long value represents the min value of the specified number.

Parameter Values

Parameter – x & y and both are numbers.

Description – A number that is required.

Example: Use the min() method to return the lowest value from x and y.

```				```
public class Main {
public static void main(String[] args) {
System.out.println(Math.min(10, 15));
System.out.println(Math.min(-8.6, 2));
System.out.println(Math.min(-2.9. -8));
}
}
```
```

#### pow() Method

Definition – pow() method returns the value of x to the power of y.

Syntax

```				```
public static int pow(int number)
```
```

Returns – An int value represents the power of x to the value of y.

Parameter Values

Parameter – x & y and both are numbers.

Description – A number that is required.

Example: Use the pow() method to return the value of x to the power of y.

```				```
public class Main {
public static void main(String[] args) {
System.out.println(Math.pow(10, 2));
System.out.println(Math.pow(5, 3));
System.out.println(Math.min(-20, 2));
}
}
```
```

#### random() Method

Definition – random() method returns the random between 0 (included) and 1 (excluded).

Syntax

```				```
public static random()
```
```

Returns – A value represents the random number between 0 and 1.

Parameter Values

Parameter – none

Example: Use the random() method to return the value between 0 and 1.

```				```
public class Main {
public static void main(String[] args) {
System.out.println(Math.random());
System.out.println(Math.random());
System.out.println(Math.random());
}
}

```
```

#### round() Method

Definition – round() method returns the value of x to its nearest integer.

Syntax

```				```
public static int random(int number)
public static float random(float number)
public static double random(double number)
```
```

Returns – A int, float, double value represents the round nearest number to the value of x.

Parameter Values

Parameter – number

Description – A number that is required.

Example: Use the round() method to return the value of x to its nearest integer.

```				```
public class Main {
public static void main(String[] args) {
System.out.println(Math.round(3.2));
System.out.println(Math.round(-5.12));
System.out.println(Math.round(5));
}
}
```
```

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Share on