Java Integer Compare Method

JavaJavaBeginner
Practice Now

Introduction

In Java, Integer is a wrapper class that encapsulates a primitive data type int. Integer class provides various useful methods to perform operations on integer values. One of the methods is compare() which is used to compare two int values.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL 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/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") 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/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/class_methods -.-> lab-117698{{"`Java Integer Compare Method`"}} java/modifiers -.-> lab-117698{{"`Java Integer Compare Method`"}} java/wrapper_classes -.-> lab-117698{{"`Java Integer Compare Method`"}} java/identifier -.-> lab-117698{{"`Java Integer Compare Method`"}} java/arrays -.-> lab-117698{{"`Java Integer Compare Method`"}} java/comments -.-> lab-117698{{"`Java Integer Compare Method`"}} java/data_types -.-> lab-117698{{"`Java Integer Compare Method`"}} java/operators -.-> lab-117698{{"`Java Integer Compare Method`"}} java/output -.-> lab-117698{{"`Java Integer Compare Method`"}} java/strings -.-> lab-117698{{"`Java Integer Compare Method`"}} java/variables -.-> lab-117698{{"`Java Integer Compare Method`"}} java/system_methods -.-> lab-117698{{"`Java Integer Compare Method`"}} end

Define main method

In the IntegerCompareMethod.java file, define the main() method, which is the entry point to the Java program.

public static void main(String[] args) {
    // code goes here
}

Call compare() method

In the main() method, call the compare() method with two int values as parameters. Store the result in a variable named result.

int val1 = 5;
int val2 = 10;
int result = Integer.compare(val1, val2);

Print the result

Print the value of the result variable using the System.out.println() method.

System.out.println("The result is: " + result);

Test the comparison

Test the comparison by running the code. The output should be -1 because val1 is less than val2.

javac IntegerCompareMethod.java && java IntegerCompareMethod

Compare equal values

In the main() method, create two int variables with the same value. Call the compare() method with these two variables as parameters and store the result in a variable named result.

int val3 = 5;
int val4 = 5;
int result = Integer.compare(val3, val4);

Print the result of the comparison

Print the value of the result variable using the System.out.println() method.

System.out.println("The result is: " + result);

Test the comparison

Test the comparison by running the code. The output should be 0 because val3 is equal to val4.

javac IntegerCompareMethod.java && java IntegerCompareMethod

Compare greater value

In the main() method, create two int variables where val5 is greater than val6. Call the compare() method with these two variables as parameters and store the result in a variable named result.

int val5 = 10;
int val6 = 5;
int result = Integer.compare(val5, val6);

Print the result of the comparison

Print the value of the result variable using the System.out.println() method.

System.out.println("The result is: " + result);

Test the comparison

Test the comparison by running the code. The output should be 1 because val5 is greater than val6.

javac IntegerCompareMethod.java && java IntegerCompareMethod

Summary

In this lab, you have learned how to use the compare() method in the Integer class of Java. You also learned the syntax, parameters, and returns of the method. By following the step-by-step guide, you were able to create a functional program that compares two int values using the compare() method.

Other Java Tutorials you may like