Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

A Questions about String ==

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s1="world"; String s2="";

1)S.O.P("helloworld"==("hello"+s1)); // returns false
2)S.O.P("helloworld"==("helloworld"+s2));//returns true
3)S.O.P("helloworld"==(s2+"helloworld")); //returns false

My question is what is the difference and why is the change?

4) S.O.P("helloworld"==("hello"+s1).intern()); returns true.

Please explain why is the difference in the above statements.
Thanks.

[Edited typo inturn -> intern]
[ July 03, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s1 is String object return fasle.objects can't check using == operator.
euals only check objects.

//second statement also false check it properly

//third statement also false

//only == used for primitives data types
//bye anyway good questions
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It returns
false
false
false
true

IMHO, the first three got false because you were trying to compare objects:
But the fourth "true" is confusing indeed..
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you declare a String like String s = "hello" the JVM puts that object in the pool of Strings maintained by the String class, unless a matching String is already there, in which case it points the new String object to that place in memory.

Whenever you use the '==' operator on objects, you are comparing references. So, the following code will return true:


Now, whenever you use the + operator on two Strings, a new String object, and hence a new reference, is always returned, so your first 3 checks all returned false.

When you used the 'intern()' method, however, you forced the JVM to compare your new String to all the Strings in the String pool (mentioned earlier), and return a reference to any one that it found a match for. So, it found that "helloworld" was already there, and returned a reference to it, rather than creating an entirely new object. Of course, "helloworld" == "helloworld".
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saritha,

Please go through this article. It discusses in detail about string comparison.

WHEN ARE TWO STRINGS EQUAL?

Regards,
Anil
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic