Java Integer Min Method

JavaJavaBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117722{{"`Java Integer Min Method`"}} java/classes_objects -.-> lab-117722{{"`Java Integer Min Method`"}} java/class_methods -.-> lab-117722{{"`Java Integer Min Method`"}} java/exceptions -.-> lab-117722{{"`Java Integer Min Method`"}} java/modifiers -.-> lab-117722{{"`Java Integer Min Method`"}} java/oop -.-> lab-117722{{"`Java Integer Min Method`"}} java/packages_api -.-> lab-117722{{"`Java Integer Min Method`"}} java/user_input -.-> lab-117722{{"`Java Integer Min Method`"}} java/wrapper_classes -.-> lab-117722{{"`Java Integer Min Method`"}} java/identifier -.-> lab-117722{{"`Java Integer Min Method`"}} java/arrays -.-> lab-117722{{"`Java Integer Min Method`"}} java/comments -.-> lab-117722{{"`Java Integer Min Method`"}} java/data_types -.-> lab-117722{{"`Java Integer Min Method`"}} java/for_loop -.-> lab-117722{{"`Java Integer Min Method`"}} java/if_else -.-> lab-117722{{"`Java Integer Min Method`"}} java/operators -.-> lab-117722{{"`Java Integer Min Method`"}} java/output -.-> lab-117722{{"`Java Integer Min Method`"}} java/strings -.-> lab-117722{{"`Java Integer Min Method`"}} java/variables -.-> lab-117722{{"`Java Integer Min Method`"}} java/system_methods -.-> lab-117722{{"`Java Integer Min Method`"}} end

Setting up the Environment

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

Understanding the min() Method

The 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.

Implementing the min() Method

Copy 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.

Compiling and Running the Program

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

Test the Program with Example 2

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.

Testing the Program with Live Example

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.

Editing the Program

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.

Improving the Program

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.

Summary

In this lab, you have learned:

  • To set up the environment to implement Java min() method
  • The syntax and usage of min() method of the Integer class
  • To implement the min() method in Java
  • To compile and run Java code from the terminal
  • To test the implementation with different examples

Other Java Tutorials you may like