java (String)value 和 value.toString() , new Long(value) 和 (Long)value 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15900831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between (String)value and value.toString() , new Long(value) and (Long)value
提问by PSR
In some places i saw (String)value
.In some places value.toString()
在一些地方我看到了(String)value
在一些地方value.toString()
What is the difference between these two.In which scenario which one i need to use.
这两者有什么区别。在哪种情况下我需要使用哪个。
And what is the difference between new Long(value)
and (Long)value
?
和之间有什么区别 new Long(value)
和(Long)value
?
回答by Milo? Luka?ka
(String) value casts object value to string, which has to extends String. value.toString() calls method on object value, which is inherritated from class Object and this method return String, that show information of this object. If you have some yourClass value, it is reccomended to overrite toString()
(String) value 将对象值转换为字符串,它必须扩展 String。value.toString() 调用对象值的方法,该方法继承自Object 类,该方法返回String,显示该对象的信息。如果您有一些 yourClass 值,建议重写 toString()
new Long(value) creates new object of type Long and sets value of Long to your variable value. (Long)value get object value and cast it to object of type Long. in Long(value) value has to be number or string.
new Long(value) 创建 Long 类型的新对象并将 Long 的值设置为您的变量值。(Long)value 获取对象值并将其转换为 Long 类型的对象。在 Long(value) 值必须是数字或字符串。
回答by Nirbhay Mishra
In
在
new Long(value)
creates new wrapper class Object
创建新的包装类 Object
and
和
(Long)value
type cast value to Long( to wrapper) if possible.
如果可能,将类型转换为 Long(到包装器)。
similarly
相似地
String(value)
type cast value to to String
将值类型转换为字符串
but toString() is a method which is a object class method and One must override it according to need, eg.
但是 toString() 是一种方法,它是一个对象类方法,必须根据需要覆盖它,例如。
class User
{
String name;
int age;
public String toString()
{
return "name :"+name+" \n age :"+age;
}
}
回答by Hot Licks
In no language (that I know of) will a cast change the type of an object.
在任何语言中(我所知道的)都不会通过强制转换来改变对象的类型。
You use the cast (String)
when you have, say, a reference that the compiler thinks is an Object, but you know is really a String, and you want the compiler to know that. If you have an Integer and try to cast to String you will get a ClassCastException when you run the code.
您可以使用铸铁(String)
,当你有,比如说,编译器认为引用是一个对象,但你知道确实是一个字符串,并且希望编译器知道。如果您有一个 Integer 并尝试转换为 String,则在运行代码时您将收到 ClassCastException。
So if you have an Integer and want its String representation you'd use toString.
因此,如果您有一个 Integer 并且想要它的 String 表示,您可以使用 toString。
(Note that a cast WILL change the type of a "scalar". Ie, you can cast from int to char with (char)
and the compiler will perform the appropriate conversion. The "cast" in this case is an entirely different concept. It's unfortunate that tradition has led the same syntax to be used for both.)
(请注意,强制转换将改变“标量”的类型。即,您可以使用从 int 到 char(char)
的强制转换,编译器将执行适当的转换。在这种情况下,“强制转换”是一个完全不同的概念。不幸的是传统导致两者使用相同的语法。)
回答by omer schleifer
First of all , is value a string itself? the (String)value is a cast and will only work is value is a string. However, calling value.toString() is just a method call. toString() is a method of every object in java, so that will not fail even if value is, let's say, an integer.
首先, value 是字符串本身吗?(String)value 是一个强制转换,只有 value 是一个字符串才有效。然而,调用 value.toString() 只是一个方法调用。toString() 是java中每个对象的方法,因此即使值是整数也不会失败。
Now , Calling (Long)value is trying to cast value to Long, and will only work if value is of type Long. and, calling Long(value) is actually calling the constructor of class Long, passing in value as parameter.
现在,Calling (Long)value 正在尝试将 value 转换为 Long,并且仅当 value 为 Long 类型时才有效。并且,调用 Long(value) 实际上是调用 Long 类的构造函数,将 value 作为参数传入。
回答by Prashant K
When you cast an object to String you are sure that it is a string and at run-time if it happens to be an instance of another object then you get a class cast exception.
当您将对象强制转换为 String 时,您可以确定它是一个字符串,并且在运行时如果它恰好是另一个对象的实例,那么您会收到类强制转换异常。
On the other hand, When you call a toString() method on an object it need not be a string as all classes inherit that method from class object.
另一方面,当您在对象上调用 toString() 方法时,它不需要是字符串,因为所有类都从类对象继承该方法。
回答by AmitG
String
class implements CharSequence
and also it extends from Object
class. So if somebody is using other type of CharSequence
we have to typecast like your first version (String)value
[ (String)value
if it contains String
object and (StringBuffer)value
for StringBuffer type of object]
About toString()
we have to override toString()
method in our class which will show our object representation in String format as per our requirement. If we don't override then default implementation of toString()
from Object
class will be inherited and will give the String in hexadecimal representation of the hash code of our object like UserObject@12dacd1
String
类实现CharSequence
并且它从Object
类扩展。因此,如果有人使用其他类型的CharSequence
我们必须像您的第一个版本一样进行类型转换(String)value
[(String)value
如果它包含String
对象并且(StringBuffer)value
对于 StringBuffer 类型的对象]
关于toString()
我们必须覆盖toString()
我们类中的方法,它将根据我们的要求以字符串格式显示我们的对象表示. 如果我们不覆盖,那么toString()
fromObject
类的默认实现将被继承,并将以十六进制表示形式给出我们对象的哈希码的字符串,如UserObject@12dacd1
Other implementation of CharSequence
are CharBuffer, StringBuffer, StringBuilder
其他实现CharSequence
有CharBuffer、StringBuffer、StringBuilder
1> (String)valuemeans value
contains String object. If it doesn't contain String
the ClassCastException
will be thrown at runtime. Typecasting will throw compile time exception if you typecast with irrelevant type hierarchy. like given below
1> (String)value表示value
包含 String 对象。如果它不包含String
该ClassCastException
会在运行时抛出。如果您使用不相关的类型层次结构进行类型转换,则类型转换将引发编译时异常。如下所示
Exception e = new Exception();
String str = (String)e;
2> Invoking toString()means anyobject
is not necessary to be a String
object. Proper overriding toString()
method in our own class will be helpful for the String representation of our own class and also it will neverthrow any exception exists in the java world because it inherits toString()
from object class. So if your override toStringand you print System.out.println(userObject);
then it will not show hexadecimal representation of the hash code of our object like UserObject@12dacd1
2> 调用toString()意味着anyobject
不必是String
对象。toString()
在我们自己的类中正确的覆盖方法将有助于我们自己的类的字符串表示,并且它永远不会抛出 Java 世界中存在的任何异常,因为它继承toString()
自对象类。因此,如果您覆盖toString并打印,System.out.println(userObject);
则它不会显示我们对象的哈希码的十六进制表示,例如UserObject@12dacd1
public class UserObject {
String name;
int age;
@Override
public String toString() {
return " Name="+name+" \n age="+age;
}
public static void main(String[] args) {
UserObject uo = new UserObject();
uo.name="AAA";
uo.age=18;
System.out.println(uo); //output will be "Name=AAA age=18" instead of "UserObject@12dacd1"
}
}
About new Long(value) and (Long)value.
关于新的 Long(value) 和 (Long)value。
new Long(value)
means, you have got value as long/String
you want to convert it into wrapper class Long
object. So you can use Long(long), Long(String) constructoras per the condition.
new Long(value)
意味着,您已经获得了价值,因为long/String
您想将其转换为包装类Long
对象。所以你可以根据条件使用 Long(long), Long(String)构造函数。
Long typecasting explanation is similar to above String typecasting or any typecasting. (Long)value
means when you get the code like below then you can typecast to Long, Integer, Double as well depends on the assigned value at the right side of the equal symbol.
长类型转换的解释类似于上面的字符串类型转换或任何类型转换。(Long)value
意味着当您获得如下代码时,您可以将类型转换为 Long、Integer、Double 也取决于等号右侧的指定值。
Number i=10L; //java.lang.Number
Long a= (Long)i;
回答by Rodrigo Sasaki
Those are all very different. The first one is Type Casting
. Let's say that you receive an Object on your method, and you know it's a String, so you want it to be referred to as that type. so you do that
这些都是非常不同的。第一个是Type Casting
。假设您在方法上接收到一个对象,并且您知道它是一个字符串,因此您希望它被称为该类型。所以你这样做
public void method(Object obj){
String str = (String) obj;
}
The toStringmethod is one inherited from Objectclass, that returns to you a String
representation of any given Object
.
该的toString方法是一个继承自对象类,即返回到你String
的任何给定表示Object
。
So the difference is, when you do the casting, your object must already be a String
, there is no actual conversion. The difference is that you can use a more specific type for your variable, but when you invoke toString that is not necessary.
所以区别在于,当您进行转换时,您的对象必须已经是 a String
,没有实际转换。不同之处在于您可以为变量使用更具体的类型,但是当您调用 toString 时,这是不必要的。
Invoking toString
may give you a different object than the original one, unless your class is already a String
, in that case the same reference is returned.
调用toString
可能会为您提供与原始对象不同的对象,除非您的类已经是 a String
,在这种情况下会返回相同的引用。
回答by Erick de Oliveira Santos
when you do "(String)value" it's a casting, in other words, you is explicitly "saying" for compiler that value is a string, but if not, will throw a runtime exception.
当您执行“(字符串)值”时,它是一个转换,换句话说,您明确地“说”编译器该值是一个字符串,但如果不是,则会引发运行时异常。
when you do "value.ToString()" will be create a new string with the this value
当您执行“value.ToString()”时,将使用此值创建一个新字符串
the same goes for Long
龙也一样