String assignment ?
Hi,
in the following code:
public class Test {
public static void main(String[] args) {
String s = new String("Welcome to Java");
Object o = s;
String d = (String)o;
System.out.println(o + s + d);
}
}
Both strings and the object are referring to the same string object., and the println shows 3 "Welcome to Java" 's. I don't understand exactly why this is so? I was hoping someone might be able to explain it to me.
Thanks!
Jimbo
think
[523 byte] By [
Jimbo236a] at [2007-11-27 10:36:27]

>String s = new String("Welcome to Java");
Never use the copy c'tor if you don't have a good reason btw.
String s = "Welcome to Java" is usually sufficient.
> Both strings and the object
You mean "all three variables", because you have only one String and no Object.
>are referring to the same
> string object., and the println shows 3 "Welcome to
> Java" 's. I don't understand exactly why this is so?
Ehrm... the question is, why should it be different?
Your parents have a son: you.
Your uncle has a nephew: you.
Your employer has an employee: you.
If all three ask you for your name, why should they get three different replies?
> Both strings and the object are referring to the same
> string object.
Both String reference variables an the Object reference variable are referring to the same String object. One object, three reference variables.
>, and the println shows 3 "Welcome to
> Java" 's. I don't understand exactly why this is so?
What did you expect it to be, and why?
jverda at 2007-7-28 18:41:21 >

Wow! Thanks for your fast replys. I had to look and make sure that I was on the BEGINNERS forum. Sorry for my ignorance, but I have been at this all of about four weeks now and some of the string, Object, super and sub class things still are fuzzy to me.
First of all, I was under the impression that when you declare a string variable and use the new keyword that what you were doing was constructing a String object (to which you can apply the functions of the String class).
I am unclear what is actually happening when the code "Object o = " was used. Is the Object class the super (or parent) class, so that a String is automatically in the Object class because it is its parent? I take it that after that line of code, we have two reference variables pointing at the String......and then the second "String = " statement is just making another reference to the same String. With the family example you gave me.....applying it this string.....the three variables are just three different points of reference to the same string.
As far as why I thought it might be any different....earlier on in the class
we had code that went kinda like this:
String s = "me";
String t = "you";
s = t;
I guess in this case we lost our reference to the String "me", so if we println s and t we get two "you" 's. I guess I was getting this all confused....probably making it more difficult than it actually is...but I have a lot coming at me at once.....
Thanks for your help!
Jimbo
All classes are derived from the Object class.
> First of all, I was under the impression that when you declare
> a string variable and use the new keyword that what you were
> doing was constructing a String object (to which you can
> apply the functions of the String class).
You don't need to use new for Strings. They are used somewhat differently from other classes. Most of us declare String objects just as CeciNest... did.
> I am unclear what is actually happening when the code
> "Object o = " was used. Is the Object class the super (or parent)
> class
Yes. Object is the parent of all classes.
> I take it that after that line of code, we have two reference variables
> pointing at the String
I think that that's right.
> and then the second "String = " statement is just making another
> reference to the same String.
Yes.
> With the family example you gave me.....applying it this string
> the three variables are just three different points of reference to
> the same string.
Yes.
> String s = "me";
> String t = "you";
> s = t;
This illustrates the same idea, doesn't it?
> First of all, I was under the impression that when
> you declare a string variable and use the new keyword
> that what you were doing was constructing a String
> object (to which you can apply the functions of the
> String class).
Yes. But you don't NEED to do new String(...) to get a String object. Usually you just assign it to a String literal (s = "abc";) or get it from a config file or user input, or call some object's toString() method.
> I am unclear what is actually happening when the code
> "Object o = " was used. Is the Object class the
> super (or parent) class, so that a String is
> automatically in the Object class because it is its
> parent?
Correct.
> I take it that after that line of code, we
> have two reference variables pointing at the
> String......and then the second "String = " statement
> is just making another reference to the same String.
Correct.
> With the family example you gave me.....applying it
> this string.....the three variables are just three
> different points of reference to the same string.
Correct.
> As far as why I thought it might be any
> different....earlier on in the class
> we had code that went kinda like this:
>
> String s = "me";
> String t = "you";
> s = t;
>
> I guess in this case we lost our reference to the
> String "me", so if we println s and t we get two
> "you" 's.
Correct. So, why would printing a + b + c (or whatever your variables were in the initial post) produce anything but three of the same thing? I still don't know what you expected or why.
> I guess I was getting this all
> confused....probably making it more difficult than it
> actually is...but I have a lot coming at me at
> once.....
Actually, it sounds like you have a better handle on how Java's reference variables work than most beginners. The idea that the variable holds a reference, not an object, and that assignment doesn't copy an object, comes hard to some people. You sound like you understand that part of it.
jverda at 2007-7-28 18:41:21 >

Thank you all for your reply's!
I guess it help you to know that the code that I originally posted was from an online quiz that the author of our book provides as a resource. I think he tries to trip you up with code that you wouldn't normally see just to make sure you understand the concepts and what "not to do". I do understand that for most classes you instantiate with the keyword "new", but since Strings are used so often, they made an easier way for them...i.e. String s = "Better way";
Again, I appreciate your reply's and hope that I will learn enough that I can help somebody else someday.
See ya,
Jimbo