Unit 1.3: Expressions and Output
Literals
A literal is the source code representation of a fixed value. Literals can be assigned as a value to a primitive data type.
- A literal is a "hard-coded" value in your source code, such as the number
5or the text"Hello".
Task: Using literals to initialize variables.
int year = 2025; // 2025 is an integer literal
double pi = 3.14159; // 3.14159 is a double literal
boolean isJavaFun = true; // true is a boolean literal
char capitalC = 'C'; // 'C' is a char literal.
String Literals
A string literal is a sequence of characters enclosed in double quotes.
Task: Defining a String literal.
String message = "This is a string literal.";
Escape Sequences
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.
- Because certain characters (like
"or\) have special roles in Java syntax, you must "escape" them to use them as literal text within a string.
Escape sequences used in this course include:
- Double Quote (
\"): Used when you want to include a literal double quote inside a string without ending the string.- Example:
"She said, \"Hello!\""→ Output:She said, "Hello!"
- Example:
- Backslash (
\\): Used when you want to include a literal backslash. Since the backslash is the escape character, you need to use two of them.- Example:
"C:\\Users\\Desktop"→ Output:C:\Users\Desktop
- Example:
- Newline (
\n): Used to move the cursor to the next line within a single string.- Example:
"First line\nSecond line"→ Output:First line Second line
- Example:
Task: Using escape sequences in an output statement.
System.out.println("She said, \"Hello!\"\nThis is a new line with a backslash: \\");
Output:
She said, "Hello!"
This is a new line with a backslash: \
The Arithmetic Operators
An arithmetic expression is a combination of numeric values (literals), variables, and operators that results in a single numeric value. We will focus on expressions of type int and double.
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:
Assume integer variable a = 10 and variable b = 20, then:
| Operator | Description | Example |
| + | Addition - Adds values on either side of the operator | a + b will give 30 |
| - | Subtraction - Subtracts the right-hand operand from the left-hand operand | a - b will give -10 |
| * | Multiplication - Multiplies values on either side of the operator | a * b will give 200 |
| / | Division - Divides the left-hand operand by the right-hand operand | b / a will give 2 |
| % | Modulus - Divides the left-hand operand by the right-hand operand and returns the remainder | b % a will give 0 |
Operation Result Types
When performing arithmetic, if at least one operand is a double, the result is a double. Otherwise, if both are integer types (int, byte, short), the result is an int.
Integer Division
Integer division (int / int) truncates (cuts off) the decimal portion of the result.
- When both operands in a division are integers, Java performs integer math. The fractional part is not rounded; it is simply removed.
Task: Observing the truncation effect of integer division.
int result = 10 / 3; // result is 3
Remainder Operator
The remainder operator (%) computes the remainder of a division. It is useful for determining if a number is even or odd.
- The
%operator provides the remainder after division. For example,10 % 3is1because 3 goes into 10 three times with 1 left over.
Task: Using the remainder operator to check for even numbers.
int remainder = 10 % 3; // remainder is 1
boolean isEven = (10 % 2 == 0); // isEven is true
Operator Precedence
Java follows the standard order of operations (PEMDAS). *, /, and % are performed before + and -. Use parentheses () to control the order.
Task: Applying operator precedence rules.
int result = 5 + 2 * 3; // Multiplication first: 5 + 6 = 11
Examples of Precedence and Associativity
When operators have the same precedence (like * and /), Java evaluates them from left to right (this is called associativity).
| Expression | Evaluation Step-by-Step | Result |
|---|---|---|
| 10 + 5 * 2 | 10 + (5 * 2) → 10 + 10 | 20 |
| (10 + 5) * 2 | (15) * 2 | 30 |
| 10 / 2 + 3 | (10 / 2) + 3 → 5 + 3 | 8 |
| 10 + 10 % 3 | 10 + (10 % 3) → 10 + 1 | 11 |
| 20 - 5 - 2 | (20 - 5) - 2 → 15 - 2 | 13 |
| 12 / 2 * 3 | (12 / 2) * 3 → 6 * 3 | 18 |
Division by Zero
Dividing an integer by zero results in an ArithmeticException at run-time.
Task: Identifying code that triggers an ArithmeticException.
int a = 10 / 0;
- The result of an integer division is an
int, and any remainder is truncated. The result of a division is adoubleonly if at least one of the operands is adouble. - Java uses the standard PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction) order when evaluating several operators in the same expression. When there are multiple instances of the same precedence, Java reads from left to right.
Values in Other Number Systems
EXCLUSION STATEMENT—Binary, octal, and hexadecimal number systems are outside the scope of the AP Computer Science A course and exam. You can skip the below content as appropriate.
All the examples above used the decimal (base 10) number system for initializing the integer values:
- Decimal: Base 10, whose digits consist of the numbers 0 through 9; this is the standard number system familiar to almost everyone.
However, Java also provides Hexadecimal and Octal number systems, as described below:
Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F. This system is mostly used by programmers and is beyond the scope of this book. You must start with
0xwhen you want to represent a Hexadecimal number.Octal: Base 8, whose digits consist of numbers from 0 to 7. You should start with
0when you want to assign an Octal number.Binary: Base 2, whose digits consist of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later). You start a binary representation number with
0b.
You will use the decimal system in this book. However, if you need to use another number system, the following example shows the correct syntax. The prefix 0x indicates hexadecimal and 0b indicates binary:
The number 11, in decimal
int decVal = 11;
The number 11, in hexadecimal
int hexVal = 0xB;
The number 11, in octal
int octVal = 013;
The number 11, in binary
int binVal = 0b1011;