3

I did search on this but the keywords must be too generic to narrow down the relevant bits. Why are both ways of declaring a string valid in android and is there any difference?

4
  • 1
    They are the same. I would recommend checking out this: docs.oracle.com/javase/tutorial/java/javaOO/… are basically instantiating a new instance of the String object. Commented Jan 4, 2012 at 15:54
  • 1
    Strings are immutable, so I suspect that there is no difference. You could examine the bytecode generated by each to see if there is any difference. Commented Jan 4, 2012 at 15:55
  • Probably should be closed as copy of String vs new String() Commented Jan 4, 2012 at 16:05
  • Thanks for the links, and apologies for duplicating the question. I spent a good 10 minutes trying to find a similar Q before posting but there were way too many results to scroll through and many were c++ and ios related. This is what I wanted: "You very rarely would ever want to use the new String(anotherString) constructor. From the API..." Commented Jan 4, 2012 at 16:12

4 Answers 4

4

Using the new keyword you create a new string object, where using foo = "bar" will be optimized, to point to the same string object which is used in a different place in your app.

For instacne:

String foo = "bar";
String foo2 = "bar";

the compiler will optimize the above code to be the same exact object [foo == foo2, in conradiction to foo.equals(foo2)].

EDIT: after some search, @Sulthan was right. It is not compiler depended issue, it is in the specs:

A string literal always refers to the same instance (§4.3.1) of class String.

Sign up to request clarification or add additional context in comments.

4 Comments

This is not decided by compiler. The behavior is strictly defined.
@Sulthan: Can you provide some reference to that statement? I failed to see it in the specs, but will gladly modify my answer if such can be provided.
Docs for String.intern(). All literal strings and string-valued constant expressions are interned
@Sulthan: You were right, I found it in the specs. edited my answer. thanks for your comment.
3

This is java syntax and not only specific to Android. Here is a discussion on this. String vs new String()

Comments

0

This is not only about Android, it's about Java.

When you write "xxxx" it is a literal string. It's a String instance. Note, that all literal strings with the same value are the same instance. See method String.intern() for details.

Example:


String s1 = "abc";
String s2 = "abc";

in this example, s1 == s2 is true.

new String("xxx") is a copy constructor. You take one string (the literal) and you create a new instance from it. Since all strings are immutable, this is usually something you don't want to do.

Example:


String s1 = "abc";
String s2 = new String("abc");

s1.equals(s2) is true
s1 == s2 is false

Comments

0
String x = new String( "x" )

effectively creates 2 Strings. One for the literal (which is a expression with no variable name) and one that you keep as x then. It is the same as:

String x;
{
  String a = "x";
  x = new String( a );
}

Comments

Your Answer

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.