It's not a secret anymore!
The moose likes Beginning Java and the fly likes Default String Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Default String Constructor" Watch "Default String Constructor" New topic

Default String Constructor

Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
Although the formal declaration of a String object is:
<code> String myName = new String( "Mike" );</code>
It is common practice to not use the formal Constructor String( String value ); with the new keyword when creating a String object.
<code> String myName = "Mike";</code>
Why does the Java compiler allow this shortcut?
It isn't saving much typing and it seems to confuse Java beginners because they treat Strings like primitive variables when they are reference variables.
I've seen about a thousand questions in reference to String equality for the SJCP exam (:rolleyes it may be prudent to have someone new to Java use the formal String Constuctor.
thinking out loud...
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
String is a rare class in Java. It's allowed to straddle the line between behaving as an object (using new() ) and behaving as a primitive )using direct assignment. The designers knew String objects would be prominent in almost any program. To conserve the run-time cost of creating them, they chose to allow static String assignments. Static assignments are "paid for" at compilation time, where they can be less of a burden.

When you allocate space for a String using new, that object goes to the heap and is garbage-collectible once it's fully dereferenced. A static String remains in program memory no matter what. Even if you completely dereference it, it's there and will get used again if you re-assign its value. Honest!

-- Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Joined: Apr 07, 2001
Posts: 18
In Java, you specify literal strings between double quotes as in:
"I am a literal string of the String type."
You can use literal strings anywhere you would use a String object.
You can also use String methods directly from a literal string as in an earlier program which invokes the length() method on a literal string.
StringBuffer str6 = new StringBuffer( "StringBuffer named str6".length());
because the compiler automatically creates a new String object for every literal string, you can use a literal string to initialize a String object (without use of the new operator) as in the following code fragment from a previous program:
String str1 = "THIS STRING IS NAMED str1";
.The above construct is equivalent to, but more efficient than the following, which, according to The Java Tutorial by Campione and Walrath, ends up creating two Strings instead of one:
String str1 = new String("THIS STRING IS NAMED str1");
In this case, the compiler creates the first string when it encounters the literal string, and the second one when it encounters new String().
so it is advisable to not to use new operator for string creation .. as it is less efficient ..
Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
Thanks for the feedback.
Here's an example of one of the odd things with String that I find interesting. It's pretty cool...

(1) evaluates true
Static String whoAmI is created after myFirstName so it points to the same memory space as myFirstName.
(2) evaluates false
Object guessWho is constructed using String( String value). It is a unique instance of Class String, therefore guessWho points to a different memory address than whoAmI and myFirstName
(3) evaluates true
Object whoAmI and myFirstName obviously hold the same String value.

(4) evaluates true
String1.equals( String2) correctly compares the value of static String myFirstName and Object guessWho

I agree. Here's the link:
subject: Default String Constructor
It's not a secret anymore!