| 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: 8436
|
|
Originally posted by swapna:
"Swapna" Please check your private message for an important administrative matter.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9948
|
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
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: 32675
|
|
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: 8436
|
|
"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: 32675
|
|
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: 1295
|
|
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: 32675
|
|
Thank you, GR; you are more patient than I am
|
 |
 |
|
|
subject: creating string
|
|
|