• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in java string as an object  RSS feed

 
Ashish A Bansal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1st code Starts


2nd code starts
2nd code 1st class starts



2nd code 2nd class starts




We know that String is also an object and the name of the string variable is just a reference to that object.Then Why both these codes behave differently??.
In 1st code , the String value or data stored in the object does not changes in output whereas in 2nd code the object data gets changed.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first example you aren't doing anything to change the object. (You can't in any case; strings are immutable.) You are merely changing which object z refers to.

In the second example, both stu and s (please choose better variable names) refer to the same object. So it doesn't matter which reference is used to change the member variable value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!