java string == and equals of use

 

. 1  Package com.example.base.string;
 2  
. 3  public  class MyStringTest {
 . 4  
. 5      public  static  void main (String args []) {
 . 6          testString ();
 . 7      }
 . 8      
. 9      Private  static  void testString () {
 10          String = Hello " the Hello " ;
 11          string hello2 = new new string (" the Hello " );
 12          string LO =" LO " ;
 13          // . == comparison is false references .hello is a string constant expression, and implemented as a call interned, in various exemplary shared (string pool)
14          // of hello2 is an object, points to an address in heap 
15          System.out.println (Hello == of hello2);             // to false
 16          // to true comparison is the equals the value of two objects. 
. 17          the System.out .println (hello.equals (hello2));        // to true
 18          // to true Intern () will check the current string pool that contains the string. It returns a string that contains the pool, or to add a string object to the pool, and returns the string object reference
 19          // (end result is to return a pointer to a string constant pool references) 
20          System.out.println (Hello hello2.intern == ());    // to true
 21 is          // . are constants to true two expressions (determined at compile time), all references to a string pool 
22 is          System.out.println (Hello = = "the Hello");            //to true
 23 is          // . to true constant expression under two different classes, still pointing to the same reference cell string 
24          System.out.println (Other.hello == Hello);        // to true
 25          // to true different. two constant expression in the package is still pointing to the same reference cell string
 26 is          // System.out.println (com.example.base.string.other.Other.hello == Hello);      // to true
 27          // . are constants to true two expressions, are references to a string pool 
28          System.out.println (Hello == ( "Hel" + "LO"));     // to true
 29          // to false after. It is not constant expression, is calculated by concatenating the string runs (lo is an object, not a steady "xxx"), the newly created object will be 
30          System.out.println (Hello == ( "Hel" + LO)) ;       // false
31 is          // . The new object to true intern () returns a pointer to the string of reference cell 
32          System.out.println (Hello == ( "Hel" + LO) .intern ());      // to true 
33 is      }
 34 is  }
 35  class Other {
 36      static String = Hello "the Hello" ;
 37 [ }

 

Guess you like

Origin www.cnblogs.com/gc65/p/java.html