Is there any difference between String str="abc";ANDString str=new String("abc");

Showing Answers 1 - 15 of 15 Answers

anu

  • May 30th, 2006
 

String str = "abc"; is a String literal,where as String str = new String("abc") is a String Object.

  Was this answer useful?  Yes

Naveen Jaiswal

  • May 31st, 2006
 

Yes, There is no difference

  Was this answer useful?  Yes

Basically in this context these are different

String str ="abc"

String str2 = new String("abc")

String str1 ="abc"

Note str and str1 are literals hance they point to one reference say A and str2 will point to different reference B as you have used new operator. Literals once initialized and instantiated can be reassigned to different varaibes but new operator provides a new instance.

  Was this answer useful?  Yes

Madhu CM

  • Jul 7th, 2006
 

String str = "abc" creates one object in memory String str = new String("abc") creates two objects in memory and the obj ref str will point to "abc"1)"abc" will create one obj and new String() will create another String obj

  Was this answer useful?  Yes

Guest

  • Jul 21st, 2006
 

1) using new operatorString st=new String("abc");
2) Without using new operator String st="abc";
Once string object is created with or without new operator contents of string object cannot be modified. But we can modified String reference variable.when you are trying to modify string objects ie.. concatinating,repalcing etc.. a new string object will be created every time.
Creating new object every time consumes more memory. To solve this problem sun introduce String Constant Pool mechanism.When you creating string object with new operator a new string will be created every time. It does not use string constant pool.When we are creating string object without new operator following things happened.
a) JVM will check whether the string constant is available in the pool or not.
b) If it is available in the pool same object referece will be assigned to new reference variable.
c) If string constant is not available in the pool a new constant will be created and will be assigned to new reference variable and also placed this new constant in pool.

  Was this answer useful?  Yes

Prashant K S

  • Oct 11th, 2007
 

  I don't think String str=new String("abc") will create new object.

Please consider.......


public class StringBuffer {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = new String("abc");
        
//both the statements returns the same value

        System.out.println(str1.hashCode()); 
        System.out.println(str2.hashCode()); 
       
    }
}


  Was this answer useful?  Yes

class A {
.
.
String s1="abc";
String s2=new String "abc";
.
.
}

The first line creates (sort of, see below) the String "abc" in the String pool, and s1 points to it. The second line creates a new String object, also containing the three characters "abc", and that's just a plain old heap object. The literal "abc" in the second line is the same object as the literal "abc" in the first line; the String constructor actually copies the literal passed to it, so after line 2 you've got two String objects, one in the String pool, and one in the plain old heap.

The String is not actually constructed at line 1. It's constructed and put into the String pool when the class is loaded. All that line 1 does is fetch a reference to the String that's already in the pool, and store it in s1. The distinction really isn't that important until you check the bytecode with a Java disassembler like javap, at which point you'll see that there's a big difference.

Finally, note that you don't "create" references, really. They're not objects; they're just there. It might be useful to worry about counting the references to a given object sometimes, but not so much counting how many references there are to whatever in total.

sensashan

  • Oct 12th, 2009
 

Yes there is a difference. In case of str = "abc" we are creating an anonymous object  and assigning it the reference str. But in case of str = new String("abc"), we are actually creating a new object and assigning this object the str reference. Hence if we code, say
String str1 = "abc";
String str2 = "abc";

then both str1 == str2
and str1.equals(str2) will return true, because both refer to the same anonymous object whose value is "abc".

But if we code
String str1 = new String("abc");
String str2 = new String("abc");

Then str1 == str2 will return false ( since str1 and str2 are references to 2 different String objects )
and str1.equals(str2) will return true since the value of both the objects referenced by str1 and str2 is same i.e. "abc"

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions