Originally posted by Cathy Song:
Hi,
Can some please explain how this statement is valid?
System.out.println("12345 ".valueOf(54321));
Thanks,
Cathy.
�12345� is String literal. A String literal represents a value, not an object. It is a reference to a String object.
Hi Barkat.
String s = new String(�12345�);
s is a variable. s holds a value. That value is a reference to a String object.
String t = �12345�;
t is a variable. t holds a value. That value is a reference to a String object.
�12345� is a value. It is a reference to a String object. The value of the variable t and the value �12345� refer to the same String object.
To be more precise, "12345" is the source code representation of a value.
Learning is a spiral. You learn something. Then you revisit it and learn some more.
You might need to remind yourself what the value of a reference variable actually
is. A reference variable�s value�in other words, the bit pattern the variable holds�is
not an object. Just as the value of a primitive variable is the bit pattern representing
the primitive (for example, the bits representing the integer value 2), the value of
a reference variable is a bit pattern representing, well, a reference. We�re not using
�traditional� pointers in Java, but you can still think of it as a pointer (not necessarily
a pointer to an object, but a pointer to a pointer to�). A reference variable holds
bits that represent, in a platform-dependent format, a way to get to an object. That�s
really all we care about, and all we�re even allowed to know about reference variables
in Java, unless you happen to be one of the developers of a JVM.
String t = "12345";
1. t is a variable,
2. t holds a value "12345",
3. t and the value "12345" both reffer to String object that was created when above statement was executed.
t holds a value. That value is a reference to a String object.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Each string literal is a reference (�4.3) to an instance (�4.3.1, �12.5) of class String (�4.3.3).
The Java programming language requires that identical string literals (that is, literals that contain the same sequence of characters) must refer to the same instance of class String.
Originally posted by Marlene Miller:
�12345� is String literal. A String literal represents a value. It is not an object, it is a reference to a String object.
Originally posted by Barkat Mardhani:
Now s2 and "12345" are references alright but refferring to different objects; one on the heap and one in pool.
Now s2 and "12345" are references alright but referring to different objects; one on the heap and one in pool.
All references in the run-time constant pool of a class are initially symbolic. I don't have this next part entirely sorted out. I think the reference to the String object somehow replaces the symbol 12345.
SCJP2. Please Indent your code using UBB Code
Don't get me started about those stupid light bulbs. |