Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Understanding String objects 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Hi,

I've just started learning Java.

I've been told String is not a primitive data type but actually an object of class String.

OK I can accept this, however, in the OO forum I have been told
strings are immutable.
So when you create a string
Code:
String myName = "Bob";

and then change its content
Code:
myName = "John";

You haven't changed the state of the String object, but it's actually created a new object with the new content.

I'm confused. I thought Objects by defenition have a state and always have accessor methods to alter and interigate its state as well as other methods which make up it's protocol.

If this is not what happens with a string, how can it be an object?

I have read that Java is the ONLY true OO language from the ground up , so was wondering if perhaps, this isn't the case with Java.

All help understanding, what a string actualy is if it isn't an object and also what happens to the memory references to the other objects when the string is altered is much appreciated.

Regards,
1DMF.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
You're mixing concepts.

Strings cannot change it's state means you cannòt perform a setValue, for example.

But the example you give works the same for all objects. You're pointing your variable to different objects. You don't change an object state with assignment operations but with method calls.

Cheers,
Dian
 
Thank you so much, now you put it like that I understand.

myName is not an object it's a 'reference variable', the objects were "Bob" & "John", so you have changed the object refrence 'value' (memory address) of myName.

Thank-you, Thank-you , Thank-you!!!!

What happens to the object "Bob" now that nothing is pointing to it?

Is the memory / object automatically released once there is no reference variable pointing to it any more?

Can you re-assign a reference variable back to the "Bob" object?





"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
P.S.

Why do strings not have methods for chaging state? if they are objects.

Why do you use assignent for strings and not...
Code:
myName.setValue("Paul");


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
With "normal" object, they are collected for garbage (after some nursering and a lot of stuff, but the details are enough to write a book).

In the case os Strings (and integres, from the top off my head), there is an internal pool that the JVM holds with the created objects, so if you do the same again, your variable will be pointed there.

Keep in mind that these are tricky considerations that have little impact in "normal" programming.

If you wanna know more, there are a lot of articles about this:


Good luck :)


Cheers,
Dian
 
Right, I shouldn't get bogged down with too much detail over how inner stuff works. got ya.

I really appreciate your help, it's a lot more clearer now.

I have got a lot of reading to be doing over the weekend, i'm only on unit 3 (out of 14), but it's good to get things straight in your head before ploughing on, I feel much happier about what i've already studied now.

Again, thank you, I really appreciate your input.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top