String with the ==

Why does this work?

Page 1 of 1

7 Replies - 687 Views - Last Post: 10 September 2009 - 01:05 PM Rate Topic: -----

#1 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

String with the ==

Posted 10 September 2009 - 08:22 AM

Why does this work? I was expecting it not to equal, but it does.
Thanks

		String s = "abcd";
		String s2 = "abcd";
		if(s == s2) {
			System.out.println("equal");  }
		else  {
			System.out.println("Not equal"); }



OUTPUT:
equal
Is This A Good Question/Topic? 0
  • +

Replies To: String with the ==

#2 Guest_Neumann*


Reputation:

Re: String with the ==

Posted 10 September 2009 - 08:28 AM

Strings literals are kept in the static string pool in Java, so that whenever you use a string literal that is already contained in the pool it is being reused instead of wasting the memory creating a new one. That is what's happening in your program - "abcd" is already contained in the string pool, so s2 assigned the same address as s.

You can force the creation of strings like this - String s2 = new String("abcd");, now the "==" won't work.

This post has been edited by Neumann: 10 September 2009 - 08:32 AM

Was This Post Helpful? 1

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: String with the ==

Posted 10 September 2009 - 09:36 AM

As with all objects, the == operator only determines if the addresses are the same. Use the .equals() method to compare Strings like so:

if(string1.equals(string2))


Was This Post Helpful? 0
  • +
  • -

#4 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: String with the ==

Posted 10 September 2009 - 09:47 AM

Or, you can use this method to compare two strings too.
s1.compareTo(s2);



Hope that helps! :)
Was This Post Helpful? 0
  • +
  • -

#5 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: String with the ==

Posted 10 September 2009 - 10:16 AM

The reason why you don't use == is not because it comes up false when they are the same, its because it can be inconsistent with caching and efficiency adjustments you can't know whether or not its going to come up true or false at any one time.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Neumann*


Reputation:

Re: String with the ==

Posted 10 September 2009 - 11:05 AM

Wow, gotta love how everybody here completely missed the whole point of this thread.
Was This Post Helpful? 0

#7 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: String with the ==

Posted 10 September 2009 - 11:10 AM

View PostNeumann, on 10 Sep, 2009 - 01:05 PM, said:

Wow, gotta love how everybody here completely missed the whole point of this thread.


lol!

Thanks for the answer Neumann!
Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: String with the ==

Posted 10 September 2009 - 01:05 PM

View PostNeumann, on 10 Sep, 2009 - 12:05 PM, said:

Wow, gotta love how everybody here completely missed the whole point of this thread.


Yep.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1