logo

Метод Java Integer compareTo().

The порівняти() Метод є методом класу Integer під пакет java.lang . Цей метод порівнює два цілих числа об'єктів чисельно. Він повертає результат значення 0, якщо Integer дорівнює аргументу Integer, значення менше 0, якщо Integer менше ніж аргумент Ціле число та значення більше 0, якщо ціле число більше аргументу ціле число. Цей метод визначено Порівнянний Інтерфейс .

Примітка. Цей метод не може порівнювати два різні типи аргументів, тому і аргумент, і число мають бути одного типу.

Синтаксис

Нижче наведено декларацію порівняти() метод:

 public int compareTo(Integer anotherInteger) 

Параметр:

Тип даних Параметр опис Обов'язковий/необов'язковий
внутр anotherInteger Ціле значення для порівняння вимагається

Повернення:

Цей метод повертає такі значення:

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

Винятки:

ТО

двійкове дерево пошуку

Версія сумісності:

Java 1.2 і вище

Приклад 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,>
Перевірте зараз

Вихід:

 -1 0 1 

Приклад 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)>

Приклад 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)); } } 
Перевірте зараз

Вихід:

 1 0 -1 

Приклад 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)); } } 

Вихід:

файл зміни linux
 Enter the integer value to be campare with another Integer: 200 -1 0 1