Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating string

 
swapna hyderabad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can create the string in two ways:

String s="abc";

String s=new String("abc");


what is the diff b/w two?
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by swapna:


"Swapna"
Please check your private message for an important administrative matter.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Due to the mechanics of how strings work, the second one creates more String objects than the first. Don't worry about how or why, just know that the first way is almost always preferred.
 
swapna hyderabad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"the second one creates more String objects than the first."

Please tell me the exact difference....question was asked in an interview:-(.....why second one creates more than one object....
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please confirm that your displayed name bears some similarity to your real name; Hyderabad is a city, but I know there are some people who have names like "London" and "Lancaster" and "York(e)."

new String("swapna") involves two String objects.
  • 1: new String("swapna")
  • 2: new String("swapna")
  • There is the "swapna" which you have written in the code as a String literal. If you have used "swapna" anywhere else, the JVM will use the same object in the String pool for both. The new String bit, however, will create a second object, whose value is (would you believe) "swapna".

    This is called a copy constructor, and is probably in the String class more for completeness' sake than because people need it all the time, as you find out if you read the API.
     
    swapna hyderabad
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    string pool is containing the literals: swapna,abc,campbell.....

    I hav ecreated the string

    new String("abc")

    inthis scenario also it creates two strings?..means the string poll is already having "abc" literal..
     
    Maneesh Godbole
    Saloon Keeper
    Posts: 11027
    12
    Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "swapna hyderabad",
    In addition to what Campbell said, the ranch naming policy dictates that the names should not be obviously fictitious.

    Please do take out time to go through the naming policy. You can adjust your screen name by clicking here
     
    swapna hyderabad
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    Please give me the reply
     
    Campbell Ritchie
    Sheriff
    Posts: 48917
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have already told you, but with "swapna" rather than "abc."

    What don't you understand?
     
    Garrett Rowe
    Ranch Hand
    Posts: 1296
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Consider the normal way an object is created with the new operator:

    new <classname>(<args>) ;

    So when you write:

    new String("myString");

    what is the type of the argument to the String constructor?... That's right, its a String. So if you already have a String, you have to have one to pass to the String constructor, why do you need the extra characters to create another String. The answer is you usually don't. I can say with some degree of certainty that you will never need to use the new String(String) constructor when the argument is a String literal. There are some rare cases when you will have to do this when the argument is not a literal String, but rather a String created by some other method. But the takeaway from all this is that there are indeed two String objects created, the String object created by the String literal, and the String object that is created as a result of calling the String constructor.
    [ December 01, 2008: Message edited by: Garrett Rowe ]
     
    Campbell Ritchie
    Sheriff
    Posts: 48917
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, GR; you are more patient than I am
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic