Java Find Minimum Sample Code

The Java Match Class utilizes four versions of Math.min() method to return the smaller of the two numbers being compared. The only difference between the method 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.min() method that includes the different primitive data types used as input and output.

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


Java Find Minimum Integer

This example will use the Integer version of the min() method. The method will take two integers and return an Integer that represents the smallest or minimum value between the two integers.

int jcdInteger1 = 910;
int jcdInteger2 = -890;	

System.out.println("\nMath Minimum Value (int): "+Math.min(jcdInteger1, jcdInteger2));


Java Find Minimum Double

This example will use the Double version of the min() method. The method will take two doubles and return a double that represents the smallest or minimum value between the two doubles.

double jcdDouble1 = 422.30;
double jcdDouble2 = -312.60;
		
System.out.println("\nMath Minimum Value (double): "+Math.min(jcdDouble1, jcdDouble2));


Java Find Minimum Float

This example will use the Float version of the min() method. This method will take two floats and return a float that represents the smallest or minimum value between the two floats.

float jcdFloat1 = 3.678921F;
float jcdFloat2 = -4.56734F;
		
System.out.println("\nMath Minimum Value (float): "+Math.min(jcdFloat1, jcdFloat2));


Java Find Minimum Long

This example will use the Long version of the min() method. The method will take two longs and return a type long that represents the smallest or minimum value between the two long variables.

long jcdLong1 = 465678901L;
long jcdLong2 = -236567819L;
		
System.out.println("\nMath Minimum Value (long): "+Math.min(jcdLong1, jcdLong2));


Java Find Minimum Source Code

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

package com.jcd.java.tutorials;

public class JavaMathMinimum 
{

	public static void main(String[] args) 
	{

		//Math Minimum Value Integer
		int jcdInteger1 = 910;
		int jcdInteger2 = -890;
		
		System.out.println("\nMath Minimum Value (int): "+Math.min(jcdInteger1, jcdInteger2));
		
		//Math Minimum Value Double
		double jcdDouble1 = 422.30;
		double jcdDouble2 = -312.60;
		
		System.out.println("\nMath Minimum Value (double): "+Math.min(jcdDouble1, jcdDouble2));
		
		//Math Minimum Value Float
		float jcdFloat1 = 3.678921F;
		float jcdFloat2 = -4.56734F;
		
		System.out.println("\nMath Minimum Value (float): "+Math.min(jcdFloat1, jcdFloat2));
		
		//Math Minimum Value Float
		long jcdLong1 = 465678901L;
		long jcdLong2 = -236567819L;
		
		System.out.println("\nMath Minimum Value (long): "+Math.min(jcdLong1, jcdLong2));

	}

}
Math Minimum Value (int): -890

Math Minimum Value (double): -312.6

Math Minimum Value (float): -4.56734

Math Minimum Value (long): -236567819



Java References:

Add a Comment

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