Java Math max() method with Examples
The Java.lang.math.max() function is an inbuilt function in Java which returns maximum of two numbers. The arguments are taken in int, double, float and long.If a negative and a positive number is passed as argument then the positive result is generated. And if both parameters passed are negative then the number with the lower magnitude is generated as result.
Given below are the examples of the function max()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // Java program to demonstrate the use of max() function // when two double data-type numbers are // passed as arguments public class Gfg { public static void main(String args[]) { double a = 12.123; double b = 12.456; // prints the maximum of two numbers System.out.println(Math.max(a, b)); } } |
Output:
1 2 3 | 12.456 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // Java program to demonstrate the use of max() function // when one positive and one negative // integers are passed as argument public class Gfg { public static void main(String args[]) { int a = 23; int b = -23; // prints the maximum of two numbers System.out.println(Math.max(a, b)); } } |
Output:
1 2 3 | 23 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // Java program to demonstrate the use of max() function // when two negative integers are passed as argument. public class Gfg { public static void main(String args[]) { int a = -25; int b = -23; // prints the maximum of two numbers System.out.println(Math.max(a, b)); } } |
Output:
1 2 3 | -23 |
Java Math min() method with Examples
The Math.min(x,y)
method can be used to find the lowest value of of x and y:
1 2 3 4 5 6 7 | public class MyClass { public static void main(String[] args) { System.out.println(Math.min(5, 10)); } } |
Java Math sqrt() method with Examples
The Math.sqrt(x)
method returns the square root of x:
1 2 3 4 5 6 7 | public class MyClass { public static void main(String[] args) { System.out.println(Math.sqrt(64)); } } |
Java Math abs() method with Examples
The Math.abs(x)
method returns the absolute (positive) value of x:
1 2 3 4 5 6 7 | public class MyClass { public static void main(String[] args) { System.out.println(Math.abs(-4.7)); } } |
Java Math random() method with Examples
Math.random()
returns a random number between 0 (inclusive), and 1 (exclusive):
1 2 3 4 5 6 7 | public class MyClass { public static void main(String[] args) { System.out.println(Math.random()); } } |
Java Math multiplyFull() method with Examples
The multiplyFull(int x, int y) method of Math class is used to return the exact mathematical product of the two arguments. In the parameters, two integer values are provided and the product of both numbers expressed in long type returned by this method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // Java program to demonstrate // multiplyFull() method of Math class public class GFG { // Main method public static void main(String[] args) { // two Integer values int a = 367428, b = 1374; // apply multiplyFull method long c = Math.multiplyFull(a, b); // print result System.out.println(a + " * " + b + " = " + c); } } |
Java Math copySign() method with Examples
The java.lang.Math.copySign() method returns the first argument with the sign of the second argument.
Note:
Arguments can be of two types:
- double type : copySign(double magt, double sign)
- float type : copySign(float magt, float sign)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // Java program to demonstrate working // of java.lang.Math.copySign() method import java.lang.Math; class Gfg { // driver code public static void main(String args[]) { double a = 34.543; double b = -123.44; // Input a, b // Output -34.543( a- Magnitude, b- Sign) System.out.println(Math.copySign(a, b)); // Input b, a // Output 123.44( b- Magnitude, a- Sign) System.out.println(Math.copySign(b, a)); float c = 87.56f; float d = -685.23f; // Input c, d // Output -87.56( c- Magnitude, d- Sign) System.out.println(Math.copySign(c, d)); // Input d, c // Output 685.23( d- Magnitude, c- Sign) System.out.println(Math.copySign(d, c)); } } |