Before the bell
You are responsible for all of this before the end of class.
- The discussion form in the I Do deck. All three sections. If you are here, this should be done.
- Your classwork. This form on the right. Section 1 before the exit ticket pops up; Section 2 past the floor.
- Today's Bench Basics activity: Join the Strings. It opens later in class and lives on slide 4 of this deck.
- The exit ticket. It opens 15 minutes before the bell and lives on the last slide of this deck.
The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.
Join the Strings
This reading explains how Java joins pieces of text with the `+` symbol and how it counts the characters in a `String`.
Adding and Joining
In Java, the `+` symbol has two jobs. Between two numbers, it adds them. When either side is a `String`, it joins the two sides into one longer `String`, with the characters on the left coming first. Java works through a row of `+` signs from left to right, so the order of the pieces matters. The program below shows both jobs.
1public class Main { 2 public static void main(String[] args) { 3 String street = "Esplanade"; 4 String word = "Avenue"; 5 System.out.println(street + " " + word); 6 System.out.println("5" + 3); 7 System.out.println(5 + 3); 8 System.out.println("Total: " + 5 + 3); 9 System.out.println(5 + 3 + " total"); 10 System.out.println(street.length()); 11 } 12}
Esplanade Avenue 53 8 Total: 53 8 total 9
Line 3 stores `"Esplanade"` in the box `street`, and line 4 stores `"Avenue"` in the box `word`. Line 5 joins three Strings, `street`, `" "` and `word`, so it prints Esplanade Avenue. Line 6 joins instead of adding, because `"5"` sits in quotes and is a `String`, so it prints 53. Line 7 adds, because 5 and 3 are both numbers, so it prints 8. Line 8 starts with a `String`, so `"Total: " + 5` makes `"Total: 5"` first, and then `+ 3` joins a 3 onto the end to make `"Total: 53"`. Line 9 starts with two numbers, so `5 + 3` adds to 8 first, and then `+ " total"` joins to make `"8 total"`. Line 10 prints 9, the number of characters in `"Esplanade"`.
Counting Characters and the Java Quick Reference
The method `length()` gives the number of characters in a `String`, and a space counts as a character. For example, `"New Orleans"` has 11 characters, because the space between the two words counts. The AP Computer Science A exam gives each student a sheet called the Java Quick Reference, which lists the Java methods that may appear on the exam. Its row for this method reads `int length()`, and the explanation beside it says that it "Returns the number of characters in a String object." The word `int` at the front means the answer is a whole number. Today's bench is the first in Bench Basics with a Reference button, which opens a Java Quick Reference panel below the job. The panel lists the sheet's String, Integer, Double, Math and ArrayList methods in the class's own words, and it links to the official sheet.
The bench checks two points from this reading. The first is order, because `"Total: " + 5 + 3` joins from left to right, while `5 + 3 + " total"` adds first and then joins. The second is the space, because `" "` is a `String` of one space, which is why the first line of output has a space in it.
Sources: Oracle, The Java Language Specification, SE 21, 15.18 (n.d.); Oracle, Class String (n.d.); College Board, AP Computer Science A Java Quick Reference (2025); course JDK, Main.java (2026).
Syntax, Error Messages and Debugging
This section explains the rules a Java program must follow, how to read the message the compiler prints when a rule is broken, and how a programmer fixes the problem.
The Rules for Writing Code
Syntax is the set of rules a programmer follows when writing code. In Java, a statement such as a line that prints ends with a semicolon. Every opening brace, `{`, needs a matching closing brace, `}`, because braces mark where a block of code begins and ends. Text sits inside a pair of double quotes, as in `"Canal Street"`. Java also treats a capital letter and a small letter as different characters, so `System` must start with a capital S. A reader of English can still follow a sentence with a missing period, but the compiler will not compile a program with a missing semicolon. Syntax is only about how code is written, so a program can follow every rule and still print the wrong answer.
Reading an Error Message
As an earlier lesson explained, the javac compiler turns source code into bytecode in a .class file. It checks that the program follows the syntax rules. When a rule is broken, javac makes no .class file and prints an error message instead, which is a report that names the file, the line and the problem the compiler noticed. The first line of a javac error message has three parts, separated by colons: the file, the line number and what went wrong. Under that line, javac prints the line of code it was reading and puts a caret, `^`, under the spot where it noticed the problem. Figure 1 shows a real message, `Stops.java:4: error: ';' expected`, with each part labeled. The file is Stops.java, the line is 4, and javac expected a semicolon at the end of `System.out.println(stops)`. An error message names the file, the line and what the compiler expected, but it does not always name the fix.
When the Line Is Not the Mistake
The line in an error message is where the compiler noticed the problem, which is not always where the mistake is. Each program below fails on purpose, and its first line is a comment. The compiler skips everything from `//` to the end of that line, but the comment still counts as line 1. The next program is missing the closing brace for the class.
1// This does not compile. 2public class Stops { 3 public static void main(String[] args) { 4 System.out.println("Next stop: Canal Street"); 5 }
Stops.java:5: error: reached end of file while parsing
}
^
1 error
The message points at line 5, which is the last line of the file. The mistake is a brace that was never written, and javac noticed only when it ran out of code to read.
Some messages name what the compiler saw instead of the fix. The program below writes `system` with a small s.
1// This does not compile. 2public class Stops { 3 public static void main(String[] args) { 4 system.out.println("Next stop: Canal Street"); 5 } 6}
Stops.java:4: error: package system does not exist
system.out.println("Next stop: Canal Street");
^
1 error
Because a small s and a capital S are different characters, javac reads `system` as a different name from `System`, and it reports that no package called system exists. The message never says to use a capital S, so the programmer works out the fix from what the message describes.
Finding and Fixing a Problem
Debugging is finding the cause of a problem, fixing it and running the code again to check the fix. An OpenStax computer science textbook advises fixing only the first error message and then compiling again, because one syntax error can cause false alarms later in the code. For example, a programmer reads the message in Figure 1, opens Stops.java at line 4, adds the semicolon and compiles again. If a new message appears after that fix, the debugging is not finished yet. Changing lines at random until the message goes away is not debugging, because it does not find the cause or check the fix.
Code with no syntax errors can still be wrong. A program that compiles and prints 4 when it should print 5 has a mistake the compiler cannot see, because every rule was followed. A test with an expected output catches this kind of mistake, as the lesson on testing a change showed. The programmer runs the program, compares what it prints with what it should print, and looks for the cause when the two differ. Debugging covers both kinds of problems, the ones the compiler reports and the ones that only a test shows.
Summary
Syntax is the set of rules a programmer follows when writing Java code. When a rule is broken, javac makes no .class file and prints an error message that names the file, the line and what it expected. Debugging means finding the cause, fixing one thing and then compiling or testing again to check the fix.
Sources: Oracle, Common Problems (and Their Solutions) (n.d.); Oracle, About the Java Technology (n.d.); Oracle, The javac Command (n.d.); Oracle, The Java Language Specification, SE 21 (n.d.); Franchitti, Introduction to Computer Science (2024); course JDK, Stops.java (2026).
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 syntax in Java?
- The answer a program prints when it runs
- The set of rules a programmer follows when writing code
- The list of mistakes the compiler finds and fixes
- The order in which the lines of a program run
MC 2. An error message begins Parade.java:12: error: ';' expected. Which part is the line number?
- Parade.java
- error
- ';'
- 12
MC 3. A program is missing the closing brace for its class. Why does the error message point at the last line of the file?
- The mistake is always on the last line of a file.
- javac reads a program from the bottom up.
- javac noticed the problem only when it ran out of code to read.
- The last line has a typing error that javac found.
MC 4. Which problem can javac not report, so that only a test with an expected output catches it?
- A statement with no semicolon at the end
- A program that adds two numbers it should subtract and prints the wrong total
- The word System written with a small s
- A class with no closing brace at the end of the file
MC 5. What do these two lines print?
1System.out.println("6" + 4); 2System.out.println(6 + 4);
- 10, then 10
- 64, then 64
- 64, then 10
- 10, then 64
MC 6. What does this line print?
1System.out.println(2 + 4 + " blocks");
- 24 blocks
- 6 blocks
- 2 + 4 blocks
- 6blocks
MC 7. What does this line print?
1System.out.println("blocks: " + 2 + 4);
- blocks: 6
- blocks: 2 4
- blocks: 24
- blocks:24
MC 8. What does this code print?
1String place = "Jackson Square"; 2System.out.println(place.length());
- 13
- 14
- 16
- 2
MC 9. A student wants to know how many characters are in a String. Which row of the Java Quick Reference answers that question?
- int length()
- int indexOf(String str)
- String substring(int from)
- boolean equals(Object other)
MC 10. What does this code print?
1String a = "Bayou"; 2String b = a + " " + a; 3System.out.println(b.length());
- 10
- 5
- Bayou Bayou
- 11
CR 1. Write what each of the four println lines below prints, in order. Give a one-sentence reason for each line.
1String team = "Pelicans"; 2System.out.println("Go " + team); 3System.out.println(team + 2 + 6); 4System.out.println(2 + 6 + team); 5System.out.println(team.length());
Write your answer in your lab notebook, labeled CR 1.
CR 2. Explain in two sentences why System.out.println("Total: " + 5 + 3); prints Total: 53 and not Total: 8.
Write your answer in your lab notebook, labeled CR 2.
The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.
CR 3. The program below does not compile. javac prints the message Ferry.java:7: error: reached end of file while parsing, with the caret just after the } on line 7. Where does the missing brace belong? Explain why the message names line 7 and not the line where the brace is missing.
1// This does not compile. 2public class Ferry { 3 public static void main(String[] args) { 4 String dock = "Algiers Point"; 5 System.out.println("Ferry to " + dock); 6 System.out.println(dock.length()); 7}
Write your answer in your lab notebook, labeled CR 3.
CR 4. A van carries 3 adults and 2 kids, so the program below should print Riders: 5. It compiles and runs, but it prints Riders: 6. Describe how you would find the mistake, what you would change, and how you would check your fix.
1int adults = 3; 2int kids = 2; 3int total = adults + adults; 4System.out.println("Riders: " + total);
Write your answer in your lab notebook, labeled CR 4.
Join the Strings
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.
The classwork form is not linked yet. Use Station View and write your answers in your lab notebook with the item labels.
This opens during your class period.