Source: Princeton Review AP CS A Prep, 8th Edition
Tags: Java expressions, operator precedence, integer division, modulus, type casting, double, int, String methods, substring, compareTo, equals, escape sequences, boolean logic, De Morgan's Law, short-circuit evaluation, AP Computer Science A
Difficulty: Foundational Prerequisites: Basic familiarity with what Java is and how to read simple code.
These topics form the absolute bedrock of AP Computer Science A. Every question on the exam, from the simplest multiple choice to the most involved free response, assumes you can evaluate expressions, work with types, manipulate strings, and reason about boolean conditions. If you are coming in cold, start here. If you have been coding for a while but keep tripping on integer division or compareTo direction, this is still worth a careful read. Units 1 through 3 of the College Board curriculum map directly onto this material.
Java's arithmetic operators follow standard precedence, but integer division truncates (no rounding). Strings are objects compared with .equals() and .compareTo(), never == for content. Boolean expressions can be simplified using De Morgan's Law, and the exam loves testing whether you can trace through nested if / else if / else chains without losing track of which branch executes.
Integer division (truncation)
When both operands are int, Java discards the decimal portion entirely. 7 / 2 yields 3, not 3.5. There is no rounding. In simple terms, Java chops off everything after the decimal point.
Modulus operator (%)
Returns the remainder after integer division. 7 % 2 yields 1. Think of it as "what's left over."
Type casting
Forcing a value from one type to another. (double) 5 produces 5.0; (int) 3.9 produces 3. Casting to int truncates, same as integer division.
Operator precedence
Multiplication (*), division (/), and modulus (%) evaluate before addition (+) and subtraction (-). Parentheses override everything. Same-level operators evaluate left to right.
String immutability
Strings in Java cannot be changed after creation. Methods like toUpperCase() return a new String; they do not modify the original. If you write s.toUpperCase(); without assigning the result, s stays exactly as it was.
substring(int from, int to)
Returns the portion of the string starting at index from up to but not including index to. Indices are zero-based.
substring(int from)
Returns everything from index from to the end of the string. Equivalent to substring(from, length()).
indexOf(String str)
Returns the index of the first occurrence of str within the string, or -1 if not found.
compareTo(String other)
Returns a negative value if this comes before other lexicographically, zero if they are equal, and a positive value if this comes after. Think of it as subtraction: "cat".compareTo("dog") is negative because 'c' < 'd'.
equals(String other)
Returns true if the two strings contain exactly the same sequence of characters. This is the correct way to compare string content in Java.
== vs .equals() for objects
== checks whether two references point to the same object in memory. .equals() checks whether the contents are the same. For strings (and objects generally), use .equals() for content comparison.
Escape sequences
Special character combinations in Java strings: \" for a literal double quote, \\ for a literal backslash, \n for a newline, \t for a tab.
De Morgan's Law
!(A && B) is equivalent to !A || !B. !(A || B) is equivalent to !A && !B. The exam tests this regularly and expects you to apply it fluently.
Short-circuit evaluation
Java stops evaluating a boolean expression as soon as the outcome is determined. For &&, if the left side is false, the right side is never checked. For ||, if the left side is true, the right side is never checked.
When both operands are int, the result is int. This is the source of most integer-division mistakes.
When at least one operand is double, the result is double.
Operator precedence: *, /, % happen before +, -. Left to right within the same level.
The expression 4 + 6 % 12 / 4 evaluates as: 6 % 12 = 6, then 6 / 4 = 1 (integer division), then 4 + 1 = 5.
(double)(2 / 10) casts the result of 2 / 10, which is already 0, giving 0.0. The cast happens too late.
(double) 2 / 10 casts 2 to 2.0 first, then divides 2.0 / 10, giving 0.2. Order of casting matters.
2.0 / 10 also gives 0.2 because one operand is already a double.
Math.sqrt(4) / Math.sqrt(100) returns 2.0 / 10.0 = 0.2, since Math.sqrt returns double.
toUpperCase() and toLowerCase() return new strings. They do not mutate the original.
Passing strings to a method does not allow that method to reassign the caller's variables. Java is pass-by-value for references: the method gets a copy of the reference, not the original variable.
compareTo is used for lexicographic ordering. Negative means the calling string comes first; positive means it comes after.
To print "Friends" (with the quotes visible), you need \" around the word: System.out.print("\"Friends\"");
\\ prints a single backslash. // is not an escape sequence; it starts a comment inside code but prints literally in a string.
if / else if / else chains are mutually exclusive: once one branch executes, the rest are skipped.
Nested if statements are not mutually exclusive with the outer if unless explicitly linked with else.
When tracing through conditionals, write down the current values of every variable and check each condition in sequence. Do not skip ahead.
De Morgan's Law conversions are tested both directions. You should be able to negate a compound condition and simplify it.
Type casting and integer division appear in any system that deals with money (calculating pennies vs. pounds), pixel coordinates, or resource allocation. Boolean logic underpins every search filter, access-control rule, and input-validation check you will ever write.
Students often assume 5 / 2 returns 2.5 in Java. It does not. Both operands are int, so the result is 2.
Students frequently use == to compare string content. This compares references, not content, and can produce unexpected false results even when the strings look identical.
Calling s.toUpperCase() without assigning the result (e.g., s = s.toUpperCase()) is a common mistake. The original string s remains unchanged.
When negating boolean expressions, students often forget to flip && to || (and vice versa). De Morgan's Law requires both the operators and the individual conditions to be negated.
⚠️ Integer division and modulus appear on nearly every AP CS A exam. Trace carefully and remember: no rounding, just truncation.
⚠️ == vs .equals() for strings and objects is one of the most commonly tested concepts in the multiple-choice section.
⚠️ De Morgan's Law is tested directly (identify equivalent expressions) and indirectly (trace through negated conditions in if statements).
⚠️ Escape sequences (\", \\) show up in straightforward print-output questions. Free marks if you know them.
True or false: 9 / 2 evaluates to 4.5 in Java when both operands are int.
Fill in the blank: To compare the contents of two String objects, use the __________ method.
True or false: !(x > 5 && y < 3) is equivalent to x <= 5 || y >= 3.
Fill in the blank: "hello".compareTo("help") returns a __________ value (positive, negative, or zero).
True or false: String s = "hi"; s.toUpperCase(); changes s to "HI".
Q: Evaluate the expression 4 + 6 % 12 / 4.
A: 6 % 12 = 6, then 6 / 4 = 1 (integer division), then 4 + 1 = 5. The answer is 5.
Q: Which of the following does NOT evaluate to 0.2? (A) (1.0 2) / (1.0 10) (B) 2.0 / 10 (C) (double) 2 / 10 (D) (double)(2 / 10) (E) Math.sqrt(4) / Math.sqrt(100)
A: (D). 2 / 10 is integer division yielding 0, then casting to double gives 0.0, not 0.2.
Q: What does the following code print? String animal1 = "elephant"; String animal2 = "lion"; swap(animal1, animal2); animal1.toUpperCase(); animal2.toLowerCase(); System.out.println(animal1 + " " + animal2); where swap reassigns local parameters only.
A: elephant lion. The swap method only reassigns its local copies, so the originals are unchanged. toUpperCase() and toLowerCase() return new strings but the results are not assigned back, so animal1 and animal2 remain as they were.
Q: Given int temp = 90; boolean cloudy = false;, which evaluates to true? (I) temp >= 90 && !cloudy (II) !(temp > 90 || cloudy) (III) !(temp > 90 && !cloudy)
A: I only. (I) is true && true = true. (II) is !(false || false) = true... wait, let's retrace: temp > 90 is false (90 is not > 90), cloudy is false, so false || false = false, and !false = true. Actually (II) is also true. Let's check (III): temp > 90 is false, !cloudy is true, so false && true = false, and !false = true. All three are true. The answer is (E), all of the above evaluate to true. (The subtlety is that >= vs > matters: temp >= 90 is true but temp > 90 is false.)
Q: Given word1 = "frog", word2 = "dog", word3 = "cat", trace through a compareTo-based sorting block and determine the printed output.
A: "frog".compareTo("dog") is positive (f > d), so we enter the else branch. "frog".compareTo("dog") > 0 is true, then "dog".compareTo("cat") < 0 is false (d > c), so we go to else and print word1 + " " + word3 + " " + word2 = frog cat dog. The answer is frog cat dog which is not in ascending order, revealing a bug in the sorting logic.
These fundamentals connect directly to iteration (Unit 4), because every loop condition is a boolean expression, and loop bodies frequently involve arithmetic and string manipulation. They also feed into arrays and ArrayLists (Units 6-7), where indexing errors often stem from confusing integer division or off-by-one mistakes in substring. Understanding == vs .equals() becomes critical again when working with object references in inheritance (Unit 9).
int, double, type casting, widening, narrowing, truncation, integer division, modulus, mod, remainder, operator precedence, order of operations, PEMDAS in Java, String class, substring, indexOf, compareTo, equals, == vs equals, escape character, backslash, double quote, newline, boolean, logical operators, AND, OR, NOT, De Morgan's Law, DeMorgan, short-circuit evaluation, if else, nested conditionals, AP CS A Unit 1, AP CS A Unit 2, AP CS A Unit 3