This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default String Constructor

 
Michael Pearson
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
Mike
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
prabhatis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
-------

Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic