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
相关用法
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Clock withZone()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java Set add()用法及代码示例
- Java Map put()用法及代码示例
注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 Double.compareTo() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。