Possible Duplicate:
Java : different double and Double in comparison可能的重复:Java:不同的重复和重复
In a sample java program for one of my labs, I have two different methods taking Double and double parameters respectively.
How do I differentiate between them when passing arguments to them?
在我的一个实验室的示例java程序中,我有两个不同的方法,分别使用双参数和双参数。在向它们传递参数时,如何区分它们?
7 个解决方案
#1
19
First off you need to understand the difference between the two types. double
is a primitive type whereas Double
is an Object.
首先,您需要了解这两种类型之间的区别。double是基本类型,而double是对象。
The code below shows an overloaded method, which I assume is similar to your lab code.
下面的代码显示了一个重载方法,我假设它与您的实验代码相似。
void doStuff(Double d){ System.out.println("Object call"); }
void doStuff(double d){ System.out.println("Primitive call"); }
There are several ways you can call these methods:
有几种方法可以调用这些方法:
doStuff(100);
doStuff(200d);
doStuff(new Double(100));
These calls will result in:
这些呼吁将导致:
"Primitive call"
"Primitive call"
"Object call"
#2
22
Double
parameter can be null
when double
can't.
双参数可以为空,双参数不行。
#3
1
// Method A
public static void foo(Double d) {...}
// Method B
public static void foo(double d) {...}
Evidently, if you pass a Double
object then Method A will be called; i.e. if you had something like:
显然,如果您传递一个双对象,那么方法a将被调用;例如,如果您有以下内容:
Double d = new Double(1.0);
Further, if you pass a double literal you will call Method B. What's interesting is if you have something like
此外,如果你传递一个双重文字,你会调用Method b,有趣的是如果你有类似的东西
double d = new Double(1.0);
In this case Method B will also be called, because the type of d
is double
; the Double
object gets unboxed to a double
. On the same note, if you had:
在这种情况下,方法B也会被调用,因为d的类型是2倍的;双对象被解压到一个Double。同样,如果你有:
Double d = 1.0;
then Method A would be called, because the type of d
would be Double
(the double
-literal gets autoboxed to a Double
).
然后将调用方法A,因为d的类型是Double (Double -literal被自动框为Double)。
#4
1
- double
is a primitive type, where as Double
is a wrapper object.
- double是原始类型,其中double是包装器对象。
- One of the most common use of Wrapper objects is with Collection
.
-包装对象最常见的用途之一是集合。
Eg:
例如:
List<Double> d = new ArrayList<Double>();
- In Java 5 a mechanism called Autoboxing
has been introduced to convert between the two directly.
-在Java 5中引入了一种称为自动装箱的机制,可以直接在两者之间进行转换。
Eg:
例如:
double d = 10.41;
Double wrapper = d;
#5
0
What you have is an example of method overloading. The good part is that the compiler and the JVM will select the correct method automatically based on the type of the arguments that is used when you call the method.
你有一个方法重载的例子。好的方面是,编译器和JVM将根据调用该方法时使用的参数的类型自动选择正确的方法。
#6
0
Double
is reference type and double
is value type.
Double是引用类型,Double是值类型。
The
Double
class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double." linkDouble类将基元类型Double的值封装在对象中。类型为Double的对象包含一个类型为Double的字段
As @Fess mentioned and because Double
is reference type it can be null
.
正如@Fess所提到的,因为Double是引用类型,所以它可以为空。
If you want you can explictly convert from Double
to double
with .doubleValue()
method and viceverrsa with new Double(1.0)
.
如果需要,可以使用. doublevalue()方法将Double转换为Double,使用新的Double(1.0)方法将viceverrsa转换为Double。
Also as @millimoose said:
也是@millimoose说:
You should use
X.valueOf()
instead ofnew X()
. ThevalueOf
methods are allowed to cache the boxing types to reduce memory use. (Not sure this is done forDouble
s but it's a good habit to get into.)"您应该使用X. valueof()而不是新的X()。允许valueOf方法缓存装箱类型,以减少内存使用。(我不确定双打比赛是否能做到这一点,但这是个不错的习惯。)
#7
0
Double is a wrapper class while double is a primitive type like c/c++. As pointed out above, Double is mostly used in generics but also is useful anywhere there is a need for both numerical value and proper object encapsulation. In most cases the Double and double can be used interchangeably.
Double是一个包装类,而Double是一个原始类型,比如c/c++。如上所述,Double主要用于泛型,但在需要数值和适当对象封装的地方也很有用。在大多数情况下,Double和Double可以互换使用。
#1
19
First off you need to understand the difference between the two types. double
is a primitive type whereas Double
is an Object.
首先,您需要了解这两种类型之间的区别。double是基本类型,而double是对象。
The code below shows an overloaded method, which I assume is similar to your lab code.
下面的代码显示了一个重载方法,我假设它与您的实验代码相似。
void doStuff(Double d){ System.out.println("Object call"); }
void doStuff(double d){ System.out.println("Primitive call"); }
There are several ways you can call these methods:
有几种方法可以调用这些方法:
doStuff(100);
doStuff(200d);
doStuff(new Double(100));
These calls will result in:
这些呼吁将导致:
"Primitive call"
"Primitive call"
"Object call"
#2
22
Double
parameter can be null
when double
can't.
双参数可以为空,双参数不行。
#3
1
// Method A
public static void foo(Double d) {...}
// Method B
public static void foo(double d) {...}
Evidently, if you pass a Double
object then Method A will be called; i.e. if you had something like:
显然,如果您传递一个双对象,那么方法a将被调用;例如,如果您有以下内容:
Double d = new Double(1.0);
Further, if you pass a double literal you will call Method B. What's interesting is if you have something like
此外,如果你传递一个双重文字,你会调用Method b,有趣的是如果你有类似的东西
double d = new Double(1.0);
In this case Method B will also be called, because the type of d
is double
; the Double
object gets unboxed to a double
. On the same note, if you had:
在这种情况下,方法B也会被调用,因为d的类型是2倍的;双对象被解压到一个Double。同样,如果你有:
Double d = 1.0;
then Method A would be called, because the type of d
would be Double
(the double
-literal gets autoboxed to a Double
).
然后将调用方法A,因为d的类型是Double (Double -literal被自动框为Double)。
#4
1
- double
is a primitive type, where as Double
is a wrapper object.
- double是原始类型,其中double是包装器对象。
- One of the most common use of Wrapper objects is with Collection
.
-包装对象最常见的用途之一是集合。
Eg:
例如:
List<Double> d = new ArrayList<Double>();
- In Java 5 a mechanism called Autoboxing
has been introduced to convert between the two directly.
-在Java 5中引入了一种称为自动装箱的机制,可以直接在两者之间进行转换。
Eg:
例如:
double d = 10.41;
Double wrapper = d;
#5
0
What you have is an example of method overloading. The good part is that the compiler and the JVM will select the correct method automatically based on the type of the arguments that is used when you call the method.
你有一个方法重载的例子。好的方面是,编译器和JVM将根据调用该方法时使用的参数的类型自动选择正确的方法。
#6
0
Double
is reference type and double
is value type.
Double是引用类型,Double是值类型。
The
Double
class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double." linkDouble类将基元类型Double的值封装在对象中。类型为Double的对象包含一个类型为Double的字段
As @Fess mentioned and because Double
is reference type it can be null
.
正如@Fess所提到的,因为Double是引用类型,所以它可以为空。
If you want you can explictly convert from Double
to double
with .doubleValue()
method and viceverrsa with new Double(1.0)
.
如果需要,可以使用. doublevalue()方法将Double转换为Double,使用新的Double(1.0)方法将viceverrsa转换为Double。
Also as @millimoose said:
也是@millimoose说:
You should use
X.valueOf()
instead ofnew X()
. ThevalueOf
methods are allowed to cache the boxing types to reduce memory use. (Not sure this is done forDouble
s but it's a good habit to get into.)"您应该使用X. valueof()而不是新的X()。允许valueOf方法缓存装箱类型,以减少内存使用。(我不确定双打比赛是否能做到这一点,但这是个不错的习惯。)
#7
0
Double is a wrapper class while double is a primitive type like c/c++. As pointed out above, Double is mostly used in generics but also is useful anywhere there is a need for both numerical value and proper object encapsulation. In most cases the Double and double can be used interchangeably.
Double是一个包装类,而Double是一个原始类型,比如c/c++。如上所述,Double主要用于泛型,但在需要数值和适当对象封装的地方也很有用。在大多数情况下,Double和Double可以互换使用。