Home > Tutorial > Java Find Maximum Sample Code

Java Find Maximum Sample Code

The Java Match Class utilizes multiple versions of the Math.max() method in order to determine the larger or maximum of the two numbers being compared. The main difference between the method versions is the data type of the argument and the data type of the return type.

The table below shows the four versions of the Math.max() method that includes the different primitive data types used as input and output.

Method Argument Return Type
max() int, int int
max() double, double double
max() float, float float
max() long, long long


Java Find Maximum Integer

This example will use the Integer version of the max() method. The method will take two Integers and return an integer that represents the largest or maximum value between the two integer variables.

int jcdInteger1 = 2910;
int jcdInteger2 = -1890;
		
System.out.println("\nMath Maximum Value (int): "+Math.max(jcdInteger1, jcdInteger2));


Java Find Maximum Double

This example will use the Double version of the max() method. The method will take two Doubles and return a double that holds the largest or maximum value between the two doubles.

double jcdDouble1 = 672.30;
double jcdDouble2 = -532.60;
		
System.out.println("\nMath Maximum Value (double): "+Math.max(jcdDouble1, jcdDouble2));


Java Find Maximum Float

This example will use the Float version of the max() method. The method will take two Floats and return afloat data type that represents the largest or maximum value between the two float arguments.

float jcdFloat1 = 2.118921F;
float jcdFloat2 = -3.32734F;
		
System.out.println("\nMath Maximum Value (float): "+Math.max(jcdFloat1, jcdFloat2));


Java Find Maximum Long

This example will use the Long version of the max() method. The method will take two Longs and return a long that holds the largest or maximum value between the two long arguments.

long jcdLong1 = 234678901L;
long jcdLong2 = -875417819L;
		
System.out.println("\nMath Maximum Value (long): "+Math.max(jcdLong1, jcdLong2));


Java Find Maximum Source Code

The example source code below is the full class used to create this Java tutorial on Math.max() method.

package com.jcd.java.tutorials;

public class JavaMathMaximum 
{

	public static void main(String[] args) 
	{

		//Math Maximum Value Integer
		int jcdInteger1 = 2910;
		int jcdInteger2 = -1890;
		
		System.out.println("\nMath Maximum Value (int): "+Math.max(jcdInteger1, jcdInteger2));
		
		//Math Maximum Value Double
		double jcdDouble1 = 672.30;
		double jcdDouble2 = -532.60;
		
		System.out.println("\nMath Maximum Value (double): "+Math.max(jcdDouble1, jcdDouble2));
		
		//Math Maximum Value Float
		float jcdFloat1 = 2.118921F;
		float jcdFloat2 = -3.32734F;
		
		System.out.println("\nMath Maximum Value (float): "+Math.max(jcdFloat1, jcdFloat2));
		
		//Math Maximum Value Float
		long jcdLong1 = 234678901L;
		long jcdLong2 = -875417819L;
		
		System.out.println("\nMath Maximum Value (long): "+Math.max(jcdLong1, jcdLong2));

	}

}

Math Maximum Value (int): 2910

Math Maximum Value (double): 672.3

Math Maximum Value (float): 2.118921

Math Maximum Value (long): 234678901



Java References:

Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot