π1.3: Expressions & Output
Table of Contents
π This page is a condensed version of CSAwesome Topic 1.3
Output (Printing)
π¨οΈ Java has two different methods to display (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. "Hi there!"
is called a string literal, which is zero to many characters enclosed in double quotes.
Type these lines in your program, run them, and note the output. Then modify them so the !
prints on the same line as Hi there
while keeping all three statements.
System.out.print("Hi ");
System.out.println("there");
System.out.print("!");
Escape Sequences
What if you wanted to print out a double quote "
character? Since the double quote is a special character in Java, you need to put a backslash before it to βescapeβ it. This is called a backslash escape sequence. The same rule applies to printing a backslash itself, and \n
is a special escape sequence for printing a newline.
Type this line and run it to see escape sequences in action.
String message = "Here is a backslash quote \" and a backslashed backslash (\\) "
+ "Backslash n \n prints out a new line.";
System.out.println(message);
Expressions and Operators
Arithmetic expressions consist of numeric values, variables, and arithmetic operators that evaluate to a single numerical value. For example, 2 + 3
evaluates to 5.
Java uses:
+
(addition)-
(subtraction)*
(multiplication)/
(division)
TRUNCATION: When Java sees you doing integer division (or any operation with integers) it assumes you want an int
result too, so it throws away anything after the decimal point in the answer. This is called truncating. If you need a double
answer, you should make at least one of the values in the expression a double
, like 2.0
.
Type and run each line of code below:
System.out.println(2 + 3);
System.out.println(2 - 3);
System.out.println(2 * 3);
System.out.println(2 / 3);
Notice what happened with 2 / 3
. Change it so it prints the decimal result by making at least one number a double (e.g., 2.0 / 3
).
Mathematical errors sometimes lead to runtime errors in code.
For example, when the Hubble Space Telescope was launched to space in 1990, a math 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 as seen from Earth.
Fix the logic error so it correctly converts centimeters to inches (1 inch = 2.54 cm
).
System.out.print("100 centimeters in inches is: ");
System.out.println(100 * 2.54);
Compound Expressions
Compound expressions use multiple operators.
Java follows operator precedence (PEMDAS): *
, /
, and %
compute before +
and -
, unless parentheses ( )
override it.
Predict the output, then run:
System.out.println(2 + 3 * 2);
System.out.println((2 + 3) * 2);
System.out.println(2 + (3 * 2));
The Remainder %
Operator
The %
operator, sometimes called βmoduloβ or βmodulusβ, returns the remainder after using truncating integer division.
The left example shows
2 % 3
returns2
and the right example shows shows5 % 2
returns1
. 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.
Predict the output, then run:
System.out.println(11 % 10);
System.out.println(3 % 4);
System.out.println(8 % 2);
System.out.println(9 % 2);
Note: If
x
is smaller thany
,x % y
is alwaysx
.
Summary
- (AP 1.3.A.1)
System.out.print
andSystem.out.println
display output. - (AP 1.3.B.1) A literal is a fixed value in code.
- (AP 1.3.B.2) A string literal is text in double quotes.
- (AP 1.3.B.3) Escape sequences like
\"
,\\
, and\n
have special meaning in strings. - (AP 1.3.C.1) Arithmetic expressions use numbers, variables, and operators.
- (AP 1.3.C.2) Operators:
+
,-
,*
,/
,%
. - (AP 1.3.C.3) Integer division truncates; use a double for decimal results.
- (AP 1.3.C.4)
%
gives the remainder. - (AP 1.3.C.5) Compound expressions follow operator precedence.
- (AP 1.3.C.6) Integer division by zero causes an
ArithmeticException
.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.