Click here to Skip to main content
12,954,522 members (55,188 online)
Rate this:
Please Sign up or sign in to vote.
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.

Posted 9-Jan-13 18:18pm
Rate this: bad
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.
Rahul Dhoble 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.
Rate this: bad
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

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

I hope that is helpfull
Rahul Dhoble 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!!)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

visit link
nice explanation with figure that will clear doubts :)
Topic : 2.1 String Literal vs. String Object[^]
Happy Coding!
Rahul Dhoble 10-Jan-13 1:14am
Very good link Aarti .
Aarti Meswania 10-Jan-13 1:15am
Thank you! :)
Marcus Kramer 10-Jan-13 9:28am
Java? OP is asking for C#
Rate this: bad
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.
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2017
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