Howcome you can create a String object like: String a = "Hello,World"; What mechanism does it use to automatically create a new String object and store "Hello,World" inside it? I would like create a class like that so I could create new objects just like String does it. Imagine you have something like this: class foo { int value; } Now I can create a new one like: foo bar = 1; So that I wouldn't have to do this: foo bar = new foo(); bar.value = 1; Is the String class a special class that only has this ability? Frank
Carl Trusiak
Sheriff
Joined: Jun 13, 2000
Posts: 3340
posted
0
Originally posted by Frank Hale: Howcome you can create a String object like: String a = "Hello,World"; What mechanism does it use to automatically create a new String object and store "Hello,World" inside it? I would like create a class like that so I could create new objects just like String does it. Imagine you have something like this: class foo { int value; } Now I can create a new one like: foo bar = 1; So that I wouldn't have to do this: foo bar = new foo(); bar.value = 1; Is the String class a special class that only has this ability? Frank
Actually there is something special about the String class. In C++ it is possible to overload operators(= + / etc.). Well, in java it was determined that this could lead to ambiquity so, the functionallity wasn't carried into it EXCEPT for the String class which overloads = and +. From everything I read or been taught you can't duplicate it in your own classes.
So the "String" class is special, eh? It is evidently one of a very select group of classes where you can assign a value directly to the OBJECT itself, as opposed to assigning value to a PROPERTY of that object.
In my travels, I came across at least one other class that works that way: "Date". Does anyone know of any others?
But now for the real questIon: how would I know that if I didn't know that? Is there something I could look for in the signature of String and Date that tells me that they have this rather unique feature?
Ernest Friedman-Hill
author and iconoclast
Marshal
The String class is indeed "special" in that there's a special notation for String literals. But neither "Date", nor any other class, has any similar specialness regarding literal values. A few other classes -- all of them in the java.lang package -- have some other special properties: for instance, java.lang.Object is the only class with no superclass.
As to how you'd know this: the syntax for String literals is generally on page 1 of any intro to Java book. Beyond that, there's plenty of information about String (and the other special classes in java.lang) in the Java Language Specification, which you can read online here.
Originally posted by David Woods: So the "String" class is special, eh? It is evidently one of a very select group of classes where you can assign a value directly to the OBJECT itself, as opposed to assigning value to a PROPERTY of that object.
That's not true. In
String s = "someString"
what happens is that a String object is created for the literal and assigned to the reference called s. Nothin' like "assigning a value directly to an object" - I actually wouldn't know what that should mean...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Fendel Coulptz
Greenhorn
Joined: Sep 23, 2004
Posts: 24
posted
0
String s = "someString"
what happens is that a String object is created for the literal and assigned to the reference called s.
then, following this quote, does that imply
String s = "someString";
and
String s = new String("someString"); ?
ive read in a text that
String a1 = "alibaba and forty thieves"; String a2 = "alibaba and forty theives";
a1 == a2, "Java represents a literal character string internally as a String constant. If the same literal character string appears more than once in a class, then all occurences references the same String constant. Therefore, the variables s and t from our example are assigned the same String reference. As a result, s==t evaluates to true."
does this mean, if the string values are the same, they are referenced to the same object, if they are different, then new string objects are created?
then how many string objects are created in this example? i don't understand why 3 objects are created.
1� object is "Hello" which is referenced by s1 and s2. 2� object is "Pal" and the 3� object is the concatenation of "Hello" (referenced by s2) and "Pal", thus resulting in "HelloPal" being referenced by s3 and s4.
About the a1==a2 case, I believe there is a typo in
String a2 = "alibaba and forty theives";
If I'm wrong, please do forgive AND correct me.
More @ this link The SCJP Tip Line [ October 27, 2004: Message edited by: Eric Zanders ]