This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes creating string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating string" Watch "creating string" New topic
Author

creating string

swapna hyderabad
Greenhorn

Joined: Nov 20, 2008
Posts: 20
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

Joined: Jul 26, 2007
Posts: 9993
    
    7

Originally posted by swapna:


"Swapna"
Please check your private message for an important administrative matter.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
swapna hyderabad
Greenhorn

Joined: Nov 20, 2008
Posts: 20
"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

Joined: Oct 13, 2005
Posts: 36579
    
  16
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

    Joined: Nov 20, 2008
    Posts: 20
    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

    Joined: Jul 26, 2007
    Posts: 9993
        
        7

    "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

    Joined: Nov 20, 2008
    Posts: 20
    Hi,
    Please give me the reply
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36579
        
      16
    I have already told you, but with "swapna" rather than "abc."

    What don't you understand?
    Garrett Rowe
    Ranch Hand

    Joined: Jan 17, 2006
    Posts: 1296
    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 ]

    Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36579
        
      16
    Thank you, GR; you are more patient than I am
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: creating string
     
    Similar Threads
    Want the difference between String and StringBuffer
    Difference between String
    How many string objects created??
    Difference between s = "" and new String("a")
    string object