Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
math
min
communitycreator

What is Math.min() in Java?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The min() function returns the smallest value among the two numbers sent as a parameter.

Figure 1 shows a visual representation of the min() function.

Figure 1: Visual representation of min() function

Syntax

type min(type num-1, type num-2)

Parameter

The min() function takes two numbers as a parameter.

The numbers can be int, double, float, or long.

Return value

The min() function returns the smallest value from the two numbers sent as a parameter.

Code

class JAVA {
public static void main( String args[] ) {
// two positive numbers
System.out.println("Minimum between 9 and 8:");
System.out.println(Math.min(9,8));
//one positive and the other negative
System.out.println("Minimum between -9 and 8:");
System.out.println(Math.min(-9,8));
// both negative numbers
System.out.println("Minimum between -9 and -10:");
System.out.println(Math.min(-9,-10));
// both double numbers
System.out.println("Minimum between 12.234 and 1.2345:");
System.out.println(Math.min(12.234,1.2345));
// one int and the other double
System.out.println("Minimum between 12.234 and 1:");
System.out.println(Math.min(12.234,1));
}
}

RELATED TAGS

java
math
min
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring