1.3 - Expressions and Output
System.out.print and System.out.println both display info to the screen. The only difference: println moves the cursor to a new line afterward, print does not.System.out.print("Hello"); System.out.println(" World"); System.out.println("This is on a new line");
Output:
Hello World This is on a new line
One more trace, since these show up a lot. Watch where each
println ends the line:System.out.print("A"); System.out.println("B"); System.out.print("C"); System.out.print("D"); System.out.println("E"); System.out.println("F");
Output:
AB CDE F
A literal is just the code representation of a fixed value, like typing
5 or "cat" directly into your code.Each type has its own kind of literal:
intliterals are whole numbers with no decimal point, like42or1000.
doubleliterals have a decimal point, like3.14or5.0. So5is anint, but5.0is adouble.
booleanliterals aretrueandfalse.
A string literal is a sequence of characters wrapped in double quotes, like
"hello".The quotes just mark where the string starts and ends, they aren’t part of it, so
System.out.println("cat"); prints cat. Also, "5" is a String, not an int. "5" + 1 gives "51" (joining strings, see 1.15), not 6.Escape sequences are special character combos starting with a backslash that mean something specific in Java. The ones you need to know:
\"for a double quote
\\for a backslash
\nfor a new line
You need these because some characters can’t go into a string directly. A plain double quote would end the string early, and a single backslash tells Java an escape sequence is starting.
System.out.println("She said \"hi\" to me."); System.out.println("C:\\Users\\dove"); System.out.println("Line one\nLine two");
Output:
She said "hi" to me. C:\Users\dove Line one Line two
Arithmetic Expressions
Arithmetic expressions combine numeric values, variables, and operators, and they can be either
int type or double type.The operators you need to know:
+, -, *, /, and % (remainder).- If both values in the expression are
int, the result is anint.
- If at least one value is a
double, the result is adouble.
int a = 7 / 2; // a = 3 (integer division, drops the decimal) double b = 7.0 / 2; // b = 3.5 int c = 7 % 2; // c = 1 (remainder)
When dividing two
int values, you only get the integer part of the quotient, the decimal gets chopped off, not rounded. When at least one value is a double, you get the actual decimal quotient.This trips a lot of people up: the type of the variable you store the answer in doesn’t matter. Java only looks at the two values being divided.
double avg = 7 / 2; // 3.0, not 3.5: 7 / 2 is int division (3), then 3 is stored as 3.0 double avg2 = 7 / 2.0; // 3.5 int small = 1 / 2; // 0
The
% operator gives you the remainder when dividing one number by another. Just keep it to positive numbers, using negatives here is outside the scope of the course.To find
a % b, divide a by b and keep what’s left over. When a is smaller than b, b doesn’t fit into a at all, so the answer is just a.int r1 = 17 % 5; // 2, since 17 = 3 * 5 + 2 int r2 = 20 % 5; // 0, 5 divides 20 evenly int r3 = 3 % 5; // 3, a is smaller than b int r4 = 0 % 4; // 0
For positive whole numbers, two patterns show up all the time:
n % 2 is 0 when n is even and 1 when n is odd, and n % 10 gives the last digit of n (472 % 10 is 2). Its partner, n / 10, chops off the last digit (472 / 10 is 47).Operator Precedence
At compile time, Java groups values with operators according to precedence. Multiplication, division, and remainder all happen before addition and subtraction. Parentheses can override this. If two operators are tied in precedence, Java works left to right.
int result = 2 + 3 * 4; // 14, not 20 int forced = (2 + 3) * 4; // 20, parentheses change the grouping
When operators are tied, go left to right. This matters a lot with
int division, since the remainder gets thrown away at each step:int a = 10 / 4 * 2; // (10 / 4) * 2 = 2 * 2 = 4, not 10 / 8 int b = 2 * 7 % 4; // (2 * 7) % 4 = 14 % 4 = 2 int c = 7 + 5 % 3 * 2; // 7 + ((5 % 3) * 2) = 7 + (2 * 2) = 11 double d = 5 / 2 * 2.0; // (5 / 2) * 2.0 = 2 * 2.0 = 4.0 double e = 5 / 2.0 * 2; // (5 / 2.0) * 2 = 2.5 * 2 = 5.0
One important gotcha: dividing an integer by integer zero throws an
ArithmeticException. Dividing a double by zero is outside the scope of this course, so you don’t need to worry about that case.int oops = 10 / 0; // ArithmeticException at runtime