logo

Metoda Java Integer compareTo().

The primerjaj z () metoda je metoda razreda Integer pod paket java.lang . Ta metoda primerja dve celi števili predmetov številčno. Vrne rezultat vrednosti 0, če je Integer enako argumentu Integer, vrednost manjša od 0, če je Integer manjše od prepir Celo število in vrednost, večja od 0, če je Celo število večje od argumenta Celo število. To metodo določa Primerljivo Vmesnik .

Opomba: pri tej metodi ni mogoče primerjati dveh različnih vrst argumentov, zato morata biti argument in število iste vrste.

Sintaksa

Sledi izjava primerjaj() metoda:

 public int compareTo(Integer anotherInteger) 

Parameter:

DataType Parameter Opis Obvezno/izbirno
int anotherInteger Vrednost celega števila za primerjavo Obvezno

Vrne:

Ta metoda bo vrnila naslednje vrednosti:

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

Izjeme:

TO

Združljivostna različica:

Java 1.2 in novejši

Primer 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,>
Preizkusite zdaj

Izhod:

 -1 0 1 

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

Primer 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)); } } 
Preizkusite zdaj

Izhod:

 1 0 -1 

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

Izhod:

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