3

I don't understand why you create a String object as follows:

String stringObj = "";

I think, it should be:

String obj = new String();
Stephen C
724k95 gold badges849 silver badges1.3k bronze badges
asked Jul 6, 2013 at 2:01
5
  • It should be noted that there is no need to initialize your "stringObj" reference variable when you declare it. So long as a value is eventually assigned to it before you use it somewhere, you can simply say String stringObj;. When a new value is assigned to "stringObj", it is a reference to a different object -- it's not modifying the String that the variable may have previously referenced. Commented Jul 6, 2013 at 2:37
  • @WilliamMorrison, compare?! Commented Jul 6, 2013 at 3:21
  • Covers the same subjects. Am I wrong in this? Perhaps it is not truly duplicate, I will remove flag. Commented Jul 6, 2013 at 3:22
  • @WilliamMorrison, Oh I see, both have common with string interning. Excuse me Commented Jul 6, 2013 at 3:24
  • I will leave it be. This question may appear on some searches the other would not. Commented Jul 6, 2013 at 3:33

2 Answers 2

5
String stringObj = "";

Is called as String literals. They are interned.

What that means is, let us say if you have

String stringObj = "";
String stringObj2 = "";
String stringObj3 = "";

All 3 references (stringObj, stringObj2, stringObj3) points to same memory location.

String obj = new String();

This syntax creates new String object on every invocation.

What that means is, let us say if you have:

String stringObj = new String();
String stringObj2 = new String();
String stringObj3 = new String();

Three new (separate) String objects will be created and point to different memory locations.

FThompson
28.8k13 gold badges63 silver badges95 bronze badges
answered Jul 6, 2013 at 2:02
Sign up to request clarification or add additional context in comments.

2 Comments

you can explain more detail for me?
As an aside, I wouldn't worry about whether or not two String objects point to the same memory location (since you can't modify them anyway). Thus, don't use == to compare strings. Use s1.equals(s2).
4

Java compiler has special syntax for creating string objects from string literals. When you write

String stringObj = "";

Java creates a new String object, ans assigns it to stringObj.

Note that this is not directly equivalent to new String(), because strings instantiated from string literals are interned. This means that strings created from the same literal are not only object-equal, but also reference-equal (i.e. reference the same object).

answered Jul 6, 2013 at 2:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.