Skip to main content
Logo image

Section 1.3 Expressions and Output

Subsection 1.3.1 Output

Java has two different methods to print output to the screen:
  • System.out.println(value) : prints the value followed by a new line (ln)
  • System.out.print(value) : prints the value without advancing to the next line
System.out.println("Hi there!"); prints out the characters between the first " and the second " followed by a new line. The "Hi there!" is called a string literal which is zero to many characters enclosed in starting and ending double quotes. A literal is the code representation of a fixed value, which can be a string or a numerical value.

Activity 1.3.1.

Run this code to see the output below it. How would you change it to print the ! on the same line as Hi there keeping all 3 print statements?
What if you wanted to print out a double quote ” character? Since the double quote ” is a special character with meaning in Java, we put in a backslash in front of the quote to signal that we want just the character. This is called a backslash escape sequence. And if you wanted to print out a backslash, you would have to backslash it too in order to escape its special meaning. Another useful backslashed character is backslash \n which will put in a newline.

Activity 1.3.2.

Here are the escape sequences that may be used in the AP course.

Subsection 1.3.2 Expressions and Operators

Arithmetic Expressions consist of numeric values, variables, and arithmetic operators that evaluate to a single numerical value. For example, the expression 2 + 3 evaluates to 5.
Java uses the standard mathematical operators for addition (+), subtraction (-), and division (/). The multiplication operator is written as *, as it is in most programming languages, since the character sets used until relatively recently didn’t have a character for a real multiplication sign, ×, and keyboards still don’t have a key for it or for ÷. (You may have noticed that + was also used to combine String and other values into new Strings. More on this later.)
Arithmetic expressions can be of type int or double. An arithmetic expression consisting only of int values will evaluate to an int value. An arithmetic expression that uses at least one double value will evaluate to a double value. This means that when you are doing division with two integers, you will get an integer result and the decimal part of the result will be thrown away. This is called truncating division. If you want a double result, you should make at least one of the values in the expression a double like 2.0.

Activity 1.3.3.

Run the code below to see all the operators in action. Do all of those operators do what you expected? What about 2 / 3? Isn’t it surprising that it prints 0? See the note above about truncating division with integers. Change the code to make it print the decimal part of the division too. You can do this by making at least one of the numbers a double like 2.0.
Math errors sometimes lead to runtime errors in code. For example, when the Hubble Space Telescope was launched to space in 1990, a math coding error in a formula caused it to point in the wrong direction! It missed its target stars by about half a degree which is about the width of the moon seen from Earth (https://scholar.lib.vt.edu/VA-news/ROA-Times/issues/1990/rt9005/900510/05100615.htm
 1 
https://scholar.lib.vt.edu/VA-news/ROA-Times/issues/1990/rt9005/900510/05100615.htm
). Thorough testing is the only way to make sure there are no logic errors that will cause runtime errors in your code. Try the following example that tries to convert centimeters to inches. Can you fix the runtime error?

Activity 1.3.4.

The following code is trying to convert centimeters to inches, but it has a math error. Run the code to see that there are no error messages, but it simply does the wrong calculation! Can you fix the logic error in the code? 1 inch = 2.54 cms.
Another runtime error that is possible in math expressions is dividing by zero which is undefined in mathematics. An attempt to divide an integer by the integer 0 will result in an ArithmeticException in Java, as we saw in the first lesson. Try it in one of the active codes to see what happens.

Subsection 1.3.3 Compound Expressions

Expressions can be combined into compound expressions with multiple operators. When compound expressions are evaluated, operator precedence rules are used, just like when we do math (remember PEMDAS?), so that multiplication *, division /, and remainder % are done before addition + and subtraction -. However, anything in parentheses is done first. It doesn’t hurt to put in extra parentheses if you are unsure as to what will be done first or just to make it more clear.

Activity 1.3.5.

In the example below, try to guess what it will print out and then run it to see if you are right. Remember to consider operator precedence. How do the parentheses change the precedence?

Subsection 1.3.4 The Remainder Operator

The operator % in Java is the remainder operator. Like the other arithmetic operators is takes two operands. Mathematically it returns the remainder after dividing the first number by the second, using truncating integer division. For instance, 5 % 2 evaluates to 1 since 2 goes into 5 two times with a remainder of 1.
While you may not have heard of remainder as an operator, think back to elementary school math. Remember when you first learned long division, before they taught you about decimals, how when you did a long division that didn’t divide evenly, you gave the answer as the number of even divisions and the remainder. That remainder is what is returned by this operator. In the figures below, the remainders are the same values that would be returned by 2 % 3 and 5 % 2.
Figure 1.3.1. Long division showing the integer result and the remainder
Sometimes people—including Professor Lewis in the next video—will call % the modulo, or mod, operator. That is not actually correct though the difference between remainder and modulo, which uses Euclidean division instead of truncating integer division, only matters when negative operands are involved and the signs of the operands differ. With positive operands, remainder and mod give the same results. Java does have a method Math.floorMod in the Math class if you need to use modulo instead of remainder, but % is all you need in the AP exam.
Here’s the video
 2 
https://www.youtube.com/watch?v=jp-T9lFISlI&ab_channel=colleenlewis
.

Activity 1.3.6.

In the example below, try to guess what it will print out and then run it to see if you are right.

Note 1.3.2.

The result of x % y when x is smaller than y is always x. The value y can’t go into x at all (goes in 0 times), since x is smaller than y, so the result is just x. So if you see 2 % 3 the result is 2.

Activity 1.3.7.

What is the result of 158 % 10?
  • 15
  • This would be the result of 158 divided by 10. % gives you the remainder.
  • 16
  • % gives you the remainder after the division.
  • 8
  • When you divide 158 by 10 you get a remainder of 8.

Activity 1.3.8.

What is the result of 3 % 8?
  • 3
  • 8 goes into 3 no times so the remainder is 3. The remainder of a smaller number divided by a larger number is always the smaller number!
  • 2
  • This would be the remainder if the question was 8 % 3 but here we are asking for the reminder after we divide 3 by 8.
  • 8
  • What is the remainder after you divide 3 by 8?

Subsection 1.3.5 Coding Challenge : Pay Calculator

Figure 1.3.3.
In this coding challenge, you can work in pairs to create a pay calculator using math expressions and operators.

Project 1.3.9.

Complete the following expressions for a pay calculator.

Subsection 1.3.6 Summary

  • (AP 1.3.A.1) System.out.print and System.out.println are Java output statements that display information on the computer screen. System.out.println moves the cursor to a new line after the information has been displayed, while System.out.print does not.
  • (AP 1.3.B.1) A literal is the code representation of a fixed value, which can be a string or a numerical value.
  • (AP 1.3.B.2) A string literal is a sequence of zero to many characters enclosed in starting and ending double quotes.
  • (AP 1.3.B.3) Escape sequences are special sequences of characters that can be included in a string. They start with a ` and have a special meaning in Java. Escape sequences used in this course include double quote `”, backslash `, and newline `n.
  • (AP 1.3.C.1) Arithmetic expressions, which consist of numeric values, variables, and operators, include expressions of type int and double.
  • (AP 1.3.C.2) The arithmetic operators consist of +, -, * , /, and % also known as addition, subtraction, multiplication, division, and remainder.
  • (AP 1.3.C.2) An arithmetic operation that uses two int values will evaluate to an int value. With integer division, any decimal part in the result will be thrown away.
  • (AP 1.3.C.2) An arithmetic operation that uses at least one double value will evaluate to a double value.
  • (AP 1.3.C.3) When dividing numeric values that are both int values, the result is only the integer portion of the quotient. Anything after the decimal point is thrown away. When dividing numeric values that use at least one double value, the result is the double quotient as expected.
  • (AP 1.3.C.4) The remainder (modulo) operator % is used to compute the remainder when one number is divided by another number.
  • (AP 1.3.C.5) Multiple operators can be used to combine expressions into compound expressions.
  • (AP 1.3.C.5) During evaluation, numeric values are associated with operators according to operator precedence to determine how they are grouped. *, /, % have precedence over + and -, unless parentheses are used to group those to be evaluated first. Operators with the same precedence are evaluated from left to right.
  • (AP 1.3.C.6) An attempt to divide an integer by zero will result in an ArithmeticException.

Subsection 1.3.7 AP Practice

Activity 1.3.10.

Consider the following code segment.
System.out.print("Java is ");
System.out.println("fun ");
System.out.print("and cool!");
What is printed as a result of executing the code segment?
  • Java is fun and cool!
    
  • Notice the println in line 2.
  • Java isfun
    and cool!
    
  • Notice the space after is in the first line.
  • Java is
    fun
    and cool!
    
  • Notice that the first line is a print, not println.
  • Java is fun
    and cool!
    
  • Correct! Looks like you paid attention to which lines used print and which ones used println.

Activity 1.3.11.

Consider the following code segment.
System.out.println(5 + 5 / 2 * 3 - 1);
What is printed when the code segment is executed?
  • 0.666666666666667
  • Don’t forget that division and multiplication will be done first due to operator precedence.
  • 9
  • Don’t forget that division and multiplication will be done first due to operator precedence.
  • 10
  • Yes, this is equivalent to (5 + ((5 / 2) * 3) - 1).
  • 11.5
  • Don’t forget that division and multiplication will be done first due to operator precedence, and that an int/int gives an int truncated result where everything to the right of the decimal point is dropped.
  • 14
  • Don’t forget that division and multiplication will be done first due to operator precedence.
You have attempted of activities on this page.