Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

What is the difference between string s="abc" and string s =new string("abc").

I searched for it,some say no difference some say yes .

can anybody explain me or give links to the sites.


Thanks.
Posted

For Example:

String s = "Marcus";

It is a string literal.
String s2 = new String("Marcus");

A new instance is created as you have created the string with the new operator.

But again if you have created a new string like
String s1 = "Marcus"

First it searches if the string "Marcus" is there in the string pool,(in this case,as it is already there),now it just reassigns the same to s1.
 
Share this answer
 
v3
Comments
RDBurmon 10-Jan-13 1:15am    
Need some more clarity
Matt T Heffron 10-Jan-13 13:31pm    
OP asked about C# not Java.
in C# this new statement will not even compile.
No constructor that takes a string argument.
1- String objects pool issue

String object is immutable; thats mean when you say
C#
String s = "Mohammed";
s += "El-Adawi";

on runtime this means that a new String "Mohammed El-adawi" is created
and assigned to reference s
and String "Mohammed" now is eligable for gc

why sun have to do that ?, because String literals
(any thing between " " is reccognized by compiler as String literal)
are put by compiler in String pool, then if compilar found the same literal again for example
String s = "abc";
String m = "abc"; // compiler now found the same String literal

compiler will have one copy of "abc" and will assign both s,m to the same String literal, (at run time one object constructed and asigned to s,m)

if m change the String, s will point to the wrong thing, thats why String is immutable.

2- answer to your question
String s = "Marcus"; // One String object is built and assigned to s.

String s2 = new String("Marcus");
/* now compiler found "Marcus" in the pool
so it will not create a new object
new operator creates new object contains 
Marcus at runtime
*/


String s3 = new String("Mohammed Not Marcus");
// compiler adds "Mohammed Not Marcus" to String pool but with no 
// reference assignment, and String object created and assigned to S3 // at runtime 


3- Another thing
String s1 = "abc";
String s2 = "abc"; 
System.out.println(s1 == s2); // true

but
String s1 = "abc";
String s2 = new String ("abc");
System.out.println(s1 == s2); // false 

I hope that is helpfull
 
Share this answer
 
v3
Comments
RDBurmon 10-Jan-13 1:15am    
Good explanation prashant
prashant patil 4987 10-Jan-13 1:32am    
thanks Rahul....
- Prashant Patil
Matt T Heffron 10-Jan-13 13:29pm    
Your example like this was wrong (I fixed the new statement, string does not have a constructor that takes a string, were you thinking of Java?):
String s1 = "abc";
String s2 = new String ("abc".ToCharArray());
Debug.WriteLine(s1 == s2); // false (NO, this is true!!)
visit link
nice explanation with figure that will clear doubts :)
Topic : 2.1 String Literal vs. String Object
http://www3.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html[^]
Happy Coding!
:)
 
Share this answer
 
Comments
RDBurmon 10-Jan-13 1:14am    
Very good link Aarti .
Aarti Meswania 10-Jan-13 1:15am    
Thank you! :)
fjdiewornncalwe 10-Jan-13 9:28am    
Java? OP is asking for C#
Strings are reference type so they refer to the memory location rather than creating a copy.
when we use new operator it will allocate a new memory location from the heap rather than refering to an exsisting location of same String
Here is an example
Java
public class TestString{
public static void main(String[] args){
String a = "Java";
String b = new String(a);
String c = "Java";
System.out.print((a == b)+",");
System.out.print((a == c));
}
}

This will give output as : false,true

Here in first line as "Java" String is not there in pool, a new instance of String with value Java will get created. In second line, we called new, so again a new instance will get created attached to reference b. In third line as "Java" string is already present in the pool, no new instance will be created but String c will make reference to "Java" string already present.
This is also called 'Object Pooling' to enhance pooling.
 
Share this answer
 
Comments
Matt T Heffron 10-Jan-13 13:30pm    
OP asked about C# not Java.

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