Introduction to Expressions and Output

Welcome to one of the most exciting parts of learning Java! In our previous chapters, we looked at how to store data in variables. Now, it is time to actually do something with that data. Think of expressions as the "sentences" of math and logic that tell the computer how to calculate values, and output as the computer’s way of speaking back to you. Whether you are building a calculator or a high-end video game, these are the fundamental building blocks you will use every single day.

Note: This chapter focuses on Unit 1.3. For information on how to change a variable's type, see the chapter on Casting (1.5), or for shortcuts like +=, see Compound Assignment Operators (1.6).

1. Arithmetic Expressions

An expression is a combination of literals (like \( 5 \)), variables (like \( x \)), and operators (like \( + \)) that the Python evaluator turns into a single value. Java uses five main arithmetic operators:

  • Addition: \( + \)
  • Subtraction: \( - \)
  • Multiplication: \( * \)
  • Division: \( / \)
  • Modulo (Remainder): \( % \)

Integer vs. Double Division

This is a very common "gotcha" on the AP Exam! Java looks at the types of numbers you are using to decide how to divide them:

  • Integer Division: If you divide two integers, Java throws away the remainder (it doesn't round; it truncates).
    Example: \( 7 / 4 \) results in \( 1 \).
  • Double Division: If at least one of the numbers is a double (a decimal), Java gives you a decimal result.
    Example: \( 7.0 / 4 \) results in \( 1.75 \).

Quick Tip: Don't worry if this seems tricky! Just remember: Two ints make an int; if a double is invited to the party, the result is a double.

The Modulo Operator \( ( \% ) \)

The modulo operator \( \% \) returns the remainder of a division problem. It is incredibly useful for checking if a number is even or odd!

  • \( 10 \% 3 = 1 \) (Because 3 goes into 10 three times with 1 left over).
  • \( 20 \% 5 = 0 \) (Because 5 goes into 20 perfectly).
  • \( 1 \% 2 = 1 \) (Because 2 goes into 1 zero times, leaving 1 as the remainder).

Key Takeaway: Use \( / \) for the quotient (how many times it fits) and \( % \) for the leftover remainder.

2. Operator Precedence

Java follows a specific order of operations, very similar to the "PEMDAS" you learned in math class. When Java sees a long string of math, it evaluates it in this order:

  1. Parentheses: \( ( ) \) always come first.
  2. Multiplicative: Multiplication \( * \), Division \( / \), and Modulo \( % \) are handled next, from left to right.
  3. Additive: Addition \( + \) and Subtraction \( - \) are handled last, from left to right.

Example Walkthrough:
Calculate: \( 10 + 4 * 2 \% 3 \)
1. Multiplication first: \( 4 * 2 = 8 \). Expression is now \( 10 + 8 \% 3 \).
2. Modulo next: \( 8 \% 3 = 2 \). Expression is now \( 10 + 2 \).
3. Addition last: \( 10 + 2 = 12 \).

Did you know? If you have two operators of the same level (like multiplication and division), Java simply works from left to right, just like reading a book!

3. String Concatenation

The plus sign \( + \) has a special superpower in Java. When it is used with a String (text), it doesn't add numbers; it concatenates (links) them together.

  • "Java" + "Rocks" results in "JavaRocks".
  • "Score: " + 10 results in "Score: 10".

Common Trap: Order matters! Java evaluates from left to right.
\( 5 + 5 + "!" \) results in "10!" (It adds the numbers first).
\( "!" + 5 + 5 \) results in "!55" (Once it sees a String, it treats everything after it as text).

4. Output: print vs. println

To show the results of your expressions on the screen, you use System.out. There are two main methods you need to know:

  • System.out.print(): Prints the text but keeps the cursor on the same line. Anything printed next will appear right after it.
  • System.out.println(): Prints the text and then moves the cursor to a new line (the "ln" stands for "line").

Example:
System.out.print("Hello ");
System.out.println("World");
System.out.print("Java");
Output:
Hello World
Java

Quick Review Box:
  • Integers: Whole numbers only. \( 5 / 2 = 2 \).
  • Modulo \( ( \% ) \): Gives the remainder. \( 5 \% 2 = 1 \).
  • Concatenation: Using \( + \) to join text.
  • println: Prints and moves to the next line.

Common Mistakes to Avoid

  1. Integer Division Loss: Forgetting that \( 1 / 2 \) is \( 0 \) in Java. If you need \( 0.5 \), at least one number must be written as \( 1.0 \) or \( 2.0 \).
  2. Missing Quotes: Forgetting that text must be inside double quotes "like this". Numbers and variables do not use quotes.
  3. Division by Zero: In Java, trying to divide an integer by zero (e.g., \( 5 / 0 \)) will cause your program to crash with an error!

Key Takeaway: Master the difference between \( / \) and \( % \), and always keep an eye on whether you are working with int or double values. You've got this!