This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String manipulation!!" Watch "String manipulation!!" New topic
Author

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
-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
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.
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String manipulation!!
 
Similar Threads
interface question (K&B book)
Java
Function Overloading
Question on Trim
polymorphism help...