Home > Java > javaTutorial > body text

java min()

WBOY
Release: 2024-08-30 15:38:48
Original
705 people have browsed it

In Java, min() is an inbuilt method that returns the minimum value of two numbers. It is inherited from the package java.lang.math, and the arguments are taken in the types double, int, long and float. Moreover, this method can be overloaded, and there are certain conditions for implementing this method. It will be discussed in the section where the working is explained. In addition to that, syntax and examples of the min() method can be seen in the below sections.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax :

As already discussed, different data types such as int, float, double, and long can be used in this method. Following are the corresponding syntaxes of these different data types of method min().

public static int min(int num1, int num2)  //syntax of min with datatype int
Copy after login
public static long min(long num1, long num2)  //syntax of min with datatype long
Copy after login
public static float min(float num1, float num2)  //syntax of min with datatype float
Copy after login
public static double min(double num1, double num2)  //syntax of min with double
Copy after login

Parameters: num1 and num2 of different datatypes from which the minimum value among them is returned.

Return value: Minimum of two numbers passed as arguments will be returned, and the datatype of the result will be the same as the arguments.

How does the min() method work in Java?

1. If a negative number and a positive number are passed as the method’s arguments, then the result generated will be negative.

Example:If the numbers -32 and 21 are given as arguments, then -32 will be returned.

2. If both parameters passed as the method’s arguments are negative, then the result generated will be the one with a higher magnitude. That is, it will be closer to the –ve(negative) infinity.

Example: If the numbers -32 and -21 are given as arguments, then -32 will be returned.

3. If both parameters passed as the method’s arguments are the same, then the result generated will be that same value.

Example: If the numbers -32 and -32 are given as arguments, then -32 will be returned.

4. If NaN(Not a Number) is either value, the result generated will also be NaN.

Examples to Implement Java min() method

Below are the examples of the Java min() method:

Example #1

Java program to find the minimum of two int type positive numbers.

Code:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of <u>int</u> type
int x = 41;
int y = 67;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
Copy after login

Output:

java min()

In this program, two positive numbers, 41 and 67, are declared, and the minimum among them 41 is found using the min() method.

Example #2

Java program to find the minimum of two int type numbers where one is positive, and the other is negative.

Code:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of <u>int</u> type
int x = 41;
int y = -67;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
Copy after login

Output:

java min()

In this program, a positive number 41 and a negative number -67 is declared. The minimum among them, -67, which is closer to the negative infinity, is found using the min() method.

Example #3

Java program to find the minimum of two int type negative numbers.

Code:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of <u>int</u> type
int x = -41;
int y = -67;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
Copy after login

Output:

java min()

In this program, two negative numbers, -41 and -67, are declared. The minimum among them, -67, which is closer to the negative infinity, is found using the min() method.

Example #4

Java program to find the minimum of two double type positive numbers.

Code:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of double type
double x = 26.11;
double y = 26.12;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
Copy after login

Output:

java min()

Unlike the above programs, here, two positive numbers, 26.11 and 26.12, of double type are declared. But, the minimum among them, 26.11, is found using the min() method similar to the above programs.

Example #5

Java program to find the minimum of two float type positive numbers.

Code:

public class MinExample {
public static void main(String[] args) {
// Declare two numbers of float type
float x = 26.11f;
float y = 26.12f;
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
Copy after login

Output:

java min()

Here, two positive numbers, 26.11f and 26.12f of float type, are declared. The minimum among them, 26.11, is found using the min() method.

Example #6

Java program to find the minimum of two user input numbers.

Code:

import java.util.Scanner;
public class MinExample {
public static void main(String[] args) {
System.out.println("Enter two numbers from which the minimum has to be found: ");
//read input numbers from the user
Scanner in= new Scanner(System.in);
//store first number in x
int x = in.nextInt();
//store second number in y
int y = in.nextInt();
in.close();
// print the minimum number among x and y
System.out.println("Minimum among x="+x+" and y="+y+ " is: " + Math.min(x, y));
}
}
Copy after login

Output:

java min()

In this program, two numbers are asked to input by the user. As you can see, the numbers are given as 32 and 57, from which 32 is returned as the minimum number.

What will happen if two numbers are given the same?

java min()

It can be seen that the same number will be returned as a result.

The above is the detailed content of java min(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!