Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Type casting Strings  RSS feed

 
pavanasree vasireddy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everybody,

I have a small doubt regarding typecasting the string objects.

what is the difference between converting an object by calling toString()
method and by explicitly typecasting the object. I mean by explicitly giving (String) before the object.


Thanks in advance
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please be more specific in your question....
If possible, post the code you are having trouble with....

Vijay
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toString method returns some String representation of an object. In your own classes you could override this method to return some useful information. Look at Thread.
In general it returns some internal address of that reference.
I'm not sure what you mean by explicitly typecasting. Maybe it is useful when you are working with collections.
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pavanasree,

This is a good question in fact. It made me think. I am giving what my limited knowledge holds in store.

The toString() method is found originally in the Object class.

All the class, have the method. This is used when we need to print some useful, meaningful information about the object of the class. Otherwise, the System.out.println() returns a value as

OurClassName@UnsignedHashCode of the Object.

For example you have some primitive and if you need to read the primitive or an object for that matter, we override the toString() method. This will give a value that we can understand of our objects.

And when it comes to casting....i welcome some ideas on the usage of these two methods. I have been using (String) explicitly to cast the result.

Any luck guys?

Cheers,
Swamy
 
satish sathineni
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It is really good question...

.toString() is defined the Object class all other objects mostly extend from the
Object class they overridden the .toString()

This is done becoz to represent the meaningful representation of the class behaviour or stuff like that ....

Now one important fact i came to know about String class and its .toString() is that it returns the value assigned to the String

So when u r aware that the Return Type is String u can call either

.toString() or (String) both gives u the same output the
value of the String

for other than String objects u cannot expect the output of the .toString() method


Regards
Satish
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be clear about what casting does:

You cannot change the type of an object. You can cast a reference type in order to create a reference of a different type. This doesn't change the original reference, and it certainly has no effect on any object being referred to.

As already pointed out, the only situation where ref.toString() == ref would be true, would be when ref refers to an object of type String. (ref could be of type String, Object, Serializable, Comparable or CharSequence.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!