aspose file tools*
The moose likes Beginning Java and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String" Watch "String" New topic
Author

String

Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
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
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.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
David Woods
Greenhorn

Joined: Sep 04, 2004
Posts: 4
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
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

Joined: Sep 15, 2004
Posts: 16
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String