CS I · Wed Oct 28 · 13.3 · Do Now

Why Java

Open the discussion form on the right and answer Section 1 from your first reading.

Announcements

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

I Do questions
  1. notebook cue: bytecode

    The Java compiler reads Main.java and makes Main.class. What is inside Main.class, and who wrote it?

    Check

    Main.class holds bytecode, a set of simple instructions that no real chip runs directly. No person wrote it; the compiler made it from the source code in Main.java. See How Java Uses Bytecode, where javap lists the four instructions the compiler made.

  2. notebook cue: Java Virtual Machine

    The same Main.class file runs on a Windows laptop and on a Mac. What does each computer need installed, and what does that program do?

    Check

    Each computer needs its own Java Virtual Machine installed. The JVM is a program that reads the bytecode in Main.class and runs it on that computer's chip, so the bytecode never changes. See The Java Virtual Machine, paragraph 1, and Figure 1.

  3. notebook cue: portable

    Why is a Java program called portable, when a program compiled straight for one chip is not?

    Check

    Java bytecode can move to a different kind of computer and run without being rewritten, because every JVM reads the same bytecode. Someone has to compile a program made straight for one chip again for each new kind of chip. See The Java Virtual Machine, paragraph 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.
I Do questions
  1. notebook cue: bytecode

    The Java compiler reads Main.java and makes Main.class. What is inside Main.class, and who wrote it?

    Check

    Main.class holds bytecode, a set of simple instructions that no real chip runs directly. No person wrote it; the compiler made it from the source code in Main.java. See How Java Uses Bytecode, where javap lists the four instructions the compiler made.

  2. notebook cue: Java Virtual Machine

    The same Main.class file runs on a Windows laptop and on a Mac. What does each computer need installed, and what does that program do?

    Check

    Each computer needs its own Java Virtual Machine installed. The JVM is a program that reads the bytecode in Main.class and runs it on that computer's chip, so the bytecode never changes. See The Java Virtual Machine, paragraph 1, and Figure 1.

  3. notebook cue: portable

    Why is a Java program called portable, when a program compiled straight for one chip is not?

    Check

    Java bytecode can move to a different kind of computer and run without being rewritten, because every JVM reads the same bytecode. Someone has to compile a program made straight for one chip again for each new kind of chip. See The Java Virtual Machine, paragraph 2.

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

Table Talk
A Windows laptop, a Mac and a server in a bank all have different chips inside. A company wants one program to run on all three. Name two different ways the company could make that happen.

Each table lists two ways to get one program running on three different computers, and agrees on which way would take less work if there were a hundred kinds of computers instead of three.

Chromebooks stay closed while your table talks. Sections 1 and 2 of the discussion form are already done. After you talk, finish Section 3 right here under My View. Submit before the exit ticket.

This form has three sections. Section 1: answer today's Board Question. Section 2: after the reading. Section 3 opens later in class. Today's Board Question: A Windows laptop, a Mac and a server in a bank all have different chips inside. A company wants one program to run on all three. Name two different ways the company could make that happen.

CR 1. Board Question: A Windows laptop, a Mac and a server in a bank all have different chips inside. A company wants one program to run on all three. Name two different ways the company could make that happen. Give your answer and one reason.

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

MC 1. Think about your answer to the board question. A Windows laptop, a Mac and a server in a bank all have different chips inside. A company wants one program to run on all three. Name two different ways the company could make that happen. Which position is closest to yours now?

  • Write and compile the program three times, once for each kind of chip.
  • Compile it once into an in-between form, and put a program on each computer that runs that form.
  • Nothing is needed. Every computer can run every program.

MC 2. (Reading claim 1) Java bytecode runs only on the kind of computer it was compiled on.

  • Agree
  • Disagree

MC 3. (Reading claim 2) The Java Virtual Machine is a program.

  • Agree
  • Disagree

MC 4. (Reading claim 3) Java's compiler can catch some mistakes before the program runs.

  • Agree
  • Disagree

MC 5. (Reading claim 4) A person writes the bytecode in Main.class by hand.

  • Agree
  • Disagree

MC 6. (Reading claim 5) In Figure 1, the Windows laptop, the Mac and the Linux server all receive the same Main.class file.

  • Agree
  • Disagree

MC 7. (Reading claim 6) A program compiled straight for one kind of chip can run on any other kind of chip.

  • Agree
  • Disagree

MC 8. (Reading claim 7) A Java program written many years ago usually still runs on a new version of Java.

  • Agree
  • Disagree

MC 9. (Reading claim 8) AP Computer Science A has used Java since the 2003–04 school year.

  • Agree
  • Disagree

CR 2. (A different view) Write the view at your table that was most different from yours. If everyone agreed, write the strongest argument against your table's answer.

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

CR 3. Think about the Board Question again: A Windows laptop, a Mac and a server in a bank all have different chips inside. A company wants one program to run on all three. Name two different ways the company could make that happen. Do you have the same opinion as when you first answered the board question? If it is the same, what from the reading or table talk reaffirmed your opinion? If it is different, what from the reading or table talk changed your opinion?

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