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
Joined: Jul 03, 2001
Posts: 2120
posted
0
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.
SCJP2. Please Indent your code using UBB Code
Barkat Mardhani
Ranch Hand
Joined: Aug 05, 2002
Posts: 787
posted
0
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