java.lang.Double max(double a,double b)

Syntax:

public static double max(double a,double b)

Java Code Example :

This java example source code demonstrates the use of max(double a,double b) method of Double class.

Some takeaways on this method:

  • Returns the greater of two double values as if by calling Math.max.
  • since JDK 1.8
package com.javatutorialhq.java.examples;

import static java.lang.System.out;

/*
 * This example source code demonstrates the use of  
 * max(double a,double b) method of Double class
 */

public class DoubleMax {

	public static void main(String[] args) {

		Double val1 = 7.19;
		Double val2 = 0.0 / 0.0;
		Double val3 = 1200.0 / 0;
		Double val4 = -val1 / 0.0;
		Double val5 = -3.61;
		Double val6 = 0.0;

		/*
		 * test all the possible scenario on using the
		 * max(double a,double b) method
		 * Note, this only works using java 8
		 */

		out.println("Max Between "+val1+" and "+val2+ " = "
				+ Double.max(val1, val2));
		out.println("Max Between "+val1+" and "+val3+ " = "
				+ Double.max(val1, val3));
		out.println("Max Between "+val3+" and "+val4+ " = "
				+ Double.max(val3, val4));		
		out.println("Max Between "+val1+" and "+val5+ " = "
				+ Double.max(val1, val5));
		out.println("SMax Between "+val2+" and "+val6+ " = "
				+ Double.max(val2, val6));

		

	}
}

Sample Output :

Running the above example source code will give the following output

Max Between 7.19 and NaN = NaN
Max Between 7.19 and Infinity = Infinity
Max Between Infinity and -Infinity = Infinity
Max Between 7.19 and -3.61 = 7.19
SMax Between NaN and 0.0 = NaN

Suggested Reading List :

References :