Reputation: 40318
In some places i saw (String)value
.In some places 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
?
Upvotes: 8
Views: 16841
Reputation: 909
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.
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.
Upvotes: 2
Reputation: 10543
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
Other implementation of CharSequence
are CharBuffer, StringBuffer, StringBuilder
1> (String)value means 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
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 never throw any exception exists in the java world because it inherits toString()
from object class. So if your override toString and you print System.out.println(userObject);
then it will not show hexadecimal representation of the hash code of our object like 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.
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) constructor as per the condition.
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.
Number i=10L; //java.lang.Number
Long a= (Long)i;
Upvotes: -1
Reputation: 1648
In
new Long(value)
creates new wrapper class Object
and
(Long)value
type cast value to Long( to wrapper) if possible.
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.
class User
{
String name;
int age;
public String toString()
{
return "name :"+name+" \n age :"+age;
}
}
Upvotes: 4
Reputation: 7226
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
public void method(Object obj){
String str = (String) obj;
}
The toString method is one inherited from Object class, that returns to you a String
representation of any given 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.
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.
Upvotes: -1
Reputation: 842
(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()
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.
Upvotes: 10
Reputation: 47729
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.
So if you have an Integer and want its String representation you'd use 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.)
Upvotes: 3
Reputation: 29
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
the same goes for Long
Upvotes: -1
Reputation: 3935
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.
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.
Upvotes: 1