חלקPart 1: יסודותFoundations
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
String ו-StringBuilderString & StringBuilder
מחרוזות הן עצמים שלא משתנים, מתודות שימושיות, השוואה עם equals, String.format ו-StringBuilder לבנייה יעילה.
Strings are immutable objects, useful methods, comparing with equals, String.format, and StringBuilder for efficient building.
הנקודות החשובותKey points
- לא משתנה (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).
- String הוא עצם, ויש לו מתודות.A String is an object with methods.
- האינדקס מתחיל מ-0.The index starts at 0.
- המתודות מחזירות מחרוזת חדשה, והמקורית נשארת כמו שהיא.The methods return a new string, and the original stays as it is.
- רוצים לשמור את השינוי?Want to keep the change?
==בודק אם שתי הפניות מצביעות לאותו עצם.==checks whether two references point to the same object.equalsבודק אם התוכן זהה.equalschecks whether the contents match.- בתבנית כל
%מתמלא בערך הבא.In a template every%is filled by the next value.
מונחיםTerms
- מחרוזת (String)string: טקסט. ב-Java כותבים מחרוזת בין מירכאות כפולות: "שלום". מה שבמירכאות מוצג כמו שהוא.Text. In Java a string goes between double quotes: "hello". Whatever is inside the quotes is shown as is.
- לא משתנה (immutable)immutable: עצם שאי אפשר לשנות אחרי שנוצר. String כזה:
toUpperCase()לא משנה את המחרוזת, אלא מחזירה חדשה.An object that can't change once created. A String is like that:toUpperCase()doesn't change the string, it returns a new one. - מאגר המחרוזות (String pool)string pool: אזור שבו Java שומרת מחרוזות שנכתבו בקוד (ליטרלים). שני ליטרלים זהים מצביעים לאותו עצם.
new String(...)יוצר עצם חדש מחוץ למאגר.An area where Java keeps strings written in the code (literals). Two equal literals point to the same object.new String(...)makes a new object outside the pool. - אינדקסindex: המספר של מקום במחרוזת (או במערך). מתחילים מ-0: ב-
"java"התו באינדקס 0 הואj.The number of a position in a string (or an array). It starts at 0: in"java"the character at index 0 isj. - תו בריחהescape sequence: לוכסן הפוך ואחריו תו, שיחד הם תו מיוחד:
\nשורה חדשה,\tטאב,\"מירכאות,\\לוכסן.A backslash plus a character that together mean a special character:\nnew line,\ttab,\"a quote,\\a backslash. - תבנית עיצובformat specifier: החלקים שמתחילים ב-
%ב-printfוב-String.format:%sטקסט,%dשלם,%.2fעשרוני,%5dרוחב 5,%-10sצמוד לשמאל.The%parts inprintfandString.format:%stext,%dwhole number,%.2fdecimal,%5dwidth 5,%-10sleft-aligned. - StringBuilderStringBuilder: "מחרוזת שמשתנה": מוסיפים לה עם
appendבלי ליצור עצם חדש כל פעם, ובסוףtoString().A "string you can change": add to it withappendwithout making a new object each time, thentoString()at the end. - הפניהreference: מה שיושב בקופסה של משתנה מסוג עצם: לא העצם עצמו, אלא "כתובת" שמצביעה עליו ב-heap.What sits in the box of an object-type variable: not the object itself, but an "address" pointing to it on the heap.
- לא משתנה (immutable)immutable: עצם שאי אפשר לשנות אחרי שנוצר. String כזה:
toUpperCase()לא משנה את המחרוזת, אלא מחזירה חדשה.An object that can't change once created. A String is like that:toUpperCase()doesn't change the string, it returns a new one. - מאגר המחרוזות (String pool)string pool: אזור שבו Java שומרת מחרוזות שנכתבו בקוד (ליטרלים). שני ליטרלים זהים מצביעים לאותו עצם.
new String(...)יוצר עצם חדש מחוץ למאגר.An area where Java keeps strings written in the code (literals). Two equal literals point to the same object.new String(...)makes a new object outside the pool. - אינדקסindex: המספר של מקום במחרוזת (או במערך). מתחילים מ-0: ב-
"java"התו באינדקס 0 הואj.The number of a position in a string (or an array). It starts at 0: in"java"the character at index 0 isj. - תו בריחהescape sequence: לוכסן הפוך ואחריו תו, שיחד הם תו מיוחד:
\nשורה חדשה,\tטאב,\"מירכאות,\\לוכסן.A backslash plus a character that together mean a special character:\nnew line,\ttab,\"a quote,\\a backslash. - תבנית עיצובformat specifier: החלקים שמתחילים ב-
%ב-printfוב-String.format:%sטקסט,%dשלם,%.2fעשרוני,%5dרוחב 5,%-10sצמוד לשמאל.The%parts inprintfandString.format:%stext,%dwhole number,%.2fdecimal,%5dwidth 5,%-10sleft-aligned. - StringBuilderStringBuilder: "מחרוזת שמשתנה": מוסיפים לה עם
appendבלי ליצור עצם חדש כל פעם, ובסוףtoString().A "string you can change": add to it withappendwithout making a new object each time, thentoString()at the end. - הפניהreference: מה שיושב בקופסה של משתנה מסוג עצם: לא העצם עצמו, אלא "כתובת" שמצביעה עליו ב-heap.What sits in the box of an object-type variable: not the object itself, but an "address" pointing to it on the heap.
Cheatsheet
מתודות של StringString methods
String s = "Java Course";
System.out.println(s.length());
System.out.println(s.charAt(0));
System.out.println(s.indexOf("C"));
System.out.println(s.substring(0, 4));
System.out.println(s.toUpperCase());
System.out.println(s.replace("Course", "Rocks"));
System.out.println(s.contains("va"));String לא משתנהA String never changes
String name = "noa";
name.toUpperCase();
System.out.println(name);
name = name.toUpperCase();
System.out.println(name);== מול equals, ומאגר המחרוזות== vs equals, and the string pool
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));עיצוב עם printf ו-String.formatFormatting with printf and String.format
System.out.printf("%-8s|%6.2f|%n", "Coffee", 12.5);
System.out.printf("%-8s|%6.2f|%n", "Cake", 18.9);
String line = String.format("%s has %d books", "Noa", 7);
System.out.println(line);StringBuilder ותווי בריחהStringBuilder and escape sequences
StringBuilder sb = new StringBuilder();
sb.append("Ja");
sb.append("va");
sb.append(21);
System.out.println(sb);
System.out.println(sb.reverse());
System.out.println("She said \"hi\"\tthen left");שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|