/******************************************************************************
 * Example of Strings Equality
 ******************************************************************************/

public class stringEqual
{
    public static void main (String[] args) 
    {
        String strA = "hello world";
        String strB = "hello world";
        String strC = strA;
        
        String refA = Integer.toHexString(System.identityHashCode(strA));   //get the value of the reference for strA 
        String refB = Integer.toHexString(System.identityHashCode(strB)); 
        String refC = Integer.toHexString(System.identityHashCode(strC)); 

        System.out.println("strA location: \t" + refA + "\t Value: " + strA );
        System.out.println("strB location: \t" + refB + "\t Value: " + strB );
        System.out.println("strC location: \t" + refC + "\t Value: " + strC );

        if (strA == strB)  
            System.out.println("strA == strB?       " + "Yes" );
        else         
            System.out.println("strA == strB?       " + "No" );
        
        if (strA == strC)  
            System.out.println("strA == strC?       " + "Yes" );
        else         
            System.out.println("strA == strC?       " + "No" );

        if (strA.equals(strB))
            System.out.println("strA.equals(strB)?  " + "Yes" );
        else         
            System.out.println("strA.equals(strB)?  " + "No" );

        if (strA.equals(strC))
            System.out.println("strA.equals(strC)?  " + "Yes" );
        else         
            System.out.println("strA.equals(strC)?  " + "No" );
            
        System.out.println();

      //--------------------------------------------------------
      
        String strX = "Hello World";
        String strY = "Hello" ; 
        String strZ = new String("Hello World"); 

        strY += " World";
        
        String refX = Integer.toHexString(System.identityHashCode(strX));    
        String refY = Integer.toHexString(System.identityHashCode(strY)); 
        String refZ = Integer.toHexString(System.identityHashCode(strZ)); 

        System.out.println("strX location: \t" + refX + "\t Value: " + strX );
        System.out.println("strY location: \t" + refY + "\t Value: " + strY );
        System.out.println("strZ location: \t" + refZ + "\t Value: " + strZ );

        if (strX == strY)  
            System.out.println("strX == strY?       " + "Yes" );
        else         
            System.out.println("strX == strY?       " + "No" );

        if (strX == strZ)  
            System.out.println("strX == strZ?       " + "Yes" );
        else         
            System.out.println("strX == strZ?       " + "No" );

        if (strX.equals(strY))
            System.out.println("strX.equals(strY)?  " + "Yes" );
        else         
            System.out.println("strX.equals(strY)?  " + "No" );

        if (strX.equals(strZ))
            System.out.println("strX.equals(strZ)?  " + "Yes" );
        else         
            System.out.println("strX.equals(strZ)?  " + "No" );
    }
}