Introduction
In this lab, you will learn about the min()
method of the Integer
class in Java. This method is used to return the numerically smaller value(minimum value) of the two numbers passed as arguments.
In this lab, you will learn about the min()
method of the Integer
class in Java. This method is used to return the numerically smaller value(minimum value) of the two numbers passed as arguments.
Open the terminal in your Ubuntu system, and create a Java project folder using the command given below:
mkdir project
Navigate to the project folder using the command given below:
cd project/
Create a Java code file using the command given below:
touch IntegerMin.java
min()
MethodThe min()
method is a static method of the Integer
class. This method is used to return the numerically smaller value (minimum value) of the two numbers passed as arguments. If a positive and a negative number is passed then the negative value is returned, but if both the numbers passed are negative, the higher magnitude value is returned.
The syntax of the min()
method is given below:
public static int min(int a, int b)
In the above syntax, the a
and b
are the parameters whose values need to be compared.
min()
MethodCopy the below Java code and paste it into the IntegerMin.java
file.
import java.util.Scanner;
public class IntegerMin {
public static void main(String[] args) {
try {
System.out.println("Enter two integer values: ");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
// Print the smaller number between a and b.
System.out.println("Smaller value: " + Integer.min(a, b));
} catch(Exception e) {
System.out.println("Exception occurred...");
}
}
}
In the above code, we are taking two integer values as input from the user and then comparing them using the min()
method. The smaller value is then printed as output.
To compile the IntegerMin.java
file, open a terminal and run the following command:
javac IntegerMin.java
To run the program, execute the following command:
java IntegerMin
Using a command-line text editor like nano
, open the IntegerMin.java
file, and replace the existing code with the following code block:
import java.lang.Integer;
import java.util.Scanner;
public class IntegerMin {
public static void main(String[] args) {
int x = 5485;
int y = -3242;
int z = -5645;
// print the smaller number between x and y.
System.out.println("Smaller value is " + Integer.min(x, y));
// print the smaller number between y and z.
System.out.println("Smaller value is " + Integer.min(y, z));
}
}
Save and close the file.
Again compile and run the program using the below commands:
javac IntegerMin.java
java IntegerMin
Upon running the program using the above command, the program will display the smaller value between the two integers, x
and y
, and then it will display the smaller value between the two integers, y
and z
.
Using a command-line text editor like nano
, open the IntegerMin.java
file, and replace the existing code with the following code block:
import java.util.Scanner;
public class IntegerMin {
public static void main(String[] args) {
try {
System.out.println("Enter two integer values: ");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
// Print the smaller number between a and b.
System.out.println("Smaller value: " + Integer.min(a, b));
} catch(Exception e) {
System.out.println("Exception occurred...");
}
}
}
Save and close the file.
Again compile and run the program using the below commands:
javac IntegerMin.java
java IntegerMin
Upon running the program using the above command, the program will prompt for two integer values. Enter two numeric values and hit enter. The program will display the smaller value between those two integers.
Using a command-line text editor like nano
, open the IntegerMin.java
file, and replace the existing code with the following code block:
import java.util.Scanner;
public class IntegerMin {
public static void main(String[] args) {
try {
System.out.println("Enter two integer values: ");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
// Print the smaller number between a and b.
System.out.println("Smaller value: " + Integer.min(a, b));
} catch(Exception e) {
System.out.println("Exception occurred...");
}
}
}
Save and close the file.
Again compile and run the program using the below commands:
javac IntegerMin.java
java IntegerMin
Upon running the program using the above command, the program will prompt for two integer values. Enter two integer values and hit enter. The program will display the smaller value between those two integers.
Using a command-line text editor like nano
, open the IntegerMin.java
file, and replace the existing code with the following code block:
import java.util.Scanner;
public class IntegerMin {
public static void main(String[] args) {
try {
int[] array = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter ten integer values: ");
for(int i=0; i<10; i++) {
array[i] = sc.nextInt();
}
int min = array[0];
for(int j=0; j<10; j++) {
if(min > array[j]) {
min = array[j];
}
}
System.out.println("Smaller value: " + min);
} catch(Exception e) {
System.out.println("Exception occurred...");
}
}
}
Save and close the file.
Again compile and run the program using the below commands:
javac IntegerMin.java
java IntegerMin
Upon running the program using the above command, the program will prompt for ten integer values. Enter ten integer values and hit enter. The program will display the smallest value among ten integers entered.
In this lab, you have learned:
min()
methodmin()
method of the Integer
classmin()
method in Java