logo

Java-Integer-compareTo()-Methode

Der vergleichen mit() Methode ist eine Methode der Integer-Klasse unter java.lang-Paket . Diese Methode vergleicht zwei Ganzzahlen Objekte numerisch. Es gibt das Ergebnis mit dem Wert 0 zurück, wenn Integer gleich dem Argument Integer ist, und einen Wert kleiner als 0, wenn Integer kleiner als das Argument ist Streit Ganzzahl und ein Wert größer als 0, wenn Ganzzahl größer als das Argument Ganzzahl ist. Diese Methode wird angegeben durch Vergleichbar Schnittstelle .

Hinweis: Diese Methode kann nicht zwei verschiedene Arten von Argumenten vergleichen, daher sollten sowohl das Argument als auch die Zahl vom gleichen Typ sein.

Syntax

Es folgt die Erklärung von vergleichen() Methode:

 public int compareTo(Integer anotherInteger) 

Parameter:

Datentyp Parameter Beschreibung Erforderlich/Optional
int anotherInteger Der zu vergleichende Ganzzahlwert Erforderlich

Kehrt zurück:

Diese Methode gibt die folgenden Werte zurück:

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

Ausnahmen:

DAS

Kompatibilitätsversion:

Java 1.2 und höher

Beispiel 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,>
Testen Sie es jetzt

Ausgabe:

 -1 0 1 

Beispiel 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)>

Beispiel 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)); } } 
Testen Sie es jetzt

Ausgabe:

 1 0 -1 

Beispiel 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)); } } 

Ausgabe:

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