logo

Java Integer compareTo() metoda

The usporedi s() metoda je metoda klase Integer pod java.lang paket . Ova metoda uspoređuje dva cijela broja objekti numerički. Vraća rezultat vrijednosti 0 ako je Integer jednak argumentu Integer, vrijednost manja od 0 ako je Integer manji od argument Cijeli broj i vrijednost veća od 0 ako je cijeli broj veći od argumenta cijeli broj. Ovu metodu specificira Usporedivo Sučelje .

Napomena: Ova metoda ne može uspoređivati ​​dvije različite vrste argumenata, stoga i argument i broj trebaju biti iste vrste.

Sintaksa

Slijedi izjava od usporedi() metoda:

 public int compareTo(Integer anotherInteger) 

Parametar:

Tip podataka Parametar Opis Obavezno/opcionalno
int još jedanInteger Vrijednost cijelog broja za usporedbu Potreban

Povratak:

Ova metoda će vratiti sljedeće vrijednosti:

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

Iznimke:

DA

Verzija kompatibilnosti:

Java 1.2 i novije

Primjer 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,>
Testirajte sada

Izlaz:

 -1 0 1 

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

Primjer 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)); } } 
Testirajte sada

Izlaz:

 1 0 -1 

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

Izlaz:

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