Java min()

WBOY
リリース: 2024-08-30 15:38:48
オリジナル
477 人が閲覧しました

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
ログイン後にコピー
public static long min(long num1, long num2) //syntax of min with datatype long
ログイン後にコピー
public static float min(float num1, float num2) //syntax of min with datatype float
ログイン後にコピー
public static double min(double num1, double num2) //syntax of min with double
ログイン後にコピー

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 int 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)); } }
ログイン後にコピー

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 int 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)); } }
ログイン後にコピー

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 int 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)); } }
ログイン後にコピー

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)); } }
ログイン後にコピー

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)); } }
ログイン後にコピー

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)); } }
ログイン後にコピー

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.

以上がJava min()の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!