在JAVA中怎么比较Double类型数据的大小
在JAVA中怎么比较Double类型数据的大小
![](https://gss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/3e41706f6c6c75746564616972781d.jpg)
2015-11-07
Double类型的变量可以使用compareTo()方法比较大小,比如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class DoubleDemo { public static void main(String[] args) { // compares two Double objects numerically Double obj1 = new Double( "8.5" ); Double obj2 = new Double( "11.50" ); int retval = obj1.compareTo(obj2); if (retval > 0 ) { System.out.println( "obj1 is greater than obj2" ); } else if (retval < 0 ) { System.out.println( "obj1 is less than obj2" ); } else { System.out.println( "obj1 is equal to obj2" ); } } } |
zxwlp333
2015-11-07
2015-11-07
展开全部
Double是一个包装类,不是基本类型,和double是有区别,你可以把Double就看成一个普通的类型,所以类型在比较时都应该用 equals
两个对象通过==比都是不一样的,所以对象应该用equals比