1

I am wondering why in the following code:

   String args0 = args[0];
   String args1 = args[1];
   args0.intern();
   args1.intern();

   if (args0==args1){
      System.out.println("Success");
   } else {
      System.out.println("Failure");
   }

by passing as a line command argument 2 identical strings it gives back failure.

Thanks in advance.

C Snover
  • 17,908
  • 5
  • 29
  • 39
Rollerball
  • 12,618
  • 23
  • 92
  • 161

6 Answers6

4

Being String inmutable, you'd need to assign the instance intern() returns in order to make the variables reference the interned instance, so that the == comparison would return true.

String args0 = args[0];
String args1 = args[1];
args0 = args0.intern();
args1 = args1.intern();

if (args0==args1){
   System.out.println("Success");
} else {
   System.out.println("Failure");
}

In your example args0 and args1 are still referencing the original distinct instances in the array.

Xavi López
  • 27,550
  • 11
  • 97
  • 161
1

You have to do:

if (args0.intern() == args1.intern()){
    System.out.println("Success");
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

Strings are immutable.

.intern() returns a (potentially) different String instance that is in the intern pool.

You're throwing this instance away.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Java automatically interns String literals. This means that in many cases, the == operator appears to work for Strings in the same way that it does for ints or other primitive values.

Since interning is automatic for String literals, the intern() method is to be used on Strings constructed with new String().

You can refer this example also:

public class Test
{
   public static void main(String args[]){
      String Str1 = new String("Welcome to Tutorialspoint.com");
      String Str2 = new String("WELCOME TO SUTORIALSPOINT.COM");

      System.out.print("Canonical representation:" );
      System.out.println(Str1.intern());

      System.out.print("Canonical representation:" );
      System.out.println(Str2.intern());
   }
}
Manish Doshi
  • 1,205
  • 1
  • 9
  • 17
0

(I assume that args0 and args1 holds Strings with same value)

  1. args0.intern() will check if String pool already contains String with same value as String from args0.
  2. If pool contains such String reference to it will be returned, if not new String with same value as args0 will be created, placed in pool reference to it returned.

after

args0.intern();
args1.intern();

args0 and args1 still hold Strings used in command line so they are different from Strings in pool.

To change it try maybe

args0 = args0.intern();
args1 = args1.intern();

now args0 = args0.intern(); will place "put" String from args0 in pool update args0 to hold that String. After args1 = args1.intern(); intern will see that pool contains same string (placed earlier with args0.intern()) so will just return reference to it and store it under args1.

Now if(args0 == args1) should be true because both references hods the same String object.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

Strings are immutable and its methods always return the altered String:

 args0 = args0.intern();
 args1 = args1.intern();
Michael Lang
  • 3,902
  • 1
  • 23
  • 37