स्ट्रिंग की तुलना करने के लिए मेरे पास कोड की निम्नलिखित पंक्तियां हैं। str1 str2 के बराबर नहीं है, जो समझ में आता है क्योंकि यह ऑब्जेक्ट संदर्भ की तुलना करता है। लेकिन फिर एस 1 एस 2 के बराबर क्यों है?संदर्भ तुलना (==) जावा में कुछ तारों के लिए क्या काम करता है?
String s1 = "abc";
String s2 = "abc";
String str1 = new String("abc");
String str2 = new String("abc");
if (s1==s2)
System.out.println("s1==s2");
else
System.out.println("s1!=s2");
if (str1==str2)
System.out.println("str1==str2");
else
System.out.println("str1!=str2");
if (s1==str1)
System.out.println("str1==s1");
else
System.out.println("str1!=s1");
आउटपुट:
s1==s2
str1!=str2
str1!=s1
तो आपके द्वारा पहली खोज की कोशिश की? :( –
http://stackoverflow.com/questions/7144059/java-string-pool-object-creation, http://stackoverflow.com/questions/4033625/if-compares-references-in-java-why-does इन-स्ट्रिन के साथ-मूल्यांकन-से-सत्य, http://stackoverflow.com/questions/6377058/string-reference, http://stackoverflow.com/questions/1903094/java-strings-and-stringpool –
(मैं इसे बंद नहीं कर रहा हूं, क्योंकि मुझे एक और प्रश्न * इस तरह केंद्रित नहीं है * लेकिन मुझे यकीन है कि यह मौजूद है) –