This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

casting  RSS feed

 
peter brews
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please tell what is right way of casting an object to string.
so that i can do string functions like checking for null,equality,contains etc..

Thanks,
peter
 
Gale Greaser
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say "obj" is an Object that can be represented as a String.
Then you could use either of these:
String objectString1 = "" + obj;
String objectString2 = String.valueOf(obj);
Good luck!
Gale
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See http://jinx.swiki.net/77
Also notice that we are talking about String *conversion* - there is no casting involved at all.
 
Jeremy Thornton
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to strongly consider overriding the 'equals' (and of course hash) methods for your object rather than converting it to a String. String equality checks are comparitively slow and may not exhibit the behaviour you require.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy is right! I should have read the whole question, I guess... :roll:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!