is string primitive data type?

- Is String a primitive date type? Elaborate please.- How is String object is mutable?
[100 byte] By [ali_hammada] at [2007-11-27 6:19:47]
# 1
No
qUesT_foR_knOwLeDgea at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 2
>How is String object is mutable?It's immutable not mutable.Message was edited by: qUesT_foR_knOwLeDge
qUesT_foR_knOwLeDgea at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 3

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.

maple_shafta at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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)

~

yawmarka at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 5

> > 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 ? :-)

georgemca at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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)

~

yawmarka at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 7
:OWow.I learned something today.Thanks for a good wack with the humble stick.
maple_shafta at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 8

> :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

georgemca at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 9

> 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.

~

yawmarka at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 10
> 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")
yawmarka at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 11
> QED. ;o)Ahh yes, Yawmark the Dark, the confuser of newbies, feared throughout the lands... ;)
kevjavaa at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 12

> > 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?

georgemca at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 13

> > 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 ;).

kevjavaa at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 14
> Have you been lighting your grail-shaped beacon again?Dingo, is that you?:o)~
yawmarka at 2007-7-12 17:34:42 > top of Java-index,Java Essentials,Java Programming...
# 15

> > 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.

maple_shafta at 2007-7-21 21:50:54 > top of Java-index,Java Essentials,Java Programming...