Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 9-Jan-13 19:18pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v3
Comments
Rahul Dhoble at 10-Jan-13 1:15am
   
Need some more clarity
Matt T Heffron at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

1- String objects pool issue
 
String object is immutable; thats mean when you say
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
  Permalink  
v3
Comments
Rahul Dhoble at 10-Jan-13 1:15am
   
Good explanation prashant
prashant patil 4987 at 10-Jan-13 1:32am
   
thanks Rahul....
- Prashant Patil
Matt T Heffron at 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!!)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

visit link
nice explanation with figure that will clear doubts Smile | :)
Topic : 2.1 String Literal vs. String Object
http://www3.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html[^]
Happy Coding!
Smile | :)
  Permalink  
Comments
Rahul Dhoble at 10-Jan-13 1:14am
   
Very good link Aarti .
Aarti Meswania at 10-Jan-13 1:15am
   
Thank you! :)
Marcus Kramer at 10-Jan-13 9:28am
   
Java? OP is asking for C#
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
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.
  Permalink  
Comments
Matt T Heffron at 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)

  Print Answers RSS
0 DamithSL 305
1 OriginalGriff 295
2 Sergey Alexandrovich Kryukov 207
3 Peter Leow 175
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 7,460
1 DamithSL 5,519
2 Sergey Alexandrovich Kryukov 4,954
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100