What are the differences between casting to String, using the toString method, and using the String.valueOf() method?

John Mitchell

Casting only transforms the reference to the object from one type to another. Casting does nothing to the underlying object itself. The compiler and/or the runtime engine will verify the fact that the reference that you're casting to a reference to a String is, in fact, referring to an object which is (type compatible with) a String.

The String.value() method allows for the creation of a String representation of the built-in, primitive data types such as int, boolean, float, etc.

The .toString() method is a method which all Java classes inherit from the java.lang.Object root class (and which many classes override). The .toString() method of each class is supposed to create an appropriate String representation of objects of that class.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact