String Comparison: equals() vs == Operator in Java Notes in Hindi & English : स्ट्रिंग तुलना: equals() (कंटेंट) बनाम == (मेमोरी एड्रेस रेफरेंस) और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम String Comparison: equals() vs == Operator in Java (स्ट्रिंग तुलना: equals() (कंटेंट) बनाम == (मेमोरी एड्रेस रेफरेंस)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: In Java, the == operator compares memory reference addresses, whereas String.equals() compares actual character sequence content. For case-insensitive equality and lexicographical sorting, String provides equalsIgnoreCase() and compareTo().

अध्याय सार: जावा में == ऑपरेटर मेमोरी एड्रेस रेफरेंस की तुलना करता है, जबकि String.equals() वास्तविक कैरेक्टर कंटेंट की तुलना करता है। केस-असंवेदनशील तुलना और शब्दकोश क्रमबद्धता के लिए String में equalsIgnoreCase() और compareTo() मेथड उपलब्ध हैं।

String Comparison: equals() vs == Operator in Java: Detailed Notes & Architecture (स्ट्रिंग तुलना: equals() (कंटेंट) बनाम == (मेमोरी एड्रेस रेफरेंस))

1. Reference Comparison with == Operator (== ऑपरेटर द्वारा मेमोरी रेफरेंस की तुलना)

[EN] For primitive types like int or char, == compares numerical bit values directly. For any reference type—including String—== checks strict reference identity: it returns true if and only if both variables point to the exact same object address in Heap/SCP memory. Because two String objects constructed via new String("IA") and new String("IA") occupy distinct Heap addresses, s1 == s2 evaluates to false even though their text matches. Conversely, two identical literals String a = "IA"; String b = "IA"; share one SCP address, making a == b coincidentally true—a classic exam trap.

[HI] int या char जैसे प्रिमिटिव टाइप्स के लिए == सीधे उनके मानों की तुलना करता है। लेकिन String सहित किसी भी रेफरेंस टाइप के लिए == रेफरेंस पहचान जांचता है: यह तभी true लौटाता है जब दोनों वेरिएबल हीप या SCP में बिल्कुल एक ही मेमोरी पते की ओर इशारा करें। new String("IA") से बने दो ऑब्जेक्ट अलग-अलग मेमोरी पतों पर रहते हैं, इसलिए समान टेक्स्ट होने पर भी s1 == s2 false होता है। इसके विपरीत दो लिटरल String a = "IA"; String b = "IA"; SCP में एक ही पता साझा करते हैं, जिससे a == b संयोगवश true आता है।

2. Content Comparison with equals() and Null Safety (equals() द्वारा कंटेंट की तुलना और Null सुरक्षा)

[EN] java.lang.Object defines equals(Object obj) using == by default, but java.lang.String overrides equals() to compare strings character by character. First, String.equals() checks if this == anObject for an instant O(1) short-circuit; next, it verifies instanceof String, matches lengths/coders, and compares every character cleanly. Passing null into an existing string—such as str.equals(null)—never throws an exception; it cleanly returns false. However, invoking nullVar.equals("Java") throws NullPointerException, which is why Yoda condition style "Java".equals(input) is preferred in defensive Java code.

[HI] java.lang.Object में equals() डिफ़ॉल्ट रूप से == का उपयोग करता है, लेकिन String ने इसे ओवरराइड करके कैरेक्टर-दर-कैरेक्टर कंटेंट तुलना लागू की है। String.equals() पहले this == anObject जांचता है, फिर instanceof String, लंबाई/कोडर और प्रत्येक कैरेक्टर की केस-सेंसिटिव तुलना करता है। किसी गैर-null स्ट्रिंग पर str.equals(null) कॉल करने पर कोई त्रुटि नहीं आती बल्कि सुरक्षित रूप से false लौटता है। परंतु null रेफरेंस पर nullVar.equals("Java") चलाने से NullPointerException आता है, इसलिए सुरक्षित कोड में "Java".equals(input) लिखा जाता है।

3. Case-Insensitive equalsIgnoreCase() and contentEquals() (equalsIgnoreCase() और contentEquals() मेथड्स)

[EN] When letter casing should be ignored (such as email addresses or Yes/No user prompts), s1.equalsIgnoreCase(s2) compares characters regardless of uppercase or lowercase differences. Note that equalsIgnoreCase(String) takes a String parameter specifically (not Object), and returns false safely if passed null. When comparing a String directly against a StringBuffer or StringBuilder without converting via toString(), String offers contentEquals(CharSequence cs). Remember that calling str.equals(stringBuffer) always returns false because StringBuffer is not an instance of String.

[HI] जब बड़े और छोटे अक्षरों (case) का अंतर अनदेखा करना हो, तब s1.equalsIgnoreCase(s2) केस-इंसेंसिटिव कंटेंट तुलना करता है। ध्यान दें कि equalsIgnoreCase(String) विशेष रूप से String पैरामीटर लेता है (Object नहीं) और null पास करने पर false लौटाता है। बिना toString() में बदले किसी String की तुलना StringBuffer या StringBuilder से करने के लिए contentEquals(CharSequence cs) मेथड दिया गया है। सामान्य str.equals(stringBuffer) हमेशा false लौटाता है क्योंकि StringBuffer, String क्लास का ऑब्जेक्ट नहीं है।

4. Lexicographical Comparison with compareTo() and compareToIgnoreCase() (compareTo() और compareToIgnoreCase() द्वारा शब्दकोश तुलना)

[EN] String implements Comparable<String> by providing int compareTo(String anotherString) for dictionary (lexicographical) ordering. It compares characters at matching indices and returns the difference of the first mismatched Unicode UTF-16 code units (char1 - char2). If all characters up to the shorter string's length match, it returns len1 - len2; an exact match yields 0, a negative int means calling string precedes argument, and positive means it follows. Unlike equals(null), calling s.compareTo(null) throws NullPointerException immediately because ordering relative to null is undefined.

[HI] String ने Comparable<String> इम्प्लीमेंट करते हुए शब्दकोश क्रमबद्धता के लिए int compareTo(String anotherString) प्रदान किया है। यह समान इंडेक्स पर कैरेक्टर्स की तुलना करता है और पहले असमान कैरेक्टर के यूनिकोड मानों का अंतर (char1 - char2) लौटाता है। यदि छोटी लंबाई तक सभी अक्षर मिल जाएं, तो यह लंबाई का अंतर (len1 - len2) देता है; पूर्ण समानता पर 0, पहले आने पर ऋणात्मक और बाद में आने पर धनात्मक मान मिलता है। equals(null) के विपरीत, s.compareTo(null) कॉल करने पर तुरंत NullPointerException उत्पन्न होता है।

Syntax, Formula & Technical Example (String Comparison: equals() vs == Operator in Java)

String a = "Jaipur";
String b = new String("Jaipur");
String c = "jaipur";
System.out.println(a == b);               // false
System.out.println(a.equals(b));          // true
System.out.println(a.equalsIgnoreCase(c));// true
System.out.println("A".compareTo("C"));   // -2
System.out.println("Java".equals(null));  // false

String Comparison: equals() vs == Operator in Java Quick Reference & Comparison Table

Comparison MechanismReturn Type & SemanticsNull / Special Behavior
s1 == s2boolean; compares memory reference addressnull == null returns true safely without error
s1.equals(obj)boolean; case-sensitive content matchs1.equals(null) returns false; false for StringBuilder
s1.equalsIgnoreCase(s2)boolean; case-insensitive content matchAccepts String parameter; returns false on null
s1.compareTo(s2)int; Unicode lexical diff (0, <0, >0)Throws NullPointerException if s2 is null

String Comparison: equals() vs == Operator in Java Top 20 Bilingual MCQs with Answers

Q1. What does == check when applied to two String reference variables s1 and s2?

(दो String रेफरेंस वेरिएबल्स s1 और s2 पर == ऑपरेटर लगाने पर यह क्या जांचता है?)

(A) Whether s1 and s2 contain identical characters

(B) Whether s1 comes before s2 alphabetically

(C) Whether s1 and s2 refer to the exact same object memory address

(D) Whether s1 and s2 have the same length only

Correct Answer: (C)

[EN] For reference types in Java, == strictly evaluates object reference identity (memory address equality).

[HI] जावा में रेफरेंस टाइप्स के लिए == केवल यह जांचता है कि दोनों रेफरेंस एक ही मेमोरी पते की ओर संकेत कर रहे हैं या नहीं।

Q2. What is the output of: String s1 = new String("RSMSSB"); String s2 = new String("RSMSSB"); System.out.print((s1 == s2) + " " + s1.equals(s2));?

(String s1 = new String("RSMSSB"); String s2 = new String("RSMSSB"); System.out.print((s1 == s2) + " " + s1.equals(s2)); का आउटपुट क्या होगा?)

(A) true true

(B) false false

(C) false true

(D) true false

Correct Answer: (C)

[EN] new String() creates two distinct Heap objects so s1 == s2 is false, whereas equals() compares characters and returns true.

[HI] new String() हीप में दो अलग ऑब्जेक्ट बनाता है इसलिए s1 == s2 false है, जबकि equals() कंटेंट मिलाकर true देता है।

Q3. What happens when executing String s = "Test"; System.out.println(s.equals(null));?

(String s = "Test"; System.out.println(s.equals(null)); निष्पादित करने पर क्या होगा?)

(A) Prints true

(B) Compile-time error

(C) Throws NullPointerException

(D) Prints false without throwing any exception

Correct Answer: (D)

[EN] Per the Object.equals contract, passing null to equals on a valid non-null object cleanly returns false.

[HI] Object.equals नियम के अनुसार गैर-null ऑब्जेक्ट पर equals(null) कॉल करने पर बिना किसी त्रुटि के false लौटता है।

Q4. What does "Cat".compareTo("Dog") return in Java?

(जावा में "Cat".compareTo("Dog") क्या मान लौटाता है?)

(A) false

(B) 0

(C) 1

(D) -1

Correct Answer: (D)

[EN] At index 0, 'C' (Unicode 67) minus 'D' (Unicode 68) equals -1.

[HI] इंडेक्स 0 पर 'C' (यूनिकोड 67) में से 'D' (यूनिकोड 68) घटाने पर -1 प्राप्त होता है।

Q5. What does "Java".equals(new StringBuilder("Java")) return?

("Java".equals(new StringBuilder("Java")) क्या लौटाता है?)

(A) Compile-time error

(B) true

(C) Throws ClassCastException

(D) false

Correct Answer: (D)

[EN] String.equals checks instanceof String first; since StringBuilder is a different class, it returns false.

[HI] String.equals पहले instanceof String की जांच करता है; StringBuilder अलग क्लास होने से यह false लौटाता है।

Q6. Which String method compares a String's characters directly with a StringBuffer or StringBuilder?

(कौन-सा मेथड किसी String के कैरेक्टर्स की तुलना सीधे StringBuffer या StringBuilder से करता है?)

(A) compareTo(String)

(B) equalsIgnoreCase(String)

(C) contentEquals(CharSequence)

(D) matches(String)

Correct Answer: (C)

[EN] String.contentEquals(CharSequence) compares characters uniformly across String, StringBuffer, and StringBuilder.

[HI] contentEquals(CharSequence) बिना टाइप मिसमैच के String, StringBuffer और StringBuilder के कंटेंट की तुलना करता है।

Q7. What happens when you call "Java".compareTo(null)?

("Java".compareTo(null) कॉल करने पर क्या होता है?)

(A) Returns 1

(B) Returns 0

(C) Throws NullPointerException

(D) Returns -1

Correct Answer: (C)

[EN] Comparable.compareTo must throw NullPointerException when the comparison argument is null.

[HI] Comparable.compareTo नियम के तहत तुलना के लिए null पास करने पर NullPointerException आता है।

Q8. Why is writing "ADMIN".equals(role) safer than role.equals("ADMIN")?

(role.equals("ADMIN") की तुलना में "ADMIN".equals(role) लिखना अधिक सुरक्षित क्यों माना जाता है?)

(A) It ignores uppercase and lowercase automatically

(B) It forces role into the String Constant Pool

(C) It executes 10x faster using binary search

(D) It prevents NullPointerException if role happens to be null

Correct Answer: (D)

[EN] Calling equals on a non-null constant literal safely returns false when role is null rather than crashing.

[HI] स्थिर लिटरल पर equals कॉल करने से यदि role null भी हो तो प्रोग्राम क्रैश होने के बजाय सुरक्षित रूप से false लौटाता है।

Q9. What value does "Hi".compareTo("Hello") return?

("Hi".compareTo("Hello") क्या मान लौटाता है?)

(A) 4

(B) 0

(C) -3

(D) -4

Correct Answer: (A)

[EN] Index 0 matches ('H' == 'H'); at index 1, 'i' (105) minus 'e' (101) equals 4.

[HI] इंडेक्स 0 पर 'H' समान है; इंडेक्स 1 पर 'i' (105) माइनस 'e' (101) का मान 4 आता है।

Q10. What does "Abc".compareTo("Abcdef") return?

("Abc".compareTo("Abcdef") क्या लौटाता है?)

(A) 3

(B) -3

(C) -1

(D) 0

Correct Answer: (B)

[EN] When one string is a prefix of the other, compareTo returns length difference: 3 - 6 = -3.

[HI] जब एक स्ट्रिंग दूसरी का उपसर्ग हो, तो compareTo दोनों की लंबाई का अंतर 3 - 6 = -3 लौटाता है।

Q11. What does String a = null; String b = null; System.out.print(a == b); print?

(String a = null; String b = null; System.out.print(a == b); क्या प्रिंट करेगा?)

(A) null

(B) Throws NullPointerException

(C) false

(D) true

Correct Answer: (D)

[EN] Comparing two null references with == evaluates safely to true without throwing NullPointerException.

[HI] दो null रेफरेंस की तुलना == से करने पर बिना किसी त्रुटि के true प्राप्त होता है।

Q12. What is the return value of "JAVA".equalsIgnoreCase("java")?

("JAVA".equalsIgnoreCase("java") का रिटर्न मान क्या होता है?)

(A) 0

(B) true

(C) false

(D) 32

Correct Answer: (B)

[EN] equalsIgnoreCase ignores case differences and returns a boolean true.

[HI] equalsIgnoreCase अक्षरों के केस को अनदेखा कर समान होने पर बूलियन true लौटाता है।

Q13. Why did String override Object.equals() in java.lang.String?

(java.lang.String ने Object.equals() को क्यों ओवरराइड किया?)

(A) Object.equals() checks == reference identity, whereas Strings need value-based character equality

(B) Object does not have an equals() method

(C) To make Strings mutable

(D) To avoid implementing Serializable

Correct Answer: (A)

[EN] Default Object.equals simply evaluates this == obj; String overrides it along with hashCode() for content equality.

[HI] डिफ़ॉल्ट Object.equals केवल == जांचता है, इसलिए कंटेंट के आधार पर समानता जांचने हेतु String ने equals() व hashCode() को ओवरराइड किया।

Q14. Which of the following is NOT true about String comparison in Java?

(जावा में स्ट्रिंग तुलना के बारे में निम्न में से कौन-सा कथन सत्य नहीं है?)

(A) compareTo() returns 0 when two Strings have identical content

(B) == returns true for two identical String literals due to SCP sharing

(C) String overrides hashCode() consistently with equals()

(D) equals() returns true whenever two String variables have the same length

Correct Answer: (D)

[EN] equals() verifies both identical length and matching character values at every index, not length alone.

[HI] equals() केवल लंबाई ही नहीं, बल्कि प्रत्येक इंडेक्स पर सभी कैरेक्टर्स के पूर्ण मिलान की भी जांच करता है।

Q15. What does "a".compareTo("A") return in Java?

(जावा में "a".compareTo("A") क्या मान लौटाता है?)

(A) -32

(B) 32

(C) 1

(D) 0

Correct Answer: (B)

[EN] Lowercase 'a' has ASCII/Unicode code 97 and uppercase 'A' has 65, so 97 - 65 = 32.

[HI] छोटे 'a' का यूनिकोड मान 97 और बड़े 'A' का 65 होता है, इसलिए 97 - 65 = 32 लौटता है।

Q16. If two Strings satisfy s1.equals(s2) == true, what must be true about their hashCode() values?

(यदि दो स्ट्रिंग्स के लिए s1.equals(s2) सत्य है, तो उनके hashCode() मानों के बारे में क्या सत्य होना अनिवार्य है?)

(A) s1 == s2 must also be true

(B) s1.hashCode() == s2.hashCode() must be true

(C) s1.hashCode() != s2.hashCode()

(D) hashCode() throws an exception if s1 != s2

Correct Answer: (B)

[EN] Per Java's equals-hashCode contract, equal String objects always produce identical hashCode() integers.

[HI] जावा के equals-hashCode अनुबंध के अनुसार, समान कंटेंट वाले दो String ऑब्जेक्ट्स का hashCode() मान हमेशा बराबर होता है।

Q17. Within String Comparison: equals() vs == Operator in Java, which comparison mechanism directly maps to: "boolean; compares memory reference address"?

(String Comparison: equals() vs == Operator in Java में कौन सा Comparison Mechanism सीधे "boolean; compares memory reference address" से संबंधित है?)

(A) s1.equalsIgnoreCase(s2)

(B) s1.compareTo(s2)

(C) s1 == s2

(D) s1.equals(obj)

Correct Answer: (C)

[EN] In String Comparison: equals() vs == Operator in Java, 's1 == s2' handles 'boolean; compares memory reference address' (null == null returns true safely without error).

[HI] String Comparison: equals() vs == Operator in Java में 's1 == s2' का कार्य/विवरण 'boolean; compares memory reference address' (null == null returns true safely without error) है।

Q18. Which option accurately describes 's1.equals(obj)' in String Comparison: equals() vs == Operator in Java?

(String Comparison: equals() vs == Operator in Java के संदर्भ में 's1.equals(obj)' की सही विशेषता कौन सी है?)

(A) int; Unicode lexical diff (0, <0, >0)

(B) boolean; case-insensitive content match

(C) boolean; case-sensitive content match

(D) boolean; compares memory reference address

Correct Answer: (C)

[EN] 's1.equals(obj)' provides 'boolean; case-sensitive content match' — key point: s1.equals(null) returns false; false for StringBuilder.

[HI] 's1.equals(obj)' का मुख्य विवरण 'boolean; case-sensitive content match' है (s1.equals(null) returns false; false for StringBuilder)।

Q19. In String Comparison: equals() vs == Operator in Java, identify the correct parameter for the specification: "boolean; case-insensitive content match".

(String Comparison: equals() vs == Operator in Java में "boolean; case-insensitive content match" विवरण किस विकल्प के लिए सही है?)

(A) s1 == s2

(B) s1.compareTo(s2)

(C) s1.equals(obj)

(D) s1.equalsIgnoreCase(s2)

Correct Answer: (D)

[EN] 's1.equalsIgnoreCase(s2)' corresponds to 'boolean; case-insensitive content match' (Accepts String parameter; returns false on null).

[HI] 's1.equalsIgnoreCase(s2)' का संबंध 'boolean; case-insensitive content match' (Accepts String parameter; returns false on null) से है।

Q20. Considering String Comparison: equals() vs == Operator in Java, what key behavior defines 's1.compareTo(s2)'?

(String Comparison: equals() vs == Operator in Java में 's1.compareTo(s2)' की मुख्य भूमिका या मान क्या है?)

(A) boolean; compares memory reference address

(B) int; Unicode lexical diff (0, <0, >0)

(C) boolean; case-sensitive content match

(D) boolean; case-insensitive content match

Correct Answer: (B)

[EN] 's1.compareTo(s2)' represents 'int; Unicode lexical diff (0, <0, >0)' (Throws NullPointerException if s2 is null).

[HI] 's1.compareTo(s2)' का अर्थ/मान 'int; Unicode lexical diff (0, <0, >0)' है (Throws NullPointerException if s2 is null)।

String Comparison: equals() vs == Operator in Java — Exam FAQs

Q1: If two Strings have the same hashCode(), are they guaranteed to pass equals()?

यदि दो String ऑब्जेक्ट्स का hashCode() समान हो, तो क्या उनका equals() भी हमेशा true होगा?

[EN] No. Different Strings (like "Aa" and "BB", both having hashCode 2112) can produce hash collisions while equals() remains false.

[HI] नहीं। अलग-अलग स्ट्रिंग्स (जैसे "Aa" और "BB", दोनों का हैशकोड 2112) का hashCode समान हो सकता है, लेकिन equals() उन्हें असमान बताएगा।

Q2: When is using == on Strings acceptable in Java?

जावा में String की तुलना के लिए == का उपयोग कब स्वीकार्य होता है?

[EN] Only when checking if a reference is null (s == null) or deliberately comparing interned canonical references in low-level performance code.

[HI] केवल यह जांचने के लिए कि रेफरेंस null है या नहीं (s == null), अथवा विशेष लो-लेवल कोड में पहले से intern की गई स्ट्रिंग्स की रेफरेंस जांच हेतु।

Q3: How does Objects.equals(s1, s2) simplify comparing potentially null Strings?

Objects.equals(s1, s2) संभावित रूप से null स्ट्रिंग्स की तुलना को कैसे आसान बनाता है?

[EN] java.util.Objects.equals(a, b) returns true if both are null, false if only one is null, and calls a.equals(b) otherwise—eliminating NullPointerException.

[HI] Objects.equals(a, b) दोनों के null होने पर true, किसी एक के null होने पर false और अन्यथा a.equals(b) लौटाता है—जिससे NullPointerException नहीं आता।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)