jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String manipulation!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String manipulation!!" Watch "String manipulation!!" New topic

String manipulation!!

Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
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
[ 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
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
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: String manipulation!!
It's not a secret anymore!