מפגש 3: מחרוזות הן עצמים שלא משתנים, מתודות שימושיות, השוואה עם equals, String.format ו-StringBuilder לבנייה יעילה.Session 3: Strings are immutable objects, useful methods, comparing with equals, String.format, and StringBuilder for efficient building.
מחרוזות הן עצמים שלא משתנים, מתודות שימושיות, השוואה עם equals, String.format ו-StringBuilder לבנייה יעילה. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Strings are immutable objects, useful methods, comparing with equals, String.format, and StringBuilder for efficient building. At the end we add a step to the companion project.
מה נעשה היום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
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושא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":
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.
נושאTopic 2
מתודות של StringString methods
מתודות של StringString methods
String הוא עצם, ויש לו מתודות.A String is an object with methods.
substring(a, b) לוקח מ-a ועד לפני b.substring(a, b) takes from a up to before b.
String הוא עצם, ויש לו מתודות. האינדקס מתחיל מ-0. substring(a, b) לוקח מ-a ועד לפני b.A String is an object with methods. The index starts at 0. 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.
המתודות מחזירות מחרוזת חדשה, והמקורית נשארת כמו שהיא. רוצים לשמור את השינוי? שמים את התוצאה בחזרה במשתנה.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);
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה. (זה הגוף של main.)Give the class half a minute to guess, then move on. (This is the body of main.)
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
מריצים ומשווים לניחושים.Run it and compare with the guesses.
נושא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.
== בודק אם שתי הפניות מצביעות לאותו עצם. equals בודק אם התוכן זהה. שני ליטרלים זהים חולקים עצם אחד במאגר, אבל new String יוצר עצם חדש. בזיכרון למטה רואים את זה: החצים של a ו-b מגיעים לאותו עצם.== checks whether two references point to the same object. equals checks whether the contents match. Two equal literals share one object in the pool, but new String makes a new object. The memory view below shows it: the arrows from a and b reach the same object.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
main
String a ="java";
String b ="java";
String c =newString("java");
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה. (זה הגוף של main.)Give the class half a minute to guess, then move on. (This is the body of main.)
== מול equals, ומאגר המחרוזות== vs equals, and the string pool
Main.java · main
String a ="java";
String b ="java";
String c =newString("java");
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(c));
פלטOutput
true
false
true
מריצים ומשווים לניחושים.Run it and compare with the guesses.
נושא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.
בתבנית כל % מתמלא בערך הבא. מספר אחרי ה-% הוא רוחב, ומינוס צמוד לשמאל. 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 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.
StringBuilder הוא מחרוזת שאפשר לשנות: append מוסיף, reverse הופך, ו-toString מחזיר String. תווי בריחה: \n שורה חדשה, \t טאב, \" מירכאות.A StringBuilder is a string you can change: append adds, reverse flips, and toString gives back a String. 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
שמרו ספר במשתנים (title, author, year, stars מסוג int בין 1 ל-5). בנו עם String.format שורה בפורמט <title> | <author> | <year>, והדפיסו אותה ואחריה שורת כוכבים: * כפול stars (רמז: "*".repeat(stars)).Keep a book in variables (title, author, year, stars as an int 1–5). Build with String.format a line like <title> | <author> | <year>, print it, and then a line of stars: * times stars (hint: "*".repeat(stars)).
לא משתנה (immutable): עצם שאי אפשר לשנות אחרי שנוצר.immutable: An object that can't change once created.
המונחים של המפגשThis session's terms
מאגר המחרוזות (String pool): אזור שבו Java שומרת מחרוזות שנכתבו בקוד (ליטרלים).string pool: An area where Java keeps strings written in the code (literals).
המונחים של המפגשThis session's terms
אינדקס: המספר של מקום במחרוזת (או במערך).index: The number of a position in a string (or an array).
המונחים של המפגשThis session's terms
תו בריחה: לוכסן הפוך ואחריו תו, שיחד הם תו מיוחד:escape sequence: A backslash plus a character that together mean a special character:
המונחים של המפגשThis session's terms
תבנית עיצוב: החלקים שמתחילים ב-% ב-printf וב-String.format:format specifier: The % parts in printf and String.format:
המונחים של המפגשThis session's terms
StringBuilder: "מחרוזת שמשתנה":StringBuilder: A "string you can change":
המונחים של המפגשThis session's terms
String הוא עצם, ויש לו מתודות.A String is an object with methods.
substring(a, b) לוקח מ-a ועד לפני b.substring(a, b) takes from a up to before b.
מתודות של StringString methods
המתודות מחזירות מחרוזת חדשה, והמקורית נשארת כמו שהיא.The methods return a new string, and the original stays as it is.
String לא משתנהA String never changes
רוצים לשמור את השינוי?Want to keep the change?
String לא משתנהA String never changes
שמים את התוצאה בחזרה במשתנה.Put the result back in the variable.
String לא משתנהA String never changes
== בודק אם שתי הפניות מצביעות לאותו עצם.== checks whether two references point to the same object.
== מול equals, ומאגר המחרוזות== vs equals, and the string pool
equals בודק אם התוכן זהה.equals checks whether the contents match.
== מול equals, ומאגר המחרוזות== vs equals, and the string pool
שני ליטרלים זהים חולקים עצם אחד במאגר, אבל new String יוצר עצם חדש.Two equal literals share one object in the pool, but new String makes a new object.
== מול equals, ומאגר המחרוזות== vs equals, and the string pool
בתבנית כל % מתמלא בערך הבא.In a template every % is filled by the next value.
עיצוב עם printf ו-String.formatFormatting with printf and String.format
מספר אחרי ה-% הוא רוחב, ומינוס צמוד לשמאל.A number after % is a width, and a minus aligns left.
עיצוב עם printf ו-String.formatFormatting with printf and String.format
String.format מחזיר את הטקסט במקום להדפיס.String.format returns the text instead of printing it.
עיצוב עם printf ו-String.formatFormatting with printf and String.format
StringBuilder הוא מחרוזת שאפשר לשנות: append מוסיף, reverse הופך, ו-toString מחזיר String.A StringBuilder is a string you can change: append adds, reverse flips, and toString gives back a String.
StringBuilder ותווי בריחהStringBuilder and escape sequences
תווי בריחה: \n שורה חדשה, \t טאב, \" מירכאות.Escapes: \n new line, \t tab, \" a quote.
StringBuilder ותווי בריחהStringBuilder and escape sequences
מדף, צעד 3: כרטיס מעוצבShelf, step 3: a formatted card
הצעד של היום בפרויקט מדףToday's step in the Shelf project
מה ידפיס "Hello".charAt(1)?What does "Hello".charAt(1) print?
HH
ee
ll
שגיאהAn error
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
ee
נכון: מתחילים לספור מ-0.Right: counting starts at 0.
מה ידפיס הקוד?What does the code print?
ABCABC
abcabc
AbcAbc
שגיאת קומפילציהA compile error
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
abcabc
נכון: String לא משתנה, והתוצאה החדשה נזרקה.Right: a String never changes, and the new result was thrown away.
מה למדנו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.
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.
עכשיו לתרגולNow, practice
7 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית7 exercises and 2 challenges, auto-checked