This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String - Char

 
Tarun Oohri
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone
I have a doubt on String. We all know String object are immutable, they can not be changed once created.
Above is the code which reverse the given string. My questions are as follows :
1. If String are immutable then how come we are able to append into the String using + operator ?
2. Why we can not cast (String)char ?
Thanks!!!
 
E Armitage
Rancher
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun Oohri wrote:Hi Everyone

1. If String are immutable then how come we are able to append into the String using + operator ?
Thanks!!!


We are not appending to the existing String. We are creating a new String whose value is a result of the concatenation of those two Strings. So the current String is not changed but a new String is created. That's why you don't just have
but to make sure that the old reference is now pointing to the new String.
Tarun Oohri wrote:Hi Everyone
2. Why we can not cast (String)char ?

Because String does not extend char or Character.
 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun when you will try for this (String)char compile time error will occur. becauce if you are going to cast a char to a reference type then only possible options are Character, Object as a primitive is promoted to its own Wrapper or Object class , while String is a difference class.
 
Marc Cracco
Ranch Hand
Posts: 80
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To turn your char to a String you could use the primitive wrapper class.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic