Reputation: 1767
may i know why is there .toString() when you can use (String) instead? and vice versa and also for numbers can use the wrapper class's so why do we need to have a (double)? etc
Upvotes: 2
Views: 284
Reputation: 10954
toString()
is a method that already define in Object
class which all objects in Java implicitly extend. It is used for the System.out.println()
. In some cases, you need to override it, let's say you have an object like this and want to use System.out.println()
to print it out automatically
class Person {
private String name;
private int age;
Person(String name, int age){ .....}
@Override
public String toString(){
return name + " " + age;
}
}
Person p = new Person("Bill", 24);
System.out.println(p);
About the (string)
cast, you can just cast some wrappers and primitives and nearly useless for cases like the one above.
Upvotes: 0
Reputation: 116306
(String)
is a class cast, which works only if the object is actually of the type you are casting to (including subclasses - however, String
is final
, so in this case there can be no subclasses). And if the type of the object is something else than expected, it fails with a ClassCastException
. A cast does not create a new object, neither does it change the original, only gives you a different type of reference to it.
OTOH, toString()
is a (potential) conversion which is defined for all objects, thus always yields results (except when invoked on a null pointer of course). I wrote potential, because for String
s obviously no conversion is taking place. In other cases, a conversion in general usually creates a new object (in our case, a String
), and might even modify the internal state of the original object (e.g. in case it is caching the result of the conversion - although this difference should not be visible to the outside world).
Upvotes: 4
Reputation: 5300
double, int, float are primitive types, they are not objects. You cannot do int.toString.
But Double and Integer are classes from which you can make objects and then you call call their methods such as as Integer.toString().
Upvotes: 0
Reputation: 14505
Type Casting , Wrapper classes and toString() are completely different.
Basically the Type Casting is the process where you will change your one entity into the other entity with different datatype. Means you will simply change the datatype of the entity.
Wrapper classes are used to create an object of type from existing type object.
.toString() is method defined in Object, this method is basically used to represent your object in String format. You can override this method to represent your object in any string format.
Upvotes: 3
Reputation: 206996
Casting to String
is not the same as calling .toString()
on an arbitrary object.
Note that casting does not do any kind of conversion at all: casting is nothing more than giving the compiler a hint "look, I know this object is a String
, so I want you to treat it as a String
". If, at runtime, the object is not really a String
, you will get a ClassCastException
.
Upvotes: 0
Reputation: 54045
(String) obj
means: obj
is an instance of String
, and I want a reference of this type.
toString()
is a regular method implemented by all Object
s.
Upvotes: 3
Reputation: 597382
Because they are different things.
Integer i = Integer.valueOf("0");
String zero1 = i.toString(); // WORKS
String zero2 = (String) i; // FAILS
The toString()
method is a method of each object to obtain a string representation of the object.
Casting is used when you know what the type of the object is, but it is currently referred as a supertype.
Upvotes: 9