משתנים וטיפוסים פרימיטיבייםVariables & primitive types
int, double, boolean, char ו-long, var, המרות טיפוסים, גלישה (overflow) וחלוקה של מספרים שלמים.
int, double, boolean, char and long, var, type conversions, overflow and integer division.
01המונחים של המפגשThis session's terms
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.
Before the code, the words. Every term here is also linked in the course glossary.
- משתנה: קופסה עם שם ששומרת ערך. ב-Java לכל קופסה יש טיפוס קבוע:
int age = 15;.variable: 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.
- טיפוס פרימיטיבי: 8 הטיפוסים הבסיסיים של Java (
int,long,double,boolean,charועוד). הערך יושב ישר בקופסה, בלי עצם ב-heap.primitive type: Java's 8 basic types (int,long,double,boolean,charand more). The value sits right in the box, with no object on the heap. - הצהרה: השורה שיוצרת משתנה: טיפוס ושם, למשל
int score;.declaration: 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". - ליטרל: ערך שכתוב ישר בקוד:
42,3.14,true,'A',"hi".literal: A value written straight in the code:42,3.14,true,'A',"hi". - המרה מפורשת (cast): כותבים טיפוס בסוגריים כדי להמיר:
(int) 3.9נותן 3. בהמרה מ-double ל-int החלק העשרוני נחתך.cast: Writing a type in brackets to convert:(int) 3.9gives 3. Going from double to int cuts off the decimal part. - גלישה (overflow): כשהתוצאה גדולה מדי לטיפוס, היא "מתגלגלת" למספר שגוי, בלי שגיאה.
intמגיע עד כ-2.1 מיליארד.overflow: 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. - var: במקום לכתוב טיפוס,
varאומר למהדר להסיק אותו מהערך:var n = 5;הואint. הטיפוס עדיין קבוע.var: Instead of writing a type,vartells the compiler to infer it from the value:var n = 5;is anint. The type is still fixed. - final: משתנה
finalמקבל ערך פעם אחת ולא משתנה יותר. קבועים כותבים באותיות גדולות:final int MAX = 10;.final: Afinalvariable gets a value once and never changes. Constants are written in capitals:final int MAX = 10;.
02משתנה = קופסה עם שם וטיפוסA variable = a box with a name and a type
ב-Java כותבים קודם את הטיפוס, אחר כך את השם, ואז = והערך. = הוא השמה: "שים בקופסה". אפשר לשים ערך חדש, והישן נמחק.
In Java you write the type first, then the name, then = and the value. = is an assignment: "put in the box". You can put a new value in, and the old one is gone.
public class Main {
public static void main(String[] args) {
int age = 15;
String name = "נועה";
System.out.println(name + " בת " + age);
age = age + 1;
System.out.println("בשנה הבאה: " + age);
}
}public class Main {
public static void main(String[] args) {
int age = 15;
String name = "Noa";
System.out.println(name + " is " + age);
age = age + 1;
System.out.println("Next year: " + age);
}
}נועה בת 15 בשנה הבאה: 16
Noa is 15 Next year: 16
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
03הטיפוסים הבסיסייםThe basic types
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).
public class Main {
public static void main(String[] args) {
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);
}
}12 | 39.9 | true | A | 100000000000
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
04המרות: אוטומטית ומפורשתConversions: automatic and explicit
מ-int ל-double Java ממירה לבד (אין מה לאבד). בכיוון ההפוך צריך המרה מפורשת (cast) מפורש, והחלק העשרוני נחתך (לא מעוגל). לעיגול: Math.round.
From int to double Java converts by itself (nothing is lost). The other way needs an explicit cast, and the decimal part is cut off (not rounded). To round: Math.round.
public class Main {
public static void main(String[] args) {
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);
}
}7.0 9 10 3 vs 3.5
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
05גלישה: כשהמספר גדול מדיOverflow: when the number is too big
int מגיע עד 2147483647. אם עוברים אותו, אין שגיאה: התוצאה פשוט "מתגלגלת" למספר שלילי (גלישה). למספרים גדולים משתמשים ב-long.
int goes up to 2147483647. Go past it and there's no error: the result just "wraps around" to a negative number (overflow). For big numbers use long.
public class Main {
public static void main(String[] args) {
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);
}
}2147483647 -2147483648 2147483648
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
06var ו-finalvar and final
var נותן למהדר להסיק את הטיפוס מהערך, אבל הטיפוס עדיין קבוע. final נועל את הקופסה אחרי ההשמה הראשונה. קבועים כותבים באותיות גדולות.
var lets the compiler infer the type from the value, but the type is still fixed. final locks the box after the first assignment. Constants go in capitals.
public class Main {
public static void main(String[] args) {
var count = 3;
var ratio = 0.5;
final int MAX_BOOKS = 10;
System.out.println(count * ratio);
System.out.println("מקום פנוי: " + (MAX_BOOKS - count));
}
}public class Main {
public static void main(String[] args) {
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));
}
}1.5 מקום פנוי: 7
1.5 Free slots: 7
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
07הצעד של היום בפרויקט מדףToday's step in the Shelf project
שמרו ספר אחד במשתנים: String title, String author, int year, double rating ו-boolean read. הדפיסו כרטיס של 4 שורות: שם וסופר, שנה, Rating: 4.5/5, ו-Read: true.
Keep one book in variables: String title, String author, int year, double rating and boolean read. Print a 4-line card: title and author, year, Rating: 4.5/5, and Read: true.
את הצעד הזה כותבים בעמוד התרגול (התרגיל המסומן כצעד בפרויקט). בעמוד הפתרונות יש פתרון מלא עם סימולציה שמריצה אותו.
You write this step on the practice page (the exercise marked as a project step). The solutions page has a full solution with a simulation that runs it.
סיימתם את ההרצאה!Lecture complete!
עכשיו המצגת לחזרה מהירה, או ישר לתרגול.
Next: the slides for a quick review, or straight to practice.