Java Math max method
DevSniper
129 Views
public static int max(int a, int b) public static double max(double a, double b) public static long max(long a, long b) public static float max(float a, float b)
Java Math.max is a inbuild static method. it returns greatest value of given two arguments. It is present in Java.lang.Math. Java.lang.Math provides set of methods to perform mathematical operations.
Java Math.max(int , int)
public class Program{ public static void main(String []args){ int x=5; int y=10; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); x=5; y=-10; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); x=-5; y=-10; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); } }
Max value of 5 and 10 is = 10 Max value of 5 and -10 is = 5 Max value of -5 and -10 is = -5
Java Math.max(double , double)
public class Program{ public static void main(String []args){ double x=9.25; double y=9.50; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); y=-y; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); x=-x; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); } }
Max value of 9.25 and 9.5 is = 9.5 Max value of 9.25 and -9.5 is = 9.25 Max value of -9.25 and -9.5 is = -9.25
Java Math.max(long , long)
public class Program{ public static void main(String []args){ long x=10000000000L; long y=10000000002L; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); y=-y; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); x=-x; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); } }
Max value of 10000000000 and 10000000002 is = 10000000002 Max value of 10000000000 and -10000000002 is = 10000000000 Max value of -10000000000 and -10000000002 is = -10000000000
Java Math.max(float , float)
public class Program{ public static void main(String []args){ float x=9f; float y=10f; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); y=-y; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); x=-x; System.out.println("Max value of "+x+" and "+y+" is = "+ Math.max(x,y)); } }
Max value of 9.0 and 10.0 is = 10.0 Max value of 9.0 and -10.0 is = 9.0 Max value of -9.0 and -10.0 is = -9.0