Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strings of Same Value in Java?

a quick and confusing question. If Class A and Class B have this inside them:-

String name="SomeName"; 

and both classes are instantiated, is it true that both instances refer to same memory location of variable "name" say when we do this objA.name or objB.name ? which has value "SomeName" and since String is immutable, several instances of both classes of same JVM use the same variable repeatedly? I read somewhere online that, unless there is

String example=new String("something"); 

is used, the former declaration always creates one copy and it is used until all its applications are terminated for reclaiming memory. Note: I see several answers, which one do I count on, can someone conclude. Thank you all for your effort, appreciate it.

like image 581
Daniel Avatar asked Jul 02 '11 16:07

Daniel


People also ask

Can you use == for strings in Java?

To compare these strings in Java, we need to use the equals() method of the string. You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not.

How do you check if strings are the same in Java?

You can check the equality of two Strings in Java using the equals() method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

What does == mean when comparing strings?

String Comparison With Objects Class Objects is a utility class which contains a static equals() method, useful in this scenario – to compare two Strings. The method returns true if two Strings are equal by first comparing them using their address i.e “==”.

How do you know if two strings have the same value?

In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .


2 Answers

Yes, if you create two strings like:

String a = "Hello";
String b = "Hello";

They will be the exact same object. You can test it yourself by doing

System.out.println(a == b);

If they are the same object, then their internal reference to the character array will be exactly the same.

Now, if you did String c = "Hell" + "o";, it would not have the same reference since it would have been (internally) built using StringBuilder.

There is a lot of good information here.

The relevant sections has (Note: The following is copied from that web site):


As mentioned, there are two ways to construct a string: implicit construction by assigning a String literal or explicitly creating a String object via the new operator and constructor. For example,

String s1 = "Hello";              // String literal
String s2 = "Hello";              // String literal
String s3 = s1;                   // same reference
String s4 = new String("Hello");  // String object
String s5 = new String("Hello");  // String object

Strings

Java has designed a special mechanism for keeping the String literals - in a so-called string common pool. If two String literals have the same contents, they will share the same storage locations inside the common pool. This approach is adopted to conserve storage for frequently-used strings. On the other hands, String object created via the new operator are kept in the heap. Each String object in the heap has its own storage just like any other object. There is no sharing of storage in heap even if two String objects have the same contents. You can use the method equals() of the String class to compare the contents of two Strings. You can use the relational equality operator '==' to compare the references (or pointers) of two objects. Study the following codes:

s1 == s1;         // true, same pointer
s1 == s2;         // true, s1 and s1 share storage in common pool
s1 == s3;         // true, s3 is assigned same pointer as s1
s1.equals(s3);    // true, same contents
s1 == s4;         // false, different pointers
s1.equals(s4);    // true, same contents
s4 == s5;         // false, different pointers in heap
s4.equals(s5);    // true, same contents

Edit to add: Run this SSCE to test reference equality between two constant strings in to different classes:

class T {
  String string = "Hello";

  public static void main(String args[]) {
    T t = new T();
    T2 t2 = new T2();
    System.out.println(t.string == t2.string);
  }
}


class T2 {
  String string = "Hello";
}

prints out true.

like image 150
Reverend Gonzo Avatar answered Oct 20 '22 10:10

Reverend Gonzo


If "something" is literally hard-coded into your source code, then the two variables will point to the same in-memory String object.

like image 31
Jesse Barnum Avatar answered Oct 20 '22 10:10

Jesse Barnum