• We can find maximum of two numbers by using if condition check.
  • In java.lang.Math class also having a method Math.max() which will take two numbers and returns maximum number.
  • We have separate methods for int , float , double and long.




  1.  public static double max(double a, double b)

Java program to find maximum of two double values using Math.max() method.


  1. package com.mathmax;
  2. public class MathMAx {
  3.     
  4.     /**
  5.      * Math.max() method in java
  6.      * @author www.instanceofjava.com
  7.      */
  8.  
  9.   public static void main(String[] args) {
  10.         
  11.         
  12.         double a = 234567;
  13.         double b =234557;
  14.      
  15.         double x = 764554;
  16.         double y =764464;
  17.         
  18.         System.out.println("Math.max(" + a + "," + b + ")=" + Math.max(a, b));
  19.         System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y));
  20.         
  21.     }
  22.  
  23. }
Output:

  1. Math.pow(3.0,4.0)=81.0
  2. Math.pow(4.0,3.0)=64.0
  3. Math.pow(4,2)=16.0

Java program to find maximum of two float values using Math.max() method.



math.max method in java float


Java program to find maximum of two int values using Math.max() method.
 
  1. package com.mathmax;
  2. public class MathMAx {
  3.     
  4.     /**
  5.      * Math.max() method in java
  6.      * @author www.instanceofjava.com
  7.      */
  8.  
  9. public static void main(String[] args) {
  10.         
  11.         System.out.println( Math.max(1, 1));
  12.         System.out.println( Math.max(1, 2));
  13.         System.out.println(Math.max(1.222f, 1.223f));
  14.         System.out.println(Math.max(1.222, 1.223));
  15.         
  16.     }
  17.  
  18. }

Output:

  1. 1
  2. 2
  3. 1.223
  4. 1.223

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu