If you google your question you will get the answer.
A String in Java is an object and not a primitive data type. It is completely immutable.
Once created it cannot be changed. Calling any method that performs operations on a String and returns a String is ultimately returning a whole new String object.
> It is completely immutable. Once created it cannot be changed.
import java.lang.reflect.*;
public class ImmutableStringDemo {
public static void main(String[] args) throws Exception {
String string = "foo";
System.out.println(string);
new StringChanger().change(string);
System.out.println(string);
}
}
class StringChanger {
public void change(String s) throws IllegalAccessException {
Field[] fields = s.getClass().getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
if (f.getType() == char[].class) {
f.set(s, "bar".toCharArray());
}
}
}
}
QED. ;o)
~
> > It is completely immutable. Once created it
> cannot be changed.
>
> import java.lang.reflect.*;
>
> public class ImmutableStringDemo {
>
> public static void main(String[] args) throws
> Exception {
>String string = "foo";
> System.out.println(string);
>new StringChanger().change(string);
> System.out.println(string);
>}
>
> class StringChanger {
>
> public void change(String s) throws
> IllegalAccessException {
> Field[] fields =
> s.getClass().getDeclaredFields();
>for (Field f : fields) {
>f.setAccessible(true);
>if (f.getType() == char[].class) {
> f.set(s, "bar".toCharArray());
>}
>}
>
> }
>
>
> QED. ;o)
>
> ~
You've got a PERL script running that scans forums for the words "strings are immutable", and replies with that, and I claim my ? :-)
> You've got a PERL script running that scans forums
> for the words "strings are immutable", and replies
> with that, and I claim my ? :-)
Haha! No, this thread just happened to be at the top of the list when I checked in this morning (afternoon for you imperialists... ^ahem^ friends from across the pond). It was still fresh in my mind from [url=http://forum.java.sun.com/thread.jspa?threadID=5177927]the other day[/url]. But hey, if ? will make you happy, swing by and I'll hand it to you in person. :o)
~
> :O
>
> Wow.
>
> I learned something today.
>
> Thanks for a good wack with the humble stick.
Bit of a sneaky stick really, and not entirely accurate :-) wack him back with it!
Teh Docs describe java.lang.String as immutable, but in some implementations of Java, and under certain conditions, it's possible to use reflection to overcome this
> I learned something today.
Be careful! With great power comes great responsibility! ;o)
Kidding aside, the example is not to be taken seriously. It violates some fundamental design principles and isn't what I'd consider good programming practice. Strings are intended to be immutable, and the API takes reasonable measures to enforce that behavior.
~
> > Bit of a sneaky stick really, and not entirely
> > accurate :-) wack him back with it!
>
> Indeed. I deserve it.
>
> Yawmark (< a.k.a. "Bad, wicked, naughty Zoot")
Have you been lighting your grail-shaped beacon again?
> > Bit of a sneaky stick really, and not entirely
> > accurate :-) wack him back with it!
>
> Indeed. I deserve it.
>
> Yawmark (< a.k.a. "Bad, wicked, naughty Zoot")
By the way, you should modify your Perl script to also jump on the Java-is-always-pass-by-value debate. I'm sure that would ruffle some feathers ;).
> > Bit of a sneaky stick really, and not entirely
> > accurate :-) wack him back with it!
>
> Indeed. I deserve it.
>
> Yawmark (< a.k.a. "Bad, wicked, naughty Zoot")
Well alright... but if you start getting too much into it then I am going to have to get my crotchless leather pants, a whip, and a gag.
It contributes to the whole humble stick experience.