logo

Método Java entero compareTo ()

El comparar con() El método es un método de la clase Integer bajo paquete java.lang . Este método compara dos números enteros. objetos numéricamente. Devuelve el resultado del valor 0 si Integer es igual al argumento Integer, un valor menor que 0 si Integer es menor que el argumento Entero y un valor mayor que 0 si Entero es mayor que el argumento Entero. Este método está especificado por Comparable Interfaz .

Nota: Este método no puede comparar dos tipos diferentes de argumento, por lo que tanto el argumento como el número deben ser del mismo tipo.

Sintaxis

A continuación se presenta la declaración de comparar() método:

 public int compareTo(Integer anotherInteger) 

Parámetro:

Tipo de datos Parámetro Descripción Requerido/Opcional
En t otroEntero El valor entero a comparar. Requerido

Devoluciones:

Este método devolverá los siguientes valores:

 0 = The value 0 if this Integer = the argument Integer, -1 = The value less than 0 if thisInteger the argument Integer 

Excepciones:

ESO

Versión de compatibilidad:

Java 1.2 y superior

Ejemplo 1

 public class IntegerCompareToExample1 { public static void main(String[] args) { Integer num1 = new Integer(10); Integer num2 = new Integer(20); Integer num3 = new Integer(10); Integer num4 = new Integer(30); // as num1<num2, output will be a value less than zero system.out.println(num1.compareto(num2)); as num1="num3," system.out.println(num1.compareto(num3)); num4> num2, Output will be a value greater than zero System.out.println(num4.compareTo(num2)); } } </num2,>
Pruébalo ahora

Producción:

 -1 0 1 

Ejemplo 2

 public class IntegerCompareToExample2 { public static void main(String[] args) { // compares two Integer values numerically Integer x = new Integer(&apos;90&apos;); Integer y= new Integer(&apos;58&apos;); int retResult = x.compareTo(y); if(retResult &gt; 0) { System.out.println(&apos;x is greater than y&apos;); } else if(retResult<0) { system.out.println('x is less than y'); } else equal to < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> x is greater than y </pre> <h2>Example 3</h2> <pre> public class IntegerCompareToExample3 { public static void main(String[] args) { Integer value = new Integer(222); // check if the value is less than 222 System.out.println(value.compareTo(155)); // check if value is equal to 222 System.out.println(value.compareTo(222)); // check if value is greater than 222 System.out.println(value.compareTo(305)); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 0 -1 </pre> <h2>Example 4</h2> <pre> import java.util.Scanner; public class IntegerCompareToExample4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(&apos;Enter the integer value to be campare with another Integer: &apos;); Integer value = sc.nextInt(); sc.close(); System.out.println(value.compareTo(305)); System.out.println(value.compareTo(200)); System.out.println(value.compareTo(155)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the integer value to be campare with another Integer: 200 -1 0 1 </pre> <br></0)>

Ejemplo 3

 public class IntegerCompareToExample3 { public static void main(String[] args) { Integer value = new Integer(222); // check if the value is less than 222 System.out.println(value.compareTo(155)); // check if value is equal to 222 System.out.println(value.compareTo(222)); // check if value is greater than 222 System.out.println(value.compareTo(305)); } } 
Pruébalo ahora

Producción:

 1 0 -1 

Ejemplo 4

 import java.util.Scanner; public class IntegerCompareToExample4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(&apos;Enter the integer value to be campare with another Integer: &apos;); Integer value = sc.nextInt(); sc.close(); System.out.println(value.compareTo(305)); System.out.println(value.compareTo(200)); System.out.println(value.compareTo(155)); } } 

Producción:

 Enter the integer value to be campare with another Integer: 200 -1 0 1