Forums Register Login

About == Operator

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
maybe becasue they are equal!!!
+Pie Number of slices to send: Send
output is quite straightforward .
Could you elaborate your doubt ?
+Pie Number of slices to send: Send
Follow the link or try search 'string equal'
Link
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 710 times.
Similar Threads
Trim()
Please Answer soon with explanation
String Question
== doubt
Doubts in Strings
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:07:06.