UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Basic Java Question    
 
   
zeta_bosio
post May 22 2008, 10:43 AM
Post #1

UtterAccess Veteran
Posts: 302
From: Pennsylvania



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());

I understand that the first part works and it is correct. I just don't know what's the logic behind it.

Thank you!

Zeta
Go to the top of the page
 
+
rabroersma
post May 22 2008, 10:52 AM
Post #2

UtterAccess VIP
Posts: 1,215
From: Arcadia, California, USA



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:

System.out.println("hello".length());

Which is similar to: <untested>

System.out.println( new String("Hello").length());
Go to the top of the page
 
+
freakazeud
post May 22 2008, 10:53 AM
Post #3

UtterAccess VIP
Posts: 31,413
From: NC, USA



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.:

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.
Go to the top of the page
 
+
zeta_bosio
post May 22 2008, 12:59 PM
Post #4

UtterAccess Veteran
Posts: 302
From: Pennsylvania



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')?

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
Go to the top of the page
 
+
freakazeud
post May 22 2008, 01:02 PM
Post #5

UtterAccess VIP
Posts: 31,413
From: NC, USA



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.
Go to the top of the page
 
+
zeta_bosio
post May 22 2008, 01:13 PM
Post #6

UtterAccess Veteran
Posts: 302
From: Pennsylvania



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
Go to the top of the page
 
+
freakazeud
post May 22 2008, 01:28 PM
Post #7

UtterAccess VIP
Posts: 31,413
From: NC, USA



Maybe an implicit string declaration? The explicit version would use the String constructor.
Go to the top of the page
 
+
zeta_bosio
post May 22 2008, 02:19 PM
Post #8

UtterAccess Veteran
Posts: 302
From: Pennsylvania



Thanks a lot Oli!

Zeta
Go to the top of the page
 
+
freakazeud
post May 22 2008, 02:28 PM
Post #9

UtterAccess VIP
Posts: 31,413
From: NC, USA



No problem...good luck on future projects!
Go to the top of the page
 
+
rabroersma
post May 22 2008, 02:53 PM
Post #10

UtterAccess VIP
Posts: 1,215
From: Arcadia, California, USA



QUOTE
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.
Go to the top of the page
 
+
zeta_bosio
post May 22 2008, 10:28 PM
Post #11

UtterAccess Veteran
Posts: 302
From: Pennsylvania



Thank you Richard, that is very helpful.

Zeta
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th May 2013 - 07:25 AM