0

I'm reading a Java magazine and the autor use the intern() method to compare Strings.

String ns;
String erg; 

if( ns != erg.intern() ) {
    System.out.println("is not equals");
}

I never heard about this method. I'm always using equals instead.

if( !ns.equals(erg) ) {
    System.out.println("is not equals");
}

When should I use intern() and when equals? or are they the same?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
user3232446
  • 439
  • 2
  • 14
  • 3
    Basically: don't use .intern(); more often than not it's not worth the trouble – fge Dec 04 '15 at 13:05
  • I don't think this is the same question. @user3232446 [`intern()`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern()) uses a pool of strings. Basically don't use it. It is not obvious what your intentions are and it has a bunch of gotchas, where as `equals()` is obvious. – Michael Lloyd Lee mlk Dec 04 '15 at 13:10

0 Answers0