==, .equals(), compareTo(), and compare()


class EqualsNotEqualTo { 
	public static void main(String args[]) { 
		String s1 = "Hello"; 
		String s2 = new String(s1); 

		// Matching Text inside s1 and s2 
		System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2)); 	// 	returns true because "Hello" = "Hello"
		// Matching Object references
		System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2)); 		//	returns false becuase s1 memory location != s2 memorylocation
	} 
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: