Integer Arithmetic and Output

Part of: Primitive Types

Arithmetic Operators. Java provides five core arithmetic operators for numbers: - + addition - - subtraction - multiplication - / division - % modulus (remainder) When both operands are int, the result is an int. This lesson focuses on integer math with +, -, and ; division is covered next. Printing Output. Java has two main ways to send text to the console: - System.out.println(x) prints x then moves to a new line . - System.out.print(x) prints x and stays on the same line . Notice how print lets you build a line piece by piece, while println finishes the line. String Concatenation. The + operator does double duty. With numbers it adds; with a String on either side it concatenates (joins text): Here total is converted to text and glued after "Total = ". Be careful: "x" + 2 + 3 produces x23 (left to right), but 2 + 3 + "x" produces 5x because the numeric addition happens first. What usually goes wrong. What you see What caused it How to fix it --- --- --- Sum: 73 instead of Sum: 10 The left + ran first, so 7 joined the text and 3 joined after it Group the addition in parentheses so it happens before the join The label and the number land on separate lines You printed the label with

Challenge: Labeled Sum