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

חזרה למפגשBack
JavaJava

משתנים וטיפוסים פרימיטיבייםVariables & primitive types

מפגש 2: int, double, boolean, char ו-long, var, המרות טיפוסים, גלישה (overflow) וחלוקה של מספרים שלמים.Session 2: int, double, boolean, char and long, var, type conversions, overflow and integer division.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • משתנה = קופסה עם שם וטיפוסA variable = a box with a name and a type
  • הטיפוסים הבסיסייםThe basic types
  • המרות: אוטומטית ומפורשתConversions: automatic and explicit
  • גלישה: כשהמספר גדול מדיOverflow: when the number is too big
  • var ו-finalvar and final
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • משתנה: קופסה עם שם ששומרת ערך.variable: A named box that holds a value.
  • טיפוס: מה מותר לשים בקופסה:type: What may go into a box:
  • טיפוס פרימיטיבי: 8 הטיפוסים הבסיסיים של Java (int, long, double, boolean, char ועוד).primitive type: Java's 8 basic types (int, long, double, boolean, char and more).
  • הצהרה: השורה שיוצרת משתנה:declaration: The line that creates a variable:
  • השמה: שמים ערך בקופסה עם =.assignment: Putting a value in a box with =.
  • ליטרל: ערך שכתוב ישר בקוד:literal: A value written straight in the code:
נושאTopic 2

משתנה = קופסה עם שם וטיפוסA variable = a box with a name and a type

משתנה = קופסה עם שם וטיפוס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.
שאלה לכיתהQuestion for the class

מה ידפיס הקוד?What will this print?

main
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);

    משתנה = קופסה עם שם וטיפוסA variable = a box with a name and a type

    Main.java · main
    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);
    פלטOutput
    נועה בת 15
    בשנה הבאה: 16
    Noa is 15
    Next year: 16
    נושאTopic 3

    הטיפוסים הבסיסייםThe basic types

    הטיפוסים הבסיסיים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).
    נושאTopic 4

    המרות: אוטומטית ומפורשתConversions: automatic and explicit

    המרות: אוטומטית ומפורשתConversions: automatic and explicit

    • מ-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).
    • לעיגול: Math.round.To round: Math.round.
    נושאTopic 5

    גלישה: כשהמספר גדול מדיOverflow: when the number is too big

    גלישה: כשהמספר גדול מדיOverflow: when the number is too big

    • 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).
    • למספרים גדולים משתמשים ב-long.For big numbers use long.
    שאלה לכיתהQuestion for the class

    מה ידפיס הקוד?What will this print?

    main
    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);

      גלישה: כשהמספר גדול מדיOverflow: when the number is too big

      Main.java · main
      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);
      פלטOutput
      2147483647
      -2147483648
      2147483648
      נושאTopic 6

      var ו-finalvar and final

      var ו-finalvar and final

      • var נותן למהדר להסיק את הטיפוס מהערך, אבל הטיפוס עדיין קבוע.var lets the compiler infer the type from the value, but the type is still fixed.
      • final נועל את הקופסה אחרי ההשמה הראשונה.final locks the box after the first assignment.
      • קבועים כותבים באותיות גדולות.Constants go in capitals.
      נושאTopic 7

      פרויקט מדףShelf project

      הצעד של היום בפרויקט מדףToday's step in the Shelf project

      • מדף, צעד 2: ספר במשתניםShelf, step 2: a book in variables

      משתנה: קופסה עם שם ששומרת ערך.variable: A named box that holds a value.

      טיפוס: מה מותר לשים בקופסה:type: What may go into a box:

      טיפוס פרימיטיבי: 8 הטיפוסים הבסיסיים של Java (int, long, double, boolean, char ועוד).primitive type: Java's 8 basic types (int, long, double, boolean, char and more).

      הצהרה: השורה שיוצרת משתנה:declaration: The line that creates a variable:

      השמה: שמים ערך בקופסה עם =.assignment: Putting a value in a box with =.

      ליטרל: ערך שכתוב ישר בקוד:literal: A value written straight in the code:

      ב-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.

      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).

      לעיגול: Math.round.To round: Math.round.

      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).

      למספרים גדולים משתמשים ב-long.For big numbers use long.

      var נותן למהדר להסיק את הטיפוס מהערך, אבל הטיפוס עדיין קבוע.var lets the compiler infer the type from the value, but the type is still fixed.

      final נועל את הקופסה אחרי ההשמה הראשונה.final locks the box after the first assignment.

      קבועים כותבים באותיות גדולות.Constants go in capitals.

      מדף, צעד 2: ספר במשתניםShelf, step 2: a book in variables

      מה ידפיס הקוד?What does this code print?

      • 77
      • 1010
      • 1414
      • xx

      1010

      מה הערך של (int) 9.99?What is the value of (int) 9.99?

      • 1010
      • 99
      • 9.999.99
      • שגיאת קומפילציהA compile error

      99

      מה למדנוWhat we learned

      • משתנה: קופסה עם שם ששומרת ערך.variable: A named box that holds a value.
      • ב-Java כותבים קודם את הטיפוס, אחר כך את השם, ואז = והערך.In Java you write the type first, then the name, then = and the value.
      • 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).
      • int מגיע עד 2147483647.int goes up to 2147483647.
      • var נותן למהדר להסיק את הטיפוס מהערך, אבל הטיפוס עדיין קבוע.var lets the compiler infer the type from the value, but the type is still fixed.

      עכשיו לתרגולNow, practice

      6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
      הערות מרצהSpeaker notes

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