Click here to Skip to main content
15,888,106 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
I was asked this question in an interview, but could not figure out the answer. I did some google too, but maybe was not in a proper direction. Maybe someone at codeproject can help...

String s = "Codeproject";
String s = new String();

what is the difference in these two statements..?
Waiting for the answer............
Posted
Comments
Pravin Patil, Mumbai 3-Feb-11 13:09pm    
Very good question sam.

This is a very basic and very good question sam.
First line String s = "Codeproject" doesn't create new variable but simply looks for any literal having this value in the string pool. If compiler finds one, s is initialized with this reference.
But the second line String s = new String("Codeproject") (It should have been like this) simply creates new storage in the memory with the value "Codeproject".
so if you write first line 10 times, it will create only one variable and that reference will be stored in ten instances that you declared.
While the second line will create as many variables as you want, every time creating new space in the memory.

You can find a very good reference at http://en.csharp-online.net/CSharp_String_Theory%E2%80%94String_intern_pool[^]

I hope this helps and you crack the interview next time.
All the best.

I found one more very nice link for JAVA string pooling :
http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3[^]

and for .NET :
http://www.webpronews.com/blogtalk/2006/10/17/net-string-internal-pool[^]
 
Share this answer
 
v2
Comments
Rahul Jain, Serious Coder 3-Feb-11 13:14pm    
Absolutely brilliant answer
I liked the way you presented it
Nice attempt
sam, atlanta 3-Feb-11 13:18pm    
Very big explanation.
Let me read it.
Sergey Alexandrovich Kryukov 3-Feb-11 22:06pm    
Sorry, Pravin, this is not exactly so.
What do you mean "doesn't create new variable"? Of course, "s" is a variable, and the variables are not created, it's the instance of the class that is created. A variable is "initialized". In fact, it does the same as String s = new String("Codeproject") in slightly different way.
Another mistake is this: "so if you write first line 10 times, it will..." - not it will cause compilation error, because a variable is already introduced and initialized.

You reference on "string theory" is good and interesting.

Your reference about string internal pool is complete nonsense! Did you check any facts?
For example, the code "Console.WriteLine(s1 Is s2)" is gibberish, it would not event compile!
The learn about referential identity, comparison and their difference object.ReferenceEquals and object.Equals should be used (also "=="). At the same time the topic about string interning is the key to understanding of strings.

Pravin, you should check up facts and learn the detail yourself. Sorry, it wasn't so good this time. I know you can do better.

Respect,
--SA
first one: s is like constant, its value is "codeproject"
second : u r creating the string object s, that can take any string ...
for ex. String s = new String();
s = "anything dynamically or statically"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900