Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String manipulation!!

 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of curiosity, why/who on earth would want to try string manipulation without assigning it to another string?? I mean is their any practical use to it at all?? e.g. -
x = "abc";
x.concat("def"); //The new string "abcdef" is immediately discarded!!
Now if their is no practical/logical use to it then why didn't the big brains behind java design java to just ignore such statement...don't even compile/execute it if its of no use(save us from the chaos ) , just like the java shortcuts in a boolean expression. Or was it just included so that the examiners can tease our brain in the exam??
Thanks in advance
-Sudhakar
[ July 30, 2003: Message edited by: Sudhakar Krishnamurthy ]
[ July 30, 2003: Message edited by: Sudhakar Krishnamurthy ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is so because of the fact string objects are immutable.
Now you will ask why on earth did they the String class immutable?
For the sake of security. Immutable objects are safer than mutable. They can be shared among many objects/threads and none will modify it in a way that breaks others.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The situation arises from two facts:
1. strings are immutable
2. assignment construct can omitt LHS
Yes, compiler can be made smart to recognise these situations to ignor compiling or running. But think of it: How many times in real life a programmer will write such statements? Examiners want to test us on these facts because independently they have value in them selves.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic