hi, 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

+8  A: 

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.

+4  A: 

(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 Strings 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).

Péter Török
+3  A: 

(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 Objects.

Konrad Garus

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.

+3  A: 

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.

"Type Casting is the process where you will change your one entity into the other entity with different datatype" -- what??? This makes no sense, and it's definitely not official JLS-speak.

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().


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){ .....}

   public String toString(){
      return name + " " + age;

Person p = new Person("Bill", 24);

About the (string) cast, you can just cast some wrappers and primitives and nearly useless for cases like the one above.

Truong Ha