• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About == Operator

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why am i getting true for the following code:
if("string"=="string")
{
System.out.println("Equal");
}
else
{
System.out.println("Not Equal")
}

krknagarajan@yahoo.com
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe becasue they are equal!!!
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
output is quite straightforward .
Could you elaborate your doubt ?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Follow the link or try search 'string equal'
Link
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
I think the out put is not very straight forward coz both "string" and "string" are 2 diff objects on a first look but d thing is dat java handles string objects (in d string pool) in such a way dat, if gc is not been run, den it makes d 2 diff reference refer to same object, when de wanna contain d same string value. So wat u get at last is dat both d references point to d same object, i.e. d bit patterns stored in both references is d same and hence u get d result.
I hope diz answers ur question. Thank you. Bye.
Kind regards,
Anish Doshi.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think when you have some confusion where the String is placed just check out using
javap -c classname
this will help you, to refer the JVM Spec to get the Instruction set
hope this helps
Vivek Nidhi
 
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a perfectly valid question. The Java programming language is specifically designed so that string literals such as this are always equal. It accomplishes this by "interning" the string during class loading in a pool of unique strings maintained by the String class. If the string already exists, the intern() method in the String class returns a reference to the existing object, else it adds the string to the pool.
The two subjects you must study to fully understand this subject is the interning of strings and the definition of "computed strings". Computed strings (e.g. a string returned from a method), unlike string literals, are not automatically added to the pool of unique strings. They can, however, be added to the pool by invoking the intern() method yourself.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
when 2 string literals are created they denote the same object.this is the same as
String s = "rajeev";
String s1 = "rajeev";
s == s1 ; // true
But when u create an object of String then both of them doesnot denote the same object
String s = new String("rajeev");
String s1 = new String("rajeev");
s == s1 ; // false
but s.equals(s1) ; // true coz they check the values
hope this solves the problem
 
Doug Dunn
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the String(String original) constructor would come up in response to this post. It is the "copy constructor" for the String class, and is specifically designed to return a different object than the one passed. Here are the API docs:
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic