-5
  String str=new String("JAVA");
 System.out.println("value of str before "+str); //JAVA
 String str2=null;
 str=str2;
 System.out.println("value of str"+str);  //null
 System.out.println("value of str2"+str2);//null

 str="Prog";
 System.out.println("value of str"+str); //prog 
 System.out.println("value of str2"+str2);//null

Ques 1 If string is immutable why is value of str changing??

 Student stu= new Student(123);        
 System.out.println("value of stu before "+stu); //some address is printed
 Student stu2=null;
 stu=stu2;
 System.out.println("value of stu"+stu);  //null
 System.out.println("value of stu2"+stu2);//null
 Student stu3=new Student(456);
 stu=stu3;
 System.out.println("value of stu"+stu); //some new address 
System.out.println("value of stu2"+stu2);//null

Ques 2.String and Object are behaving in same way .Then why String is immutable and Object mutable. Where's the difference

  • This code doesn't make sense and won't even compile. – chrylis -cautiouslyoptimistic- Sep 07 '13 at 19:25
  • Java String objects are "immutable" -- they can't be changed. An object type is immutable if its class contains no methods (or public fields) allowing it to be modified. – Hot Licks Sep 07 '13 at 19:26
  • And as pointed out, your code above will not compile because javac does not allow you to use a local variable that has not been initialized. – Hot Licks Sep 07 '13 at 19:29
  • 3
    (But your confusion is between the concepts of *reference* and *object*. The variables `stu` and `stu2` above are *reference variables* (pointers). They are NOT *objects* but "point to" *objects*. If you change a *reference variable* you don't affect the contents of the *object* it's referencing, rather you change *which* object it references.) – Hot Licks Sep 07 '13 at 19:32
  • I have re-framed my question. Hope now I am able to make myself clear – JavaProgrammer Sep 08 '13 at 09:53
  • Answer 1): Because you changed what str points to. It's the OBJECT that's immutable, not the REFERENCE VARIABLE. – Hot Licks Sep 09 '13 at 15:44

5 Answers5

1

when you create Object like new Student(123) or new String("JAVA") it takes space in heap. str, str2, stu, stu2 are references, they can hold the reference of same type of Object.

Same memory is allocated now or different??

Different as String and Student are not same class, object will take different space in heap.

if stu changes will stu2 change??

yes, as long they both referencing the same object.

Why object is mutable and string is immutable?

you can better go through this SO question - Immutable class?

Community
  • 1
  • 1
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

Strings can't ever change, period; that's a core characteristic of the language.

If, however, str and str2 were a different that was mutable, then str = str2 would just set str to point to the same object str2 does, and changes to str2 would therefore be changes to str. Note that in your particular case, str2 is uninitialized, and so your code won't compile.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

Java does not allow Strings that is just part of the language. Also in your code str2 is never initialized.

lilmessi42
  • 313
  • 2
  • 3
  • 13
0

Mutable vs immutable - String is immutable, you can't change it after construction. As to why, there are several advantages for an immutable class as you would follow here :

Difference between Mutable objects and Immutable objects

Most issues with java reference is resovled within this post : Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
serj
  • 508
  • 4
  • 20
  • Q1 - the value that str points to doesn't change, instead the place str points to is changed. After the str = str2 assignment, str points to null ("nowhere"). Q2 - the same thing all over, you don't change any object by referencing the pointer that points to it, to another object. Both mutable and immutable object can be assigned and referenced to other object. Try to imagine the following example, you can call your table a chair, it doesn't change it's nature. If you want to change a table, break one of it's legs. A dumb example but it might give you some intuition. – serj Sep 10 '13 at 13:21
-1

Both strings would use the same memory. However, when you change str, str2 would not change and vice versa. This is because strings in Java are stored in a string pool. Take into consideration the following code:

String str1 = "hello";
String str2 = "hello";

Both of these strings would point to the same place in memory in the string pool. When you "create" a string, it checks if the string already exists in the string pool, and if it does, points to that string. This has many implications. For example, consider:

String str1 = "hello";
String str2 = "hello";
str2 = str2 + " world";

In this case, "hello" would be added to the string pool and str1 would point to it. str2 would check that "hello" exists, see that it does, and then point to it as well. The last line would create an entire new string "hello world" in the string pool and then str2 would point to it.

BR410bury
  • 13
  • 4
  • Oh, please! Don't confuse things with interned strings just yet! Let's help the guy understand the difference between "reference" and "object" first. – Hot Licks Sep 07 '13 at 19:33
  • He asked why strings were immutable and what that meant. I explained that part and yet you mark me down? Especially when I'm obviously new to answering on this site? Way to encourage individuals to contribute. – BR410bury Sep 09 '13 at 12:49