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 માં work કરતું હતું

BookBridge ને title search મળે છે. તમે test કરો છો:

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

lab માં એ "Let Us C" ને perfectly find કરે છે. demo day: librarian exactly same title ને types કરે છે, અને search કંઈ return નથી કરતું. Same spelling, same case, તમે બંને strings ને print કરો છો અને એ identical દેખાય છે.

તેના typing માં કંઈ wrong નથી. == માં કંઈ wrong છે, અને કારણ કે તેણે તમને 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 ઉમેરે છે: string pool. તમારા code નો દરેક string literal shared warehouse માં જાય છે, અને identical literals એક pooled object ને reuse કરે છે. એટલે "Let Us C" assign કરેલા 2 variables same object પર point કરે છે.

પણ run time પર built strings (user input, concatenation, new String(...)) fresh objects તરીકે pool ની બહાર lives કરે છે.

Practical

Same characters, different objects

public class TitleCheck {
    public static void main(String[] args) {
        String a = "Let Us C";             // literal: pooled
        String b = "Let Us C";             // a જેવું જ pooled object
        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

== પૂછે છે where, equals પૂછે છે what

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, different 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 નથી કરી શકતું; તે new String બનાવે છે અને RETURN કરે છે, જે આ code assign ન કરવાથી throw away કરે છે. s હજુ "book" પર point કરે છે. result ને keep કરવા તમારે લખવું પડે s = s.concat("bridge"). Option B એ students માટે trap છે જેઓ in-place modification ને assume કરે છે, જે mutable StringBuffer કેવું behave કરે છે, 2 lessons આગળ. અહીં કંઈ compile fail નથી થતું; code legal છે, just useless.

Think first

Immutable, તો + પર શું થાય છે?

જો કોઈ String ever change નથી કરી શકતું, તો title + " (2nd Ed)" actually memory પર શું કરે છે? tap કરતા પહેલા reason કરો.

Show the answer

એ third, brand-new String create કરે છે જે joined characters ને hold કરે છે; બંને originals untouched રહે છે. strings પર દરેક + એ new object બનાવે છે. એ થોડા joins માટે harmless છે, પણ 6000-line catalog ને looping s = s + line દ્વારા build કરવાથી હજારો throwaway objects બને છે; એ cost નો Java નો answer StringBuffer છે, 2 lessons આગળ આવે છે. Bonus thread: BCA204 ના Python strings પણ same reasons માટે immutable હતા: safe sharing અને pooling.

Watch out

demo ને save કરતો rule

ક્યારેય string content ને == સાથે compare ન કરો. એ exactly enough વાર work કરે છે (pooled literals) તમારા testing ને survive કરવા માટે અને client ની સામે fail થવા માટે.

null direction ને પણ watch કરો: જો typed null હોઈ શકે, તો typed.equals(a) NullPointerException throw કરે છે; defensive habit એ "Let Us C".equals(typed) છે, literal first, જે typed null હોય ત્યારે simply false છે.

Theory

આ BookBridge ને શું buy કરે છે

fixed search એ one line છે: if (typed.equals(b.title)), case-insensitive જો librarian prefer કરે: equalsIgnoreCase. Immutability, આ lesson નો second idea, inconvenience નથી; એટલે Strings objects અને threads (Unit 4) વચ્ચે share કરવા safe છે કોઈ તમારા titles ને corrupt કર્યા વગર. next 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) separate objects છે.
  • == references ને compare કરે છે (same object?); .equals() characters ને compare કરે છે (same content?).
  • Content માટે હંમેશા .equals() / equalsIgnoreCase વાપરો; ordering માટે compareTo.
  • Strings immutable છે: concat અને + new Strings return કરે છે, originals ક્યારેય change નથી થતા.
  • assignment વગર s.concat("x") કંઈ change નથી કરતું.
  • Memory hook: == પૂછે છે where, equals પૂછે છે what.

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