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

JavaJava
2
חלק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.

או Ctrl+Por Ctrl+P

הנקודות החשובות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 בסוף).int for whole numbers, double for numbers with a point, boolean for true/false, char for one character in single quotes, and long for huge whole numbers (with an L at the end).
  • מ-int ל-double Java ממירה לבד (אין מה לאבד).From int to double Java 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.int goes up to 2147483647.
  • אם עוברים אותו, אין שגיאה: התוצאה פשוט "מתגלגלת" למספר שלילי (גלישה).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, char and 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, like int 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.9 gives 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. int goes up to about 2.1 billion.
  • varvar: במקום לכתוב טיפוס, var אומר למהדר להסיק אותו מהערך: var n = 5; הוא int. הטיפוס עדיין קבוע.Instead of writing a type, var tells the compiler to infer it from the value: var n = 5; is an int. The type is still fixed.
  • finalfinal: משתנה final מקבל ערך פעם אחת ולא משתנה יותר. קבועים כותבים באותיות גדולות: final int MAX = 10;.A final variable 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, char and 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, like int 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.9 gives 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. int goes up to about 2.1 billion.
  • varvar: במקום לכתוב טיפוס, var אומר למהדר להסיק אותו מהערך: var n = 5; הוא int. הטיפוס עדיין קבוע.Instead of writing a type, var tells the compiler to infer it from the value: var n = 5; is an int. The type is still fixed.
  • finalfinal: משתנה final מקבל ערך פעם אחת ולא משתנה יותר. קבועים כותבים באותיות גדולות: final int MAX = 10;.A final variable 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

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