3 Replies - 307 Views - Last Post: 17 January 2010 - 11:58 AM Rate Topic: -----

#1 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Comparsions

Posted 17 January 2010 - 11:01 AM

Hi. This question will be rather theoretical. My teacher said to examine this piece of code, and try to guess results.
public class Compare{ 
	 public static void main(String[] args){ 
		 String s1 = "foo"; 
		 String s2 = "foo"; 
		 System.out.println(s1 == s2); 
		 System.out.println(s1.equals(s2)); 
		 String s3 = new String("foo");   
		 System.out.println(s1 == s3);  
		 System.out.println(s1.equals(s3));  
	 } 
}


I was sure that result will be:
false
true
false
true


But, in fact, it's
true
true
false
true


And I don't get why! s1 == s2 looks like reference comparsion, and references should not be equal. So, maybe any info why this works that way?

Is This A Good Question/Topic? 0
  • +

Replies To: Comparsions

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Comparsions

Posted 17 January 2010 - 11:37 AM

This is one of these funny ones. When comparing two Strings, you should always use .equals. == is a comparison to make sure two Objects are exactly the same. So lets look at yours.

System.out.println(s1 == s2);

This comes out true because the two Objects are the same.

System.out.println(s1.equals(s2)); 

This is the correct way to do it, so obviously true.
System.out.println(s1 == s3);

Here, s3 is a new String Object, and when comparing that using ==, it is different to a String reference. Therefore, this is false.

 System.out.println(s1.equals(s3));  

And this would be true because its comparing the contents of the String and not the type.
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4318
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: Comparsions

Posted 17 January 2010 - 11:42 AM

Java creates an internal pool of literals and constants. So when you specify "foo" for s1 it creates it in the pool. All literals are declared ONCE so when you use s2 you are going to use to same literal from the pool. In a way, s1 and s2 are pointing to the same literal string. They ARE the same object. Now the trick is down below where you declare a third string with the new keyword. This is going to create a separate String object who has a value of "foo", it is however not a literal string like the others. Thus it is a different object.

You are right in thinking terms of comparing objects. Its just that in this case s1 and s2 point to the same object in the literal string pool. It is also because of this comparison of objects which is why you should really use .equals() when it comes to evaluating the "Value" of the objects than the objects themselves. :)
Was This Post Helpful? 1
  • +
  • -

#4 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Comparsions

Posted 17 January 2010 - 11:58 AM

Thanks, I get it now ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1