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

חזרה למפגשBack
JavaJava

מה זה תכנות? JVM, JDK, IntelliJ ו-jshellWhat is programming? JVM, JDK, IntelliJ & jshell

מפגש 1: איך מחשב מבצע הוראות, איך Java רצה (קוד, javac, bytecode, JVM), התקנת JDK ו-IntelliJ ב-Windows וב-Mac, jshell, והתוכנית הראשונה.Session 1: How a computer follows instructions, how Java runs (source, javac, bytecode, the JVM), installing the JDK and IntelliJ on Windows and Mac, jshell, and a first program.

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

  • מה זה תכנותWhat programming is
  • למה JavaWhy Java
  • javac, bytecode ו-JVMjavac, bytecode, JVM
  • התקנת JDKInstalling the JDK
  • IntelliJIntelliJ
  • מבנה תוכניתProgram shape
נושאTopic 1

מה זה תכנותWhat programming is

מה זה תכנות?What is programming?

  • תוכנית היא רשימת הוראות שהמחשב מבצע לפי הסדרA program is a list of instructions the computer carries out in order
  • המחשב לא מנחש: הוא עושה בדיוק מה שכתובThe computer never guesses: it does exactly what's written
  • Java רגישה לאותיות גדולות: println ו-Println הן מילים שונותJava is case-sensitive: println and Println are different words

אנלוגיהAnalogy

תחשבו על רובוט-שף שמקבל מתכון. הוא יבצע "שבור 2 ביצים" בדיוק, צעד אחרי צעד. אבל אם כתבתם "שבור 2 ביצם", הוא לא ינחש שהתכוונתם לביצים. הוא יעצור ויגיד "לא הבנתי". ככה בדיוק מתנהגת Java: מדויקת, צייתנית, ובלי דמיון.Think of a robot chef following a recipe. It will "crack 2 eggs" exactly, step by step. But if you wrote "crack 2 egs", it won't guess you meant eggs. It stops and says "I didn't understand". Java behaves exactly like that: precise, obedient, and with zero imagination.

טעות נפוצהCommon mistake

Main.java · main
System.out.Println("שלום");
System.out.Println("Hello");
מה javac עונהWhat javac says
Main.java:3: error: cannot find symbol
cannot find symbol אומרת "אני לא מכירה את השם הזה". השורה symbol: method Println(String) מראה בדיוק איזה שם: אין מתודה Println. מתקנים ל-println עם p קטנה.cannot find symbol means "I don't know this name". The line symbol: method Println(String) shows exactly which name: there's no method Println. Fix it to lowercase println.
נושאTopic 2

למה JavaWhy Java

למה דווקא Java?Why Java?

  • Java רצה בשרתים, ב-Android, בבנקים, במערכות נתונים ובמשחקיםJava runs servers, Android, banks, data systems and games
  • ה-מהדר בודק את הקוד לפני שהוא רץ, ותופס הרבה טעויות מוקדםThe compiler checks the code before it runs, and catches many mistakes early
  • מה שבמירכאות הוא טקסט (מחרוזת); מה שבלי מירכאות, Java מחשבתWhat's in quotes is text (a string); without quotes, Java calculates
שאלה לכיתהQuestion for the class

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

main
System.out.println(2 + 3);
System.out.println("2 + 3");

    למה JavaWhy Java

    Main.java · main
    System.out.println(2 + 3);
    System.out.println("2 + 3");
    פלטOutput
    5
    2 + 3

    אנלוגיהAnalogy

    המהדר של Java הוא כמו מורה קפדנית שבודקת את שיעורי הבית לפני השיעור: היא לא תיתן לכם להגיש עבודה עם מילה חסרה. זה מתסכל בהתחלה, אבל זה חוסך הרבה הפתעות כשהתוכנה כבר עובדת אצל אנשים אמיתיים.Java's compiler is like a strict teacher who checks your homework before class: she won't let you hand in work with a missing word. It's frustrating at first, but it saves a lot of surprises once the software is running for real people.

    טעות נפוצהCommon mistake

    Main.java · main
    System.out.println(2 +);
    מה javac עונהWhat javac says
    Main.java:3: error: illegal start of expression
    illegal start of expression היא שגיאת קומפילציה: השורה לא כתובה לפי חוקי ה-תחביר. ה-^ מצביע בערך למקום שבו המהדר הסתבך. משלימים: System.out.println(2 + 3);.illegal start of expression is a compile error: the line doesn't follow the syntax rules. The ^ points roughly to where the compiler got confused. Complete it: System.out.println(2 + 3);.
    נושאTopic 3

    javac, bytecode ו-JVMjavac, bytecode, JVM

    איך Java רצה: javac, bytecode ו-JVMHow Java runs: javac, bytecode and the JVM

    • שני שלבים: javac Main.java מקמפל, java Main מריץTwo steps: javac Main.java compiles, java Main runs
    • ה-מהדר יוצר bytecode בקובץ ‎.class, וה-JVM מריצה אותוThe compiler makes bytecode in a .class file, and the JVM runs it
    • אותו bytecode רץ בכל מחשב שיש בו JVMThe same bytecode runs on any computer with a JVM

    אנלוגיהAnalogy

    דמיינו מתכון שכתוב בעברית. המהדר מתרגם אותו פעם אחת ל"שפת הרובוטים" (bytecode). בכל מטבח בעולם יש רובוט שמדבר את השפה הזו (JVM): אחד ל-Windows, אחד ל-Mac, אחד ל-Linux ואפילו אחד לדפדפן. לכן את אותו מתכון מתורגם אפשר לתת לכל מטבח. בעולם של Java קוראים לזה "כותבים פעם אחת, רץ בכל מקום".Picture a recipe written in English. The compiler translates it once into "robot language" (bytecode). Every kitchen in the world has a robot that speaks that language (a JVM): one for Windows, one for Mac, one for Linux and even one for the browser. So the same translated recipe can go to any kitchen. In the Java world this is called "write once, run anywhere".
    נושאTopic 4

    התקנת JDKInstalling the JDK

    התקנת JDK 21 (Windows ו-Mac)Installing JDK 21 (Windows and Mac)

    • מתקינים JDK 21 (LTS), למשל Eclipse Temurin מ-adoptium.netInstall JDK 21 (LTS), for example Eclipse Temurin from adoptium.net
    • בודקים בטרמינל: java --version וגם javac --versionCheck in the terminal: java --version and also javac --version
    • ב-Windows מסמנים בהתקנה Set JAVA_HOME ו-Add to PATHOn Windows tick Set JAVA_HOME and Add to PATH during setup
    נושאTopic 5

    IntelliJIntelliJ

    IntelliJ IDEA והתוכנית הראשונהIntelliJ IDEA and the first program

    • IntelliJ IDEA Community היא סביבת פיתוח חינמית: עורך, הרצה ודיבוג במקום אחדIntelliJ IDEA Community is a free IDE: editor, running and debugging in one place
    • קיצורים: psvm ואז Tab כותב את main, ו-sout ואז Tab כותב System.out.println()Shortcuts: psvm then Tab writes main, and sout then Tab writes System.out.println()
    • שם הקובץ חייב להיות זהה לשם המחלקה הציבוריתThe file name must match the public class name
    שאלה לכיתהQuestion for the class

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

    main
    System.out.println("שלום מ-IntelliJ");
    System.out.println("Hello from IntelliJ");

      IntelliJIntelliJ

      Main.java · main
      System.out.println("שלום מ-IntelliJ");
      System.out.println("Hello from IntelliJ");
      פלטOutput
      שלום מ-IntelliJ
      Hello from IntelliJ

      אנלוגיהAnalogy

      עורך טקסט רגיל הוא כמו מחברת ועט. IntelliJ הוא כמו סדנה מסודרת: יש בה מחברת, אבל גם מורה שמסמנת טעויות בזמן הכתיבה, מכונה שמשלימה מילים, וכפתור אחד שמקמפל ומריץ. מאחורי הכפתור הירוק IntelliJ מריץ בדיוק את מה שראינו: javac ואחריו java.A plain text editor is like a notebook and a pen. IntelliJ is like a well-equipped workshop: there's a notebook, but also a teacher who marks mistakes as you write, a machine that finishes words, and one button that compiles and runs. Behind the green button IntelliJ runs exactly what we saw: javac, then java.

      טעות נפוצהCommon mistake

      Main.java · main
      System.out.println("Hi");
      מה javac עונהWhat javac says
      Main.java:1: error: class Hello is public, should be declared in a file named Hello.java
      class Hello is public, should be declared in a file named Hello.java: המהדר מבקש להתאים. משנים את שם הקובץ ל-Hello.java (ב-IntelliJ: קליק ימני ← Refactor ← Rename), או את שם המחלקה ל-Main.class Hello is public, should be declared in a file named Hello.java: the compiler wants them to match. Rename the file to Hello.java (in IntelliJ: right-click → Refactor → Rename), or rename the class to Main.
      נושאTopic 6

      מבנה תוכניתProgram shape

      המבנה של תוכנית JavaThe shape of a Java program

      • מחלקה עוטפת הכול, ובתוכה main, שבתוכה המשפטיםA class wraps everything, inside it main, inside that the statements
      • בלוק נפתח ב-{ ונסגר ב-}; כל משפט נגמר ב-;A block opens with { and closes with }; every statement ends with ;
      • הערה: // עד סוף השורה, או בין /* ל-*/A comment: // to the end of the line, or between /* and */
      שאלה לכיתהQuestion for the class

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

      main
      // התוכנית מדפיסה כרטיס קטן
      System.out.println("+----------+");
      System.out.println("| Java 21  |");
      System.out.println("+----------+");
      System.out.println(7 * 6);
      // This program prints a small card
      System.out.println("+----------+");
      System.out.println("| Java 21  |");
      System.out.println("+----------+");
      System.out.println(7 * 6);

        מבנה תוכניתProgram shape

        Main.java · main
        // התוכנית מדפיסה כרטיס קטן
        System.out.println("+----------+");
        System.out.println("| Java 21  |");
        System.out.println("+----------+");
        System.out.println(7 * 6);
        // This program prints a small card
        System.out.println("+----------+");
        System.out.println("| Java 21  |");
        System.out.println("+----------+");
        System.out.println(7 * 6);
        פלטOutput
        +----------+
        | Java 21  |
        +----------+
        42
        +----------+
        | Java 21  |
        +----------+
        42

        אנלוגיהAnalogy

        תחשבו על בובות מטריושקה: המחלקה היא הבובה הגדולה, main היא הבובה שבתוכה, והמשפטים הם מה שבפנים. כל { פותח בובה וכל } סוגר אותה, ולכן הם תמיד באים בזוגות.Think of Russian nesting dolls: the class is the big doll, main is the doll inside it, and the statements are what's inside that. Each { opens a doll and each } closes it, so they always come in pairs.

        טעות נפוצהCommon mistake

        Main.java · main
        System.out.println("שורה ראשונה")
        System.out.println("שורה שנייה");
        System.out.println("First line")
        System.out.println("Second line");
        מה javac עונהWhat javac says
        Main.java:3: error: ';' expected
        ';' expected אומרת "ציפיתי לנקודה-פסיק". ה-^ מצביע לסוף השורה שבה היא חסרה. מוסיפים ;. שימו לב: התוכנית לא הדפיסה אפילו את השורה השנייה, כי עם שגיאת קומפילציה שום דבר לא רץ.';' expected means "I expected a semicolon". The ^ points at the end of the line where it's missing. Add the ;. Notice: the program didn't print even the second line, because with a compile error nothing runs.
        נושאTopic 7

        הדפסה וחשבוןPrinting and maths

        println, print ו-printfprintln, print and printf

        • println מדפיסה ויורדת שורה; print נשארת באותה שורהprintln prints and goes to a new line; print stays on the same line
        • + בין טקסט למספר מחבר טקסט; סוגריים מחשבים קודם+ between text and a number joins text; brackets calculate first
        • 7 / 2 הוא 3 (חלוקה של שלמים), 7 / 2.0 הוא 3.5, ו-7 % 2 הוא 17 / 2 is 3 (integer division), 7 / 2.0 is 3.5, and 7 % 2 is 1
        שאלה לכיתהQuestion for the class

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

        main
        System.out.print("אחת, ");
        System.out.print("שתיים, ");
        System.out.println("שלוש!");
        System.out.println();
        System.out.println("אחרי שורה ריקה");
        System.out.print("One, ");
        System.out.print("two, ");
        System.out.println("three!");
        System.out.println();
        System.out.println("After a blank line");

          הדפסה וחשבוןPrinting and maths

          Main.java · main
          System.out.print("אחת, ");
          System.out.print("שתיים, ");
          System.out.println("שלוש!");
          System.out.println();
          System.out.println("אחרי שורה ריקה");
          System.out.print("One, ");
          System.out.print("two, ");
          System.out.println("three!");
          System.out.println();
          System.out.println("After a blank line");
          פלטOutput
          אחת, שתיים, שלוש!
          
          אחרי שורה ריקה
          One, two, three!
          
          After a blank line

          אנלוגיהAnalogy

          println היא כמו מכונת כתיבה שאחרי כל משפט מחזירה את הגלגלת לשורה חדשה. print כותבת ומשאירה את הגלגלת בדיוק איפה שעצרה.println is like a typewriter that returns the carriage to a new line after each sentence. print types and leaves the carriage exactly where it stopped.

          טעות נפוצהCommon mistake

          Main.java · main
          System.out.println(Hello);
          מה javac עונהWhat javac says
          Main.java:3: error: cannot find symbol
          cannot find symbol עם symbol: variable Hello: המהדר חיפש משתנה בשם Hello ולא מצא. טקסט תמיד בין מירכאות כפולות: "Hello".cannot find symbol with symbol: variable Hello: the compiler looked for a variable named Hello and found none. Text always goes between double quotes: "Hello".
          נושאTopic 8

          jshelljshell

          jshell: לנסות שורה אחתjshell: try one line

          • jshell מריץ שורת Java מיד, בלי class ובלי mainjshell runs a line of Java at once, with no class and no main
          • תוצאה בלי שם נשמרת ב-$1, $2 וכן הלאהA result with no name is kept in $1, $2 and so on
          • יוצאים עם /exitLeave with /exit

          אנלוגיהAnalogy

          jshell הוא כמו דף טיוטה ליד המחברת: מנסים עליו תרגיל קטן, רואים מה יוצא, ואז כותבים את התשובה המסודרת במחברת (בתוכנית אמיתית).jshell is like scratch paper next to your notebook: try a small sum on it, see what comes out, then write the neat answer in the notebook (a real program).

          טעות נפוצהCommon mistake

          Main.java
          public class Main {
              System.out.println("Hi");
          }
          מה javac עונהWhat javac says
          Main.java:2: error: <identifier> expected
          <identifier> expected: בתוך מחלקה המהדר מצפה להגדרות (מתודות ושדות), לא למשפטים. מכניסים את השורה לתוך public static void main(String[] args) { ... }.<identifier> expected: inside a class the compiler expects definitions (methods and fields), not statements. Put the line inside public static void main(String[] args) { ... }.
          נושאTopic 9

          שני סוגי שגיאותTwo kinds of errors

          שני סוגי שגיאות: קומפילציה וזמן ריצהTwo kinds of errors: compile time and run time

          • שגיאת קומפילציה: javac מסרב, ושום דבר לא רץA compile error: javac refuses, and nothing runs
          • חריגה: התוכנית התחילה לרוץ ונעצרה באמצעAn exception: the program started running and stopped halfway
          • קוראים את השורה הראשונה (מה קרה) ואת at ...(Main.java:N) (איפה)Read the first line (what happened) and at ...(Main.java:N) (where)

          אנלוגיהAnalogy

          שגיאת קומפילציה היא כמו בדיקת איות לפני ששולחים מכתב: המכתב לא יוצא עד שמתקנים. חריגה היא כמו רכב שיצא לדרך ונתקע באמצע: הדרך עד שם כבר נעשתה.A compile error is like a spellcheck before sending a letter: the letter doesn't go out until it's fixed. An exception is like a car that set off and broke down halfway: the road up to there was already travelled.

          טעות נפוצהCommon mistake

          Main.java · main
          System.out.println("ההתחלה");
          System.out.println("הסוף);
          System.out.println("The start");
          System.out.println("The end);
          מה javac עונהWhat javac says
          Main.java:4: error: unclosed string literal
          unclosed string literal = "מחרוזת שלא נסגרה". מוסיפים " לפני הסוגר. ושימו לב: אפילו השורה הראשונה, שהייתה תקינה, לא הודפסה. זו שגיאת קומפילציה.unclosed string literal = "a string that was never closed". Add the " before the bracket. And notice: even the first line, which was fine, didn't print. That's a compile error.

          מה עושה הפקודה javac Main.java?What does the command javac Main.java do?

          • מריצה את התוכנית ומדפיסה את הפלטRuns the program and prints its output
          • בודקת את הקוד ויוצרת Main.class עם bytecodeChecks the code and creates Main.class with bytecode
          • מתקינה את Java במחשבInstalls Java on the computer
          • פותחת את IntelliJOpens IntelliJ

          בודקת את הקוד ויוצרת Main.class עם bytecodeChecks the code and creates Main.class with bytecode

          מה למדנוWhat we learned

          • תוכנית היא רשימת הוראות שהמחשב מבצע לפי הסדרA program is a list of instructions the computer carries out in order
          • Java רצה בשרתים, ב-Android, בבנקים, במערכות נתונים ובמשחקיםJava runs servers, Android, banks, data systems and games
          • שני שלבים: javac Main.java מקמפל, java Main מריץTwo steps: javac Main.java compiles, java Main runs
          • מתקינים JDK 21 (LTS), למשל Eclipse Temurin מ-adoptium.netInstall JDK 21 (LTS), for example Eclipse Temurin from adoptium.net
          • IntelliJ IDEA Community היא סביבת פיתוח חינמית: עורך, הרצה ודיבוג במקום אחדIntelliJ IDEA Community is a free IDE: editor, running and debugging in one place
          • מחלקה עוטפת הכול, ובתוכה main, שבתוכה המשפטיםA class wraps everything, inside it main, inside that the statements

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

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

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