CS I · Wed Oct 28 · 13.3 · We Do

Before the bell

You are responsible for all of this before the end of class.

  1. The discussion form in the I Do deck. All three sections. If you are here, this should be done.
  2. Your classwork. This form on the right. Section 1 before the exit ticket pops up; Section 2 past the floor.
  3. Today's Bench Basics activity: True or False. It opens later in class and lives on slide 4 of this deck.
  4. The exit ticket. It opens 15 minutes before the bell and lives on the last slide of this deck.
Classwork

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

True or False: How a Computer Checks

This reading explains how a computer checks true-or-false statements. A computer makes its decisions with checks like these, and in late November the class will see Java write the same checks.

Comparing Two Numbers

A comparison puts a number on each side of a symbol, and the result is either true or false. The symbol `<` means less than, so `8 < 12` is true, and `<=` means less than or equal to, so `12 <= 12` is true. The symbol `>` means greater than, so `20 > 25` is false, and `>=` means greater than or equal to, so `25 >= 25` is true. The pair to watch is `15 < 15`, which is false, and `15 <= 15`, which is true, because `<=` allows the two sides to be equal and `<` does not.

Joining Checks with And, Or and Not

The word and joins two checks, and the result is true only when both sides are true. The word or gives true when at least one side is true, including when both are. The word not flips a result, so true becomes false and false becomes true. The official rules for Java define all three words in this same way. The table below starts with one box, `score`, holding 74.

1score ← 74
2
3check                                   left side    right side    result
4score >= 70                                 —            —          true
5score < 74                                  —            —          false
6score <= 74                                 —            —          true
7score >= 70 and score <= 79               true         true         true
8score < 60 or score > 90                  false        false        false
9not (score > 80)                            —            —          true

The first row is true because 74 is greater than 70, and the second row is false because 74 is not less than itself. The third row is true because `<=` allows the sides to be equal. In the fourth row both sides are true, so the and is true, but in the fifth row 74 is neither below 60 nor above 90, so the or is false. In the last row `score > 80` is false, and not flips it to true.

Inclusive Ranges

A range such as "a score from 70 to 79, inclusive" means that both ends count, so it is written `score >= 70 and score <= 79`. The table below tests three values.

1score ← 70     score >= 70 and score <= 79     true and true      true
2score ← 79     score >= 70 and score <= 79     true and true      true
3score ← 80     score >= 70 and score <= 79     true and false     false

Bench Basics checks for two mistakes, and the first is using `<` where the range says inclusive, because `score < 79` is false when the score is 79. The second is reading or as "only one of them," because or is still true when both sides are true.

Sources: Oracle, The Java Language Specification, Java SE 21 Edition, Chapter 15 (n.d.).

Bytecode and the Java Virtual Machine

This section explains how one Java program can run on many kinds of computers, and why schools and companies choose Java.

One Program and Many Chips

As the previous lesson explained, a person writes source code in a programming language. A compiler then turns it into a form a computer can run. Java began as part of a research project to write software for a wide variety of devices. That goal raised a problem, because devices have different chips, and each kind of chip runs its own set of instructions. A program compiled straight into the instructions for one chip will not run on a different kind of chip. One fix is to compile the program again for every kind of computer. In 1995, the designers of Java wrote that software makers had to produce separate versions of each application for IBM PCs, Macintosh computers and many UNIX workstations.

How Java Uses Bytecode

Java solves the problem in a different way. First, a person writes source code in a plain text file whose name ends in .java, such as Main.java. Next, the Java compiler reads that file and produces bytecode, a set of simple instructions that no real chip runs directly. The compiler saves the bytecode in a file whose name ends in .class, such as Main.class. For example, a person wrote the short program below, which prints one line.

1public class Main {
2    public static void main(String[] args) {
3        System.out.println("Hello from Java");
4    }
5}
Hello from Java

A tool called javap lists the bytecode inside Main.class, and the main part of the program became four instructions.

       0: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #13                 // String Hello from Java
       5: invokevirtual #15                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return

Bytecode is not source code, because a person does not write it, and the compiler made these four lines from the source code.

The Java Virtual Machine

The Java Virtual Machine (JVM) is a program installed on a computer that reads bytecode and runs it on that computer's chip. Each kind of computer gets its own JVM, and the bytecode stays the same. Oracle's Java tutorial states that the same .class files can run on Microsoft Windows, Linux and Mac computers. The Java Virtual Machine is not a separate computer that a person can touch, because it is a program running on the computer the person already has. The course uses one sentence to describe how Java runs: Java is compiled to bytecode, which runs on the Java Virtual Machine. Figure 1 shows one Main.class file going to three computers, and each computer has its own JVM.

A program is portable when it can move to a different kind of computer and run without being rewritten. A laptop is portable because a person can carry it, but a program is portable only if it runs on another kind of computer unchanged. Java bytecode is portable because every JVM reads the same bytecode. A program compiled straight for one chip is not portable in this sense, because someone has to compile it again for each new kind of chip. The Java Virtual Machine Specification states that programmers "can write a program once, and it will run on any machine supplying a Java run-time environment."

Other Reasons People Choose Java

Portability is one reason people choose Java, and there are three others. First, Java makes the programmer say what kind of value each box in a program holds, such as a whole number or a piece of text. The Java Language Specification states that this kind of checking "helps detect errors at compile time." For example, if a program tries to store a word in a box meant for a whole number, the compiler reports an error before the program ever runs. A mistake in a program's plan is not caught this way. For example, a program that adds where it should subtract still compiles and runs.

Second, Java comes with a large standard library, which is code that is already written and ready to use. Oracle describes this library as "a large collection of ready-made software components," grouped into packages of related code.

Third, Java programs written many years ago usually still run on new versions of Java. Oracle states that most code should work on Java 21 without changes. A company with a lot of old code can usually move to a new version without rewriting it, although Oracle has removed some old features over the years.

Schools use Java as well. The College Board wrote a course description in 2003. It states that AP Computer Science courses and exams would use Java starting with the 2003–04 school year and the 2004 exams. Today AP Computer Science A, the course that follows this one in the spring, still uses a subset of Java. Friday's lesson is about its exam.

Summary

Java is compiled to bytecode, which runs on the Java Virtual Machine. Each kind of computer has its own JVM, so the same bytecode is portable. People also choose Java because its compiler catches some mistakes early, it has a large standard library, and old programs usually still run.

Sources: Oracle, About the Java Technology (n.d.); Oracle, JVM Specification, SE 21 (n.d.); Oracle, Java Language Specification, SE 21 (n.d.); Gosling and McGilton, The Java Language Environment (1995); Oracle, JDK 21 Migration Guide (n.d.); College Board, Course Description (2003); College Board, AP Computer Science A (n.d.); course JDK, Main.java (2026).

Classwork · Section 1 — Reading 2

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

MC 1. What is bytecode?

  • Simple instructions the Java compiler makes from source code, which no real chip runs directly
  • The source code a programmer types into a file such as Main.java
  • The chip's own instructions, made for one kind of computer only
  • A program installed on each computer so that Java programs can run

MC 2. What does the Java Virtual Machine do?

  • It turns Main.java into Main.class before the program can run.
  • It is a separate machine plugged into a computer to run Java.
  • It reads bytecode and runs it on the chip of the computer it is on.
  • It rewrites the program's source code for each new kind of chip.

MC 3. In today's passage, what does it mean for a program to be portable?

  • It is small enough to carry on a flash drive or a laptop.
  • It has been compiled again, once for every kind of chip.
  • It runs only on the kind of computer it was compiled on.
  • It can move to a different kind of computer and run without being rewritten.

MC 4. Which is one reason besides portability that people choose Java, according to the passage?

  • The compiler catches some mistakes before the program runs, because each box holds a stated kind of value.
  • The compiler catches every mistake, so a program that compiles always does what its writer planned.
  • Java programs run on any computer without any other program installed on it.
  • Old Java programs are always rewritten for each new version, so they stay up to date.

MC 5. A box holds n ← 30. Is the check n < 30 true or false?

  • True, because 30 and 30 are equal.
  • False, because 30 is not less than itself.
  • True, because < means "less than or equal to."
  • False, because 30 is greater than 30.

MC 6. A box holds t ← 45. Is the check t <= 45 true or false?

  • False, because 45 is not less than 45.
  • False, because <= means only "less than."
  • True, because 45 is less than 45.
  • True, because <= allows the two sides to be equal.

MC 7. A box holds p ← 12. Is the check p > 10 and p > 15 true or false?

  • True, because p > 10 is true.
  • False, because p > 15 is false, and and needs both sides to be true.
  • True, because at least one of the two sides is true.
  • False, because both of the two sides are false.

MC 8. A box holds d ← 3. Is the check d < 5 or d > 8 true or false?

  • True, because d < 5 is true, and or needs only one true side.
  • False, because d > 8 is false.
  • True, because both of the two sides are true.
  • False, because d cannot be below 5 and above 8 at once.

MC 9. A box holds k ← 9. Is the check not (k >= 9) true or false?

  • True, because k >= 9 is true.
  • True, because k >= 9 is false when k is 9.
  • False, because k >= 9 is true and not flips it to false.
  • False, because not makes the number 9 smaller.

MC 10. A box w holds a number. Which check matches "a number from 30 to 40, inclusive"?

  • w > 30 and w < 40
  • w >= 30 or w <= 40
  • w >= 30 and w <= 40
  • w <= 30 and w >= 40

CR 1. A box holds h ← 25. Mark each of the four checks below true or false, and give the reason for each one in a sentence.

1h ← 25
2h > 25
3h >= 25
4h > 20 and h < 25
5not (h < 10)

Write your answer in your lab notebook, labeled CR 1.

CR 2. Explain in two sentences how one Main.class file can run on a Windows laptop and on a Mac. Use one fact from the passage or Figure 1.

Write your answer in your lab notebook, labeled CR 2.

Figure 1. The compiler turns Main.java into one Main.class file of bytecode, and the same file runs on a Windows laptop, a Mac and a Linux server, each with its own Java Virtual Machine.
Classwork · Section 2 — Past the floor

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

CR 3. A thermostat should report "comfortable" for a temperature from 65 to 75, inclusive. Write the check for that range using a box named temp. Then test your check with 64, 65 and 75, and show whether each side is true or false for each value.

Write your answer in your lab notebook, labeled CR 3.

CR 4. A company has a program compiled straight for one kind of chip, and it wants the program on a second kind of computer. Compare that with a Java program. Use the words bytecode and Java Virtual Machine in your answer.

Write your answer in your lab notebook, labeled CR 4.

Bench Basics

True or False

Open the bench, do its six jobs, then paste your credit code into the credit-code form. You can turn in up to three codes per bench; your best score is your grade.

Bench Basics opens during your class period.

Classwork

The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.

Exit Ticket

This opens during your class period.