This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Objects of class String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects of class String" Watch "Objects of class String" New topic
Author

Objects of class String

Ken Morgan
Greenhorn

Joined: Nov 10, 2000
Posts: 4
I am in the unfortunate position of trying to learn Java at the same time that I'm teaching it! Our textbook is "Java: How to Program," 3rd ed., by Deitel and Deitel. It leaves much to be desired. But I have two questions about objects of class String.
(1) Why don't they need to be created using "new"? The declaration "String str" seems to be sufficient. Deitel says (p. 134) that all objects are created using "new."
(2) Deitel says (p. 370) that objects of class String are "immutable"--cannot be changed after being created. Yet in the very example program he is discussing (p. 369), he declares "String output" and then has numerous lines "output +=..." that obviously CHANGE "output." What is the explanation of this inconsistency?
Thanks
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
First of all, you need a new book. Those Deitel & Deitel ones are such a hard read, I hate them. (unfortunately I don't have any better suggestions).
Yeah, I was always told that String was immutable as well, but I've just played with it, and it does'nt appear to be. What's occuring?
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Strings are immutable. What happens is that a new String object gets created and the reference updated to point to the new object. The original may then be available for garbage collection.
This can be wasteful of memory, so if you are going to do a lot of string manipulation consider using StringBuffer instead.
Strings are objects but they are used so often that they are taken as a special case so it is not necessary to use 'new', though you still can if you want to.
Regards
Graeme
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In answer to your first question, String is an exception to the rule that objects are created using new. IMHO this is because they are handled a little bit differently than other objects and Java optimizes them for its own benefit.
That leads me to your second question. My understanding is that when you try to change a string, a new string is actually created in memory. It may be referenced by the same variable name as before, but it actually points to a new string. That is why it is recommended to use a string buffer when going through a loop to concatenate strings. For example:
<PRE>
String zeroThroughNine;
for(int i=0;i<10;i++){
zeroThroughNine = zeroThroughNine + " " + i;
}
System.out.println(zeroThroughNine);
</PRE>
This code fragment will output " 0 1 2 3 4 5 6 7 8 9" but it also has the nasty side effect of having created ten distinct strings in memory. This isn't really a problem for something as small as this, but in larger applications or longer loops, it could really hose you up. I think this is what is meant by String being immutable.
The solution to this problem, BTW, is to use the StringBuffer class (which is designed for just this purpose) until you are ready to output the string. Then just call the StringBuffer's toString() method and away you go:
<PRE>
StringBuffer zeroToNine = new StringBuffer();
for(int i=0;i<10;i++){
zeroToNine.append(" " + i);
}
System.out.println(zeroToNine.toString());
</PRE>
I hope this helps.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
oops. Sorry Graeme. We were replying at the same time. I didn't mean to step on your post.
Kirti Singh
Greenhorn

Joined: Nov 10, 2000
Posts: 6
Strings can also be considered as primitive type.
This may answer your first part.
By immutable I think we mean that when ever we assign a String object a new value, we assign new reference.
e.g.
String s="cat";
String p="cat";
Now address of object s and p are same as they contain references to the same pool of strings.
thats why a==b is true
Now if p="dog", then a==b will be false as the pool of string or reference has been changes.
Similarly
String r="hat";
String g=new String("dog");
Now r==g will be false as the pool of strings refrenced by
r and g will be from different pools
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Nick
No problem, I prefer your explanation.
Kirti
You have highlighted another special case with Strings. If you don't use 'new' then the JVM will look to see if a String that equals the new String exists, if it does then it simply creates a reference to that existing String object. Hence with
String s = "cat";
String t = "cat";
s and t will reference the same object, so s==t will return true, however if you use
String s = new String("cat");
String t = new String("cat");
different objects will be created, so s==t will return false.

In both cases s.equals(t) will be true.

[This message has been edited by Graeme Brown (edited November 10, 2000).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Objects of class String
 
Similar Threads
Reference to objects
Please clarify this
Strings
about String object
What Java book to get for a beginner?