Full Version: Basic Java Question
UtterAccess Discussion Forums > And More... > JAVA and C#
zeta_bosio
Hi all,
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"
System.out.println(s.length());
instead of this:
String s = new String();
System.out.println(s.length());
Ounderstand that the first part works and it is correct. I just don't know what's the logic behind it.
Thank you!
Zeta
rabroersma
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.
On fact you could do:
System.out.println("hello".length());
Which is similar to: <untested>
System.out.println( new String("Hello").length());
freakazeud
Hi,
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.:
tring 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.
zeta_bosio
Thank you for your input you guys.
ichard, 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')?
You say:
"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).
Thanks again,
Zeta
freakazeud
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.
zeta_bosio
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?
Thanks again!
Zeta
freakazeud
Maybe an implicit string declaration? The explicit version would use the String constructor.
zeta_bosio
Thanks a lot Oli!
eta
freakazeud
No problem...good luck on future projects!
rabroersma
>Here is the book:
http://www.amazon.com/Introduction-Java-Pr...=pd_sim_b_img_1
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.
zeta_bosio
Thank you Richard, that is very helpful.
eta
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.