חלקPart 1: יסודותFoundations
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
משתנים וטיפוסים פרימיטיבייםVariables & primitive types
int, double, boolean, char ו-long, var, המרות טיפוסים, גלישה (overflow) וחלוקה של מספרים שלמים.
int, double, boolean, char and long, var, type conversions, overflow and integer division.
הנקודות החשובותKey points
- משתנה: קופסה עם שם ששומרת ערך.variable: A named box that holds a value.
- טיפוס: מה מותר לשים בקופסה:type: What may go into a box:
- ב-Java כותבים קודם את הטיפוס, אחר כך את השם, ואז
=והערך.In Java you write the type first, then the name, then=and the value. =הוא השמה: "שים בקופסה".=is an assignment: "put in the box".intלמספרים שלמים,doubleלמספרים עם נקודה,booleanל-true/false,charלתו אחד בין גרשיים בודדים, ו-longלמספרים שלמים ענקיים (עםLבסוף).intfor whole numbers,doublefor numbers with a point,booleanfortrue/false,charfor one character in single quotes, andlongfor huge whole numbers (with anLat the end).- מ-
intל-doubleJava ממירה לבד (אין מה לאבד).FrominttodoubleJava converts by itself (nothing is lost). - בכיוון ההפוך צריך המרה מפורשת (cast) מפורש, והחלק העשרוני נחתך (לא מעוגל).The other way needs an explicit cast, and the decimal part is cut off (not rounded).
intמגיע עד2147483647.intgoes up to2147483647.- אם עוברים אותו, אין שגיאה: התוצאה פשוט "מתגלגלת" למספר שלילי (גלישה).Go past it and there's no error: the result just "wraps around" to a negative number (overflow).
מונחיםTerms
- משתנהvariable: קופסה עם שם ששומרת ערך. ב-Java לכל קופסה יש טיפוס קבוע:
int age = 15;.A named box that holds a value. In Java every box has a fixed type:int age = 15;. - טיפוסtype: מה מותר לשים בקופסה: מספר שלם, מספר עשרוני, טקסט ועוד. המהדר בודק שלא מערבבים.What may go into a box: a whole number, a decimal, text and more. The compiler checks you don't mix them up.
- טיפוס פרימיטיביprimitive type: 8 הטיפוסים הבסיסיים של Java (
int,long,double,boolean,charועוד). הערך יושב ישר בקופסה, בלי עצם ב-heap.Java's 8 basic types (int,long,double,boolean,charand more). The value sits right in the box, with no object on the heap. - הצהרהdeclaration: השורה שיוצרת משתנה: טיפוס ושם, למשל
int score;.The line that creates a variable: a type and a name, likeint score;. - השמהassignment: שמים ערך בקופסה עם
=. הערך הקודם נמחק.=הוא "שים", לא "שווה".Putting a value in a box with=. The old value is gone.=means "put", not "equals". - ליטרלliteral: ערך שכתוב ישר בקוד:
42,3.14,true,'A',"hi".A value written straight in the code:42,3.14,true,'A',"hi". - המרה מפורשת (cast)cast: כותבים טיפוס בסוגריים כדי להמיר:
(int) 3.9נותן 3. בהמרה מ-double ל-int החלק העשרוני נחתך.Writing a type in brackets to convert:(int) 3.9gives 3. Going from double to int cuts off the decimal part. - גלישה (overflow)overflow: כשהתוצאה גדולה מדי לטיפוס, היא "מתגלגלת" למספר שגוי, בלי שגיאה.
intמגיע עד כ-2.1 מיליארד.When a result is too big for the type it "wraps around" to a wrong number, with no error.intgoes up to about 2.1 billion. - varvar: במקום לכתוב טיפוס,
varאומר למהדר להסיק אותו מהערך:var n = 5;הואint. הטיפוס עדיין קבוע.Instead of writing a type,vartells the compiler to infer it from the value:var n = 5;is anint. The type is still fixed. - finalfinal: משתנה
finalמקבל ערך פעם אחת ולא משתנה יותר. קבועים כותבים באותיות גדולות:final int MAX = 10;.Afinalvariable gets a value once and never changes. Constants are written in capitals:final int MAX = 10;. - חלוקה של שלמיםinteger division: כששני המספרים שלמים, / זורק את מה שאחרי הנקודה: 7 / 2 הוא 3. ה-% נותן את השארית: 7 % 2 הוא 1.When both numbers are whole, / drops what's after the point: 7 / 2 is 3. % gives the remainder: 7 % 2 is 1.
- משתנהvariable: קופסה עם שם ששומרת ערך. ב-Java לכל קופסה יש טיפוס קבוע:
int age = 15;.A named box that holds a value. In Java every box has a fixed type:int age = 15;. - טיפוסtype: מה מותר לשים בקופסה: מספר שלם, מספר עשרוני, טקסט ועוד. המהדר בודק שלא מערבבים.What may go into a box: a whole number, a decimal, text and more. The compiler checks you don't mix them up.
- טיפוס פרימיטיביprimitive type: 8 הטיפוסים הבסיסיים של Java (
int,long,double,boolean,charועוד). הערך יושב ישר בקופסה, בלי עצם ב-heap.Java's 8 basic types (int,long,double,boolean,charand more). The value sits right in the box, with no object on the heap. - הצהרהdeclaration: השורה שיוצרת משתנה: טיפוס ושם, למשל
int score;.The line that creates a variable: a type and a name, likeint score;. - השמהassignment: שמים ערך בקופסה עם
=. הערך הקודם נמחק.=הוא "שים", לא "שווה".Putting a value in a box with=. The old value is gone.=means "put", not "equals". - ליטרלliteral: ערך שכתוב ישר בקוד:
42,3.14,true,'A',"hi".A value written straight in the code:42,3.14,true,'A',"hi". - המרה מפורשת (cast)cast: כותבים טיפוס בסוגריים כדי להמיר:
(int) 3.9נותן 3. בהמרה מ-double ל-int החלק העשרוני נחתך.Writing a type in brackets to convert:(int) 3.9gives 3. Going from double to int cuts off the decimal part. - גלישה (overflow)overflow: כשהתוצאה גדולה מדי לטיפוס, היא "מתגלגלת" למספר שגוי, בלי שגיאה.
intמגיע עד כ-2.1 מיליארד.When a result is too big for the type it "wraps around" to a wrong number, with no error.intgoes up to about 2.1 billion. - varvar: במקום לכתוב טיפוס,
varאומר למהדר להסיק אותו מהערך:var n = 5;הואint. הטיפוס עדיין קבוע.Instead of writing a type,vartells the compiler to infer it from the value:var n = 5;is anint. The type is still fixed. - finalfinal: משתנה
finalמקבל ערך פעם אחת ולא משתנה יותר. קבועים כותבים באותיות גדולות:final int MAX = 10;.Afinalvariable gets a value once and never changes. Constants are written in capitals:final int MAX = 10;.
Cheatsheet
משתנה = קופסה עם שם וטיפוסA variable = a box with a name and a type
int age = 15;
String name = "נועה";
System.out.println(name + " בת " + age);
age = age + 1;
System.out.println("בשנה הבאה: " + age);int age = 15;
String name = "Noa";
System.out.println(name + " is " + age);
age = age + 1;
System.out.println("Next year: " + age);הטיפוסים הבסיסייםThe basic types
int books = 12;
double price = 39.90;
boolean onSale = true;
char grade = 'A';
long stars = 100_000_000_000L;
System.out.println(books + " | " + price + " | " + onSale + " | " + grade + " | " + stars);המרות: אוטומטית ומפורשתConversions: automatic and explicit
int whole = 7;
double d = whole;
System.out.println(d);
double price = 9.99;
int cut = (int) price;
System.out.println(cut);
System.out.println(Math.round(price));
System.out.println(7 / 2 + " vs " + 7 / 2.0);גלישה: כשהמספר גדול מדיOverflow: when the number is too big
int big = Integer.MAX_VALUE;
System.out.println(big);
big = big + 1;
System.out.println(big);
long safe = Integer.MAX_VALUE + 1L;
System.out.println(safe);var ו-finalvar and final
var count = 3;
var ratio = 0.5;
final int MAX_BOOKS = 10;
System.out.println(count * ratio);
System.out.println("מקום פנוי: " + (MAX_BOOKS - count));var count = 3;
var ratio = 0.5;
final int MAX_BOOKS = 10;
System.out.println(count * ratio);
System.out.println("Free slots: " + (MAX_BOOKS - count));שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|