This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Type casting Strings

 
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
 
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
 
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.
 
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
 
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
 
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.)
 
Forget Steve. Look at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic