简体   繁体   English

为什么可以将double转换为int,但是不能将double转换为String?

[英]why casting double to int is possible but casting double to String is not possible?

I convert other type to String by String.valueOf() like following: 我通过String.valueOf()将其他类型转换为String,如下所示:

 String s = String.valueOf(2.5);

i also convert double to int by casting like following: 我也通过如下铸造将double转换为int:

 int i = (int) 2.5 ;

i want casting double to String like following: 我想将double转换为String,如下所示:

 String s = (String) 2.5; 

but i give following error: 但我给出以下错误:

 Cannot cast from double to String

why casting double to int is possible but casting double to String is not possible? 为什么可以将double转换为int,但是不能将double转换为String?

Casting from one primitive type to another is standard, well understood and supported in machine code. 从一种原始类型转换为另一种原始类型是标准的,很好理解并在机器代码中得到支持。 Ie Java just gives you the syntax to produce the appropriate machine code. 即Java只是为您提供了生成相应机器代码的语法。

Even C language had support for this. 甚至C语言也支持这一点。

Converting to objects is different as it requires you to call a library and even though C++ has operator overloading to support such conversions, it is not a cast as such. 转换为对象是不同的,因为它要求您调用库,即使C ++有运算符重载来支持这样的转换,它也不是这样的转换。 C and Java do not support implicit conversion like this. C和Java不支持这样的隐式转换。


BTW The simplest way to convert to a String is to use the following. BTW转换为String的最简单方法是使用以下方法。

primitive p = ...
String s = "" + p;

This is not efficient as it uses a StringBuilder, however it's not efficient to create a String either so it's only not as bad, rather than being very good. 这是无效的,因为它使用StringBuilder,但是创建一个String也没有效率,所以它只是没有那么糟糕,而不是非常好。 btw I have libraries to turn primitives to/from text without creating objects. 顺便说一下,我有库可以在不创建对象的情况下将原语转换为文本。

However, I use this because it is more efficient for the developer and I waiting for a profiler to tell me this is not good enough in which case I wouldn't use String.valueOf() either. 但是,我使用它是因为它对开发人员更有效,我等待分析器告诉我这不够好在这种情况下我也不会使用String.valueOf()。

double a = 44; 
String b = String.valueOf(a);

double and int are both numeric types, thus they can be converted by casting. doubleint都是数字类型,因此可以通过强制转换它们。

String is not a numeric type. String不是数字类型。

According to Java specifications, you need to convert the primitive type first: 根据Java规范,您需要首先转换基本类型:

Any type may be converted to type String by string conversion. 可以通过字符串转换将任何类型转换为String类型。

A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression (§15.9): 首先将基本类型T的值x转换为引用值,就好像将它作为参数提供给适当的类实例创建表达式(第15.9节):

If T is boolean, then use new Boolean(x). 如果T是布尔值,则使用new Boolean(x)。

If T is char, then use new Character(x). 如果T是char,则使用new Character(x)。

If T is byte, short, or int, then use new Integer(x). 如果T是byte,short或int,则使用new Integer(x)。

If T is long, then use new Long(x). 如果T很长,则使用新的Long(x)。

If T is float, then use new Float(x). 如果T是float,则使用new Float(x)。

If T is double, then use new Double(x). 如果T为double,则使用new Double(x)。

This reference value is then converted to type String by string conversion. 然后通过字符串转换将此引用值转换为String类型。

Then 然后

(...) the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; (...)转换的执行就好像通过调用没有参数的引用对象的toString方法一样; but if the result of invoking the toString method is null, then the string "null" is used instead 但是如果调用toString方法的结果为null,则使用字符串“null”

See 5.1.11. 5.1.11。 String Conversion in http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html中的 字符串转换

@Samiey Mehdi : I will give u what happens when U Type Cast From one format to another .. @Samiey Mehdi:我会告诉你当U Type Cast从一种格式到另一种格式时会发生什么。

Example

Double a = 2.5; 双a = 2.5; // consider it // 考虑一下

step 1:first machine will convert this number into binary format .. double a =(0000 0000 0010.0101); 步骤1:第一台机器将此数字转换为二进制格式.. double a =(0000 0000 0010.0101);

step 2:when your type casting it into int .. first it will remove decimals.. lets make the binary into (0000 0000 0010) 第2步:当你的类型将其转换为int ..首先它将删除小数..让我们将二进制转换为(0000 0000 0010)

step 3: it will truncate it into 32 bit after it is completely fallen into int range then it will print the value. 第3步:它会在完全落入int范围后将其截断为32位,然后它将打印该值。 int range( -2,147,483,648 to 2,147,483,647) it will print now Note: if the value of the non decimal is more than range of type cast it will lose the data or digit (shrinks it)and prints the remaining value int范围(-2,147,483,648到2,147,483,647)它将立即打印注意:如果非小数的值大于类型转换的范围,它将丢失数据或数字(缩小它)并打印剩余值

But In Case of String Format .. Step two fails .. it cant able to convert the value of binary digit into string range [bcos string don't have any range]. 但在字符串格式的情况下..第二步失败..它无法将二进制数字的值转换为字符串范围[bcos字符串没有任何范围]。 so it will print incompatible types [compile time error]................. 所以会打印不兼容的类型[编译时错误] .................

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM