Strings: basic String operations, String comparison

Java Strings immutable objects हैं, == references compare करता है जबकि .equals() characters compare करता है: वह bug जो हर lab test pass करता है और real input पर fail होता है।

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

वह Search जो सिर्फ़ Lab में काम करती थी

BookBridge को एक title search मिलती है। आप इसे test करते हैं:

if (typed == b.title) ...

Lab में यह "Let Us C" perfectly ढूँढता है। Demo day: librarian exactly same title type करती है, और search कुछ नहीं return करती। Same spelling, same case, आप दोनों strings print करते हैं और वे identical दिखती हैं।

उसकी typing में कुछ ग़लत नहीं है। == में कुछ ग़लत है, और वजह कि इसने आपको fool किया Java के favourite interview questions में से एक है।

Theory

String एक Warehouse वाली Class है

एक Java String एक object है, primitive नहीं: title character data की एक reference hold करता है, exactly जैसे Book b एक Book की reference hold करता है।

Java एक optimisation add करता है: string pool। आपके code में हर string literal एक shared warehouse में जाता है, और identical literals एक pooled object reuse करते हैं। तो "Let Us C" assign की गई 2 variables same object की तरफ़ point करती हैं।

पर run time पर बनी strings (user input, concatenation, new String(...)) fresh objects की तरह pool के बाहर रहती हैं।

Practical

Same Characters, अलग Objects

public class TitleCheck {
    public static void main(String[] args) {
        String a = "Let Us C";             // literal: pooled
        String b = "Let Us C";             // same pooled object as a
        String c = new String("Let Us C"); // forced NEW object

        System.out.println(a == b);        // true:  same object
        System.out.println(a == c);        // false: different objects
        System.out.println(a.equals(c));   // true:  same characters
    }
}

Theory

== पूछता है कहाँ, equals पूछता है क्या

Objects के बीच == references compare करता है: क्या ये 2 variables memory में same object की तरफ़ point करते हैं?

.equals() content compare करता है: क्या ये strings same characters hold करती हैं?

यह mystery solve कर देता है: आपके test ने 2 literals compare किए, दोनों pool से drawn, same object, == luck से true। Librarian का typed title एक fresh runtime object की तरह आया: same characters, अलग address, == false।

जानने लायक relatives: equalsIgnoreCase("let us c"), और dictionary order के लिए compareTo (0 का मतलब है equal)।

Quiz

String s = "book"; s.concat("bridge"); System.out.println(s); क्या print होता है?

  1. book
  2. bookbridge
  3. bridge
  4. Compile error: Strings एक method से concatenate नहीं हो सकतीं
Show the answer

book

Strings immutable हैं: concat s को change नहीं कर सकता; यह एक नया String build और RETURN करता है, जिसे यह code assign न करके फेंक देता है। s अभी भी "book" की तरफ़ point करता है। Result रखने के लिए आपको लिखना होगा s = s.concat("bridge")। Option B उन students के लिए trap है जो in-place modification assume करते हैं, जो mutable StringBuffer behave करता है, अभी से 2 lessons आगे। यहाँ कुछ भी compile होने में fail नहीं होता; code legal है, बस useless।

Think first

Immutable, तो + पर क्या होता है?

अगर कोई String कभी change नहीं हो सकता, title + " (2nd Ed)" memory के साथ असल में क्या करता है? Tap करने से पहले इसे reason out कीजिए।

Show the answer

यह joined characters hold करने वाला एक तीसरा, brand-new String बनाता है; दोनों originals untouched रहते हैं। Strings पर हर + एक नया object बनाता है। यह कुछ joins के लिए harmless है, पर s = s + line loop करके एक 6000-line catalog बनाना हज़ारों throwaway objects create करता है; उस cost का Java का answer StringBuffer है, 2 lessons आगे। Bonus thread: BCA204 की Python strings same reasons से immutable थीं: safe sharing और pooling।

Watch out

वह Rule जो Demo बचाता है

कभी string content को == से compare मत कीजिए। यह exactly इतनी बार काम करता है (pooled literals) कि आपकी testing में survive करे और client के सामने fail हो।

Null direction भी watch कीजिए: अगर typed null हो सकता है, typed.equals(a) एक NullPointerException throw करता है; defensive habit है "Let Us C".equals(typed), literal पहले, जो simply false है जब typed null है।

Theory

यह BookBridge को क्या देता है

Fixed search एक line है: if (typed.equals(b.title)), case-insensitive अगर librarian prefer करे: equalsIgnoreCase। Immutability, इस lesson का दूसरा idea, एक inconvenience नहीं है; यही वजह है Strings objects और threads (Unit 4) के बीच share करने में safe हैं बिना किसी के आपके titles corrupt किए। अगला lesson String toolbox tour करता है: 11 methods जो librarian के messy input को slice, search और clean करते हैं।

Summary

Key takeaways

  • String एक class है; variables string objects की references hold करते हैं।
  • Identical literals एक pooled object share करते हैं; runtime strings (input, new String) अलग objects हैं।
  • == references compare करता है (same object?); .equals() characters compare करता है (same content?)।
  • Content के लिए हमेशा .equals() / equalsIgnoreCase इस्तेमाल कीजिए; ordering के लिए compareTo।
  • Strings immutable हैं: concat और + नए Strings return करते हैं, originals कभी नहीं बदलते।
  • बिना assignment के s.concat("x") कुछ नहीं बदलता।
  • Memory hook: == पूछता है कहाँ, equals पूछता है क्या।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Basic Concepts of Strings and Exceptions

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Strings: basic String operations, String comparison · Java Programming Language · Gri-Learn