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


Java Double.compareTo()用法及代碼示例


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

用法:

public int compareTo(Object obj)

參數:該方法接受一個參數。
對象–要比較的對象。


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

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

以下示例程序旨在說明java.lang.Double.compareTo()方法的用法:

示例1:當兩個對象都不相同時。

// Java program to demonstrate 
// of java.lang.Double.compareTo() method 
import java.lang.Math; 
  
class Gfg1 { 
  
    // Driver code 
    public static void main(String args[]) 
    { 
  
        // When two objects are different 
        Double obj1 = new Double(124); 
        Double obj2 = new Double(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.Double.compareTo() method 
import java.lang.Math; 
  
class Gfg1 { 
  
    // Driver code 
    public static void main(String args[]) 
    { 
        // When no argument is passed 
        Double obj1 = new Double(124); 
        Double obj2 = new Double(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 Double 
cannot be applied to given types;
        int compareValue = obj1.compareTo();
                               ^
  required: Double
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

示例3:除了對象以外的任何東西都作為參數傳遞時。

// Java program to demonstrate 
// of java.lang.Double.compareTo() method 
import java.lang.Math; 
  
class Gfg1 { 
  
    // Driver code 
    public static void main(String args[]) 
    { 
  
        // When anything other than object 
        // argument is passed 
        Double obj1 = new Double(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:15: error: incompatible types: String cannot be converted to Double
        int compareValue = obj1.compareTo("gfg");
                                          ^
Note: Some messages have been simplified; recompile with 
-Xdiags:verbose to get full output
1 error


相關用法


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