Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
Frank Hale
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Woods
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing like replying to a 4-year-old thread!

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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

Welcome to JavaRanch!

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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Fendel Coulptz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;

im new to programming, thus a bit confused by the huge load of new concepts.
[ October 26, 2004: Message edited by: Fendel Coulptz ]
 
Eric Zanders
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fendel,
being a beginner aswell, I'll try to explain how I get to 3 objects:
Originally posted by Fendel Coulptz:


then how many string objects are created in this example? i don't understand why 3 objects are created.

String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;



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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic