נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
JavaJava

String ו-StringBuilderString & StringBuilder

מפגש 3: מחרוזות הן עצמים שלא משתנים, מתודות שימושיות, השוואה עם equals, String.format ו-StringBuilder לבנייה יעילה.Session 3: Strings are immutable objects, useful methods, comparing with equals, String.format, and StringBuilder for efficient building.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • מתודות של StringString methods
  • String לא משתנהA String never changes
  • == מול equals, ומאגר המחרוזות== vs equals, and the string pool
  • עיצוב עם printf ו-String.formatFormatting with printf and String.format
  • StringBuilder ותווי בריחהStringBuilder and escape sequences
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • לא משתנה (immutable): עצם שאי אפשר לשנות אחרי שנוצר.immutable: An object that can't change once created.
  • מאגר המחרוזות (String pool): אזור שבו Java שומרת מחרוזות שנכתבו בקוד (ליטרלים).string pool: An area where Java keeps strings written in the code (literals).
  • אינדקס: המספר של מקום במחרוזת (או במערך).index: The number of a position in a string (or an array).
  • תו בריחה: לוכסן הפוך ואחריו תו, שיחד הם תו מיוחד:escape sequence: A backslash plus a character that together mean a special character:
  • תבנית עיצוב: החלקים שמתחילים ב-% ב-printf וב-String.format:format specifier: The % parts in printf and String.format:
  • StringBuilder: "מחרוזת שמשתנה":StringBuilder: A "string you can change":
נושאTopic 2

מתודות של StringString methods

מתודות של StringString methods

  • String הוא עצם, ויש לו מתודות.A String is an object with methods.
  • האינדקס מתחיל מ-0.The index starts at 0.
  • substring(a, b) לוקח מ-a ועד לפני b.substring(a, b) takes from a up to before b.
נושאTopic 3

String לא משתנהA String never changes

String לא משתנהA String never changes

  • המתודות מחזירות מחרוזת חדשה, והמקורית נשארת כמו שהיא.The methods return a new string, and the original stays as it is.
  • רוצים לשמור את השינוי?Want to keep the change?
  • שמים את התוצאה בחזרה במשתנה.Put the result back in the variable.
שאלה לכיתהQuestion for the class

מה ידפיס הקוד?What will this print?

main
String name = "noa";
name.toUpperCase();
System.out.println(name);
name = name.toUpperCase();
System.out.println(name);

    String לא משתנהA String never changes

    Main.java · main
    String name = "noa";
    name.toUpperCase();
    System.out.println(name);
    name = name.toUpperCase();
    System.out.println(name);
    פלטOutput
    noa
    NOA
    נושאTopic 4

    == מול equals, ומאגר המחרוזות== vs equals, and the string pool

    == מול equals, ומאגר המחרוזות== vs equals, and the string pool

    • == בודק אם שתי הפניות מצביעות לאותו עצם.== checks whether two references point to the same object.
    • equals בודק אם התוכן זהה.equals checks whether the contents match.
    • שני ליטרלים זהים חולקים עצם אחד במאגר, אבל new String יוצר עצם חדש.Two equal literals share one object in the pool, but new String makes a new object.
    שאלה לכיתהQuestion for the class

    מה ידפיס הקוד?What will this print?

    main
    String a = "java";
    String b = "java";
    String c = new String("java");
    System.out.println(a == b);
    System.out.println(a == c);
    System.out.println(a.equals(c));

      == מול equals, ומאגר המחרוזות== vs equals, and the string pool

      Main.java · main
      String a = "java";
      String b = "java";
      String c = new String("java");
      System.out.println(a == b);
      System.out.println(a == c);
      System.out.println(a.equals(c));
      פלטOutput
      true
      false
      true
      נושאTopic 5

      עיצוב עם printf ו-String.formatFormatting with printf and String.format

      עיצוב עם printf ו-String.formatFormatting with printf and String.format

      • בתבנית כל % מתמלא בערך הבא.In a template every % is filled by the next value.
      • מספר אחרי ה-% הוא רוחב, ומינוס צמוד לשמאל.A number after % is a width, and a minus aligns left.
      • String.format מחזיר את הטקסט במקום להדפיס.String.format returns the text instead of printing it.
      נושאTopic 6

      StringBuilder ותווי בריחהStringBuilder and escape sequences

      StringBuilder ותווי בריחהStringBuilder and escape sequences

      • StringBuilder הוא מחרוזת שאפשר לשנות: append מוסיף, reverse הופך, ו-toString מחזיר String.A StringBuilder is a string you can change: append adds, reverse flips, and toString gives back a String.
      • תווי בריחה: \n שורה חדשה, \t טאב, \" מירכאות.Escapes: \n new line, \t tab, \" a quote.
      נושאTopic 7

      פרויקט מדףShelf project

      הצעד של היום בפרויקט מדףToday's step in the Shelf project

      • מדף, צעד 3: כרטיס מעוצבShelf, step 3: a formatted card

      לא משתנה (immutable): עצם שאי אפשר לשנות אחרי שנוצר.immutable: An object that can't change once created.

      מאגר המחרוזות (String pool): אזור שבו Java שומרת מחרוזות שנכתבו בקוד (ליטרלים).string pool: An area where Java keeps strings written in the code (literals).

      אינדקס: המספר של מקום במחרוזת (או במערך).index: The number of a position in a string (or an array).

      תו בריחה: לוכסן הפוך ואחריו תו, שיחד הם תו מיוחד:escape sequence: A backslash plus a character that together mean a special character:

      תבנית עיצוב: החלקים שמתחילים ב-% ב-printf וב-String.format:format specifier: The % parts in printf and String.format:

      StringBuilder: "מחרוזת שמשתנה":StringBuilder: A "string you can change":

      String הוא עצם, ויש לו מתודות.A String is an object with methods.

      האינדקס מתחיל מ-0.The index starts at 0.

      substring(a, b) לוקח מ-a ועד לפני b.substring(a, b) takes from a up to before b.

      המתודות מחזירות מחרוזת חדשה, והמקורית נשארת כמו שהיא.The methods return a new string, and the original stays as it is.

      רוצים לשמור את השינוי?Want to keep the change?

      שמים את התוצאה בחזרה במשתנה.Put the result back in the variable.

      == בודק אם שתי הפניות מצביעות לאותו עצם.== checks whether two references point to the same object.

      equals בודק אם התוכן זהה.equals checks whether the contents match.

      שני ליטרלים זהים חולקים עצם אחד במאגר, אבל new String יוצר עצם חדש.Two equal literals share one object in the pool, but new String makes a new object.

      בתבנית כל % מתמלא בערך הבא.In a template every % is filled by the next value.

      מספר אחרי ה-% הוא רוחב, ומינוס צמוד לשמאל.A number after % is a width, and a minus aligns left.

      String.format מחזיר את הטקסט במקום להדפיס.String.format returns the text instead of printing it.

      StringBuilder הוא מחרוזת שאפשר לשנות: append מוסיף, reverse הופך, ו-toString מחזיר String.A StringBuilder is a string you can change: append adds, reverse flips, and toString gives back a String.

      תווי בריחה: \n שורה חדשה, \t טאב, \" מירכאות.Escapes: \n new line, \t tab, \" a quote.

      מדף, צעד 3: כרטיס מעוצבShelf, step 3: a formatted card

      מה ידפיס "Hello".charAt(1)?What does "Hello".charAt(1) print?

      • HH
      • ee
      • ll
      • שגיאהAn error

      ee

      מה ידפיס הקוד?What does the code print?

      • ABCABC
      • abcabc
      • AbcAbc
      • שגיאת קומפילציהA compile error

      abcabc

      מה למדנוWhat we learned

      • לא משתנה (immutable): עצם שאי אפשר לשנות אחרי שנוצר.immutable: An object that can't change once created.
      • String הוא עצם, ויש לו מתודות.A String is an object with methods.
      • המתודות מחזירות מחרוזת חדשה, והמקורית נשארת כמו שהיא.The methods return a new string, and the original stays as it is.
      • == בודק אם שתי הפניות מצביעות לאותו עצם.== checks whether two references point to the same object.
      • בתבנית כל % מתמלא בערך הבא.In a template every % is filled by the next value.
      • StringBuilder הוא מחרוזת שאפשר לשנות: append מוסיף, reverse הופך, ו-toString מחזיר String.A StringBuilder is a string you can change: append adds, reverse flips, and toString gives back a String.

      עכשיו לתרגולNow, practice

      7 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית7 exercises and 2 challenges, auto-checked
      הערות מרצהSpeaker notes

      נבנה על ידיBuilt by Ariel Yohimovich