May 22 2008, 10:43 AM
This is probably a very basic question but anyway, here it goes:
In Java, why can you use methods of the String class without creating a new String object and assigning that object to a reference variable, like all the other classes? That is, why can you do this:
String s = "hello"
instead of this:
String s = new String();
I understand that the first part works and it is correct. I just don't know what's the logic behind it.
May 22 2008, 10:52 AM
The book I am reading basically said that modern versions of Java made this change as a convenience to programmers by reducing the additional steps of declaring and instantiating the string object.
In fact you could do:
Which is similar to: <untested>
System.out.println( new String("Hello").length());
May 22 2008, 10:53 AM
in JAVA strings are immutable objects so the first creates a reference to an object that holds the content 'hello'.
It doesn't really matter if you use the explicit String constructor or assign/create the object with double quotes. Either way the string object will be assigned to the reference variable. Now something really weird happens if you pass a double quoted string to the explicit String constructor e.g.:
String s = new String("hello");
This would actually create two instances of the string...one implicitly inline for the argument which then gets copied to the new string instance that is created. So as you can see the explicit String constructor is kind of useless and normally not utilized unless you want to make an explicit copy of an existing variable.
Hope that makes some sort of sense...or maybe I just confused you more.
May 22 2008, 12:59 PM
Thank you for your input you guys.
Richard, what book are you reading if you don't mind me asking? Also, do they give this 'new ability' of the String class a formal name? Are there other classes like it? Thanks!
Oli, thanks for you input but I feel my original question wasn't answered. Maybe if I rephrase my question:
What makes the class String 'special' that you can access its methods with a reference variable and the dot operator without creating an instance of that class (that is, without saying 'new')?
"in JAVA strings are immutable objects so the first creates a reference to an object that holds the content 'hello'. It doesn't really matter if you... ...assign/create the object with double quotes. Either way the string object will be assigned to the reference variable."
How is this possible? how can you assign to a reference variable the content 'hello' without creating the object first? As far as I was aware, reference variables could only point to an object on the heap (created by saying 'new') or point to nothing (null).
May 22 2008, 01:02 PM
Strings are different...utilization of the double quotes will actually create the object with the content you provide. It's a shortcut that is easier then explicitly instantiating a new object and assigning its content.
May 22 2008, 01:13 PM
Oh I see, I was unaware of that. Does that have a formal name in Java? I mean, creating the String object with the double quotes?
May 22 2008, 01:28 PM
Maybe an implicit string declaration? The explicit version would use the String constructor.
May 22 2008, 02:19 PM
Thanks a lot Oli!
May 22 2008, 02:28 PM
No problem...good luck on future projects!
May 22 2008, 02:53 PM
Richard, what book are you reading if you don't mind me asking? Also, do they give this 'new ability' of the String class a formal name? Are there other classes like it?
Here is the book:http://www.amazon.com/Introduction-Java-Pr...=pd_sim_b_img_1
I don't think that they give it any official name. They just describe the behavior that the java compiler takes when it sees and interprets this syntax. They also give the justification for this behavior.
What is nice about the book is that it points out language construction changes that occur between java versions. For example, In Java 1.5 a new "for each" construct was added. They draws attention to each change an then explain how it can be implemented usefully in code.
May 22 2008, 10:28 PM
Thank you Richard, that is very helpful.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here