當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java long compareTo()用法及代碼示例


java.lang.Long.compareTo()是java中的內置方法,該方法以數字方式比較兩個Long對象。如果此對象等於自變量對象,則此方法返回0;如果此對象在數值上小於自變量對象,則返回小於0;如果此對象在數值上大於自變量對象,則返回大於0的值。

用法:

public int compareTo(Object obj)

Parameter:
obj - The object which is to be compared to.

返回值:該函數返回三個值:


  • 等於0:對象等於參數對象
  • 小於0:對象小於參數對象
  • 大於0:對象大於參數對象

程序1:下麵的程序演示了函數的工作。

// Java program to demonstrate 
// of java.lang.Long.compareTo() method 
import java.lang.Math; 
  
class Gfg1 { 
  
    // driver code 
    public static void main(String args[]) 
    { 
  
        // when two objects are different 
        Long obj1 = new Long(124); 
        Long obj2 = new Long(167); 
        int compareValue = obj1.compareTo(obj2); 
  
        if (compareValue == 0) 
            System.out.println("object1 and object2 are equal"); 
        else if (compareValue < 0) 
            System.out.println("object1 is less than object2"); 
        else
            System.out.println("object1 is greater than object2"); 
    } 
}

輸出:

object1 is less than object2

程序2:下麵的程序演示了不傳遞任何參數時函數的工作方式

// Java program to demonstrate 
// of java.lang.Long.compareTo() method 
import java.lang.Math; 
  
class Gfg1 { 
  
    // driver code 
    public static void main(String args[]) 
    { 
        // when no argument is passed 
        Long obj1 = new Long(124); 
        Long obj2 = new Long(167); 
  
        int compareValue = obj1.compareTo(); 
  
        if (compareValue == 0) 
            System.out.println("object1 and object2 are equal"); 
        else if (compareValue < 0) 
            System.out.println("object1 is less than object2"); 
        else
            System.out.println("object1 is greater than object2"); 
    } 
}

輸出:

prog.java:14:error:method compareTo in class Long cannot be applied to given types;
      int compareValue = obj1.compareTo(); 
                             ^
  required:Long
  found:no arguments
  reason:actual and formal argument lists differ in length
1 error

程序3:下麵的程序演示了在參數中傳遞對象以外的任何東西時函數的工作方式

// Java program to demonstrate 
// of java.lang.Long.compareTo() method 
import java.lang.Math; 
  
class Gfg1 { 
  
    // driver code 
    public static void main(String args[]) 
    { 
  
        // when anything other than object 
        // argument is passed 
        Long obj1 = new Long(124); 
  
        int compareValue = obj1.compareTo("gfg"); 
  
        if (compareValue == 0) 
            System.out.println("object1 and object2 are equal"); 
        else if (compareValue < 0) 
            System.out.println("object1 is less than object2"); 
        else
            System.out.println("object1 is greater than object2"); 
    } 
}

輸出:

prog.java:14:error:incompatible types:String cannot be converted to Long
      int compareValue = obj1.compareTo("gfg"); 
                                        ^
Note:Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Java long compareTo() with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。