Primitives: every key term you need (+ practice quiz)
60 flashcard terms for AP Computer Science A Unit 1, written to match the course framework. Read them here, drill them as flashcards, or take the 45-question quiz. Free, no account needed.
When an int computation exceeds Integer.MAX_VALUE (2147483647) it wraps around to negative values silently. Integer.MAX_VALUE + 1 equals Integer.MIN_VALUE; Java throws no error.
Constants in the Integer class holding the smallest (-2147483648) and largest (2147483647) int values. Frequently used to initialize a running min or max before a search loop.
Compound Assignment Operators
+=, -=, *=, /=, %= combine an operation with assignment. x += 2.5 on an int x compiles because compound assignment includes an implicit cast back to the left-hand type.
Increment and Decrement
x++ and x-- add or subtract 1. The AP subset uses them only as standalone statements, so prefix versus postfix distinctions are not tested in expressions.
Round-off Error
doubles store binary approximations, so 0.1 + 0.2 is not exactly 0.3. Compare doubles with a tolerance, e.g. Math.abs(a - b) < 0.0001, never with ==.
Rounding a double to nearest int
For positive x, (int)(x + 0.5) rounds to nearest; for negative x use (int)(x - 0.5). Casting alone truncates toward zero, so 2.9 casts to 2.
Truncation vs Floor
(int) truncates toward zero, so (int)-2.7 is -2, while Math.floor(-2.7) is -3.0. They agree for positive values only.
Widening Conversion in Expressions
When an int meets a double in a binary operation, the int is promoted to double before the operation. 3 / 2.0 is 1.5 but 3 / 2 * 1.0 is 1.0 because the int division happens first.
Division by Zero
Integer division or modulo by zero throws ArithmeticException at runtime. Double division by zero does not throw; it produces Infinity or NaN.
final Keyword for Constants
final double TAX_RATE = 0.07; makes the variable unassignable after initialization. Any later assignment is a compile-time error.
Literal Types
3 is an int literal, 3.0 is a double literal, 'c' is char, "c" is a String, true is boolean. The type of a literal drives the arithmetic that follows it.
Expression vs Statement
An expression evaluates to a value (x * 2 + 1); a statement performs an action and ends in a semicolon (int y = x * 2 + 1;). Assignment is itself an expression that yields the assigned value.
Assignment is Right-to-Left
In x = x + 1; the right side is evaluated first using the current value of x, then stored back into x. Reading it as an algebra equation is a classic misconception.
Casting Precedence
A cast binds tighter than arithmetic: (double) 7 / 2 casts 7 first, giving 3.5, while (double)(7 / 2) casts the integer result 3, giving 3.0.
Modulo for Cycling
index % n always yields a value from 0 to n-1 for non-negative index, so it wraps counters around a fixed range (days of the week, positions on a ring).
Digit Extraction Pattern
n % 10 gives the last digit; n / 10 removes it. Looping these two steps until n reaches 0 processes every digit of a positive integer from right to left.
Even/Odd Test Safely
Use n % 2 == 0 for even and n % 2 != 0 for odd. Testing n % 2 == 1 fails for negative odd numbers because -3 % 2 is -1.
Numeric Precision of double
double holds about 15-16 significant decimal digits; large integers such as 12345678901234567890 cannot be exact. Money calculations often use cents as ints instead.
Char Arithmetic (Beyond Subset)
Chars are Unicode ints underneath, so 'a' + 1 is 98 (an int) and (char)('a' + 1) is 'b'. Not required by the CED but appears in code you may read.
A local variable exists only inside the block { } where it is declared. Redeclaring a name still in scope is a compile error; using it outside is a compile error too.
Uninitialized Local Variable
Reading a local variable before it is definitely assigned is a compile-time error, not a runtime one. Instance variables, in contrast, get default values.
Integer.MIN_VALUE Negation Anomaly
-Integer.MIN_VALUE evaluates back to Integer.MIN_VALUE because +2147483648 has no int representation. Math.abs of MIN_VALUE is likewise negative.
Mixed-type Promotion Rules
In a binary operation Java promotes the narrower operand: int with double gives double, char with int gives int. Promotion happens per operation, not per statement.
Compound Assignment Hides a Cast
int x = 5; x += 2.7; compiles because += performs an implicit narrowing cast, leaving x as 7. The equivalent x = x + 2.7 will not compile.
Order of Side Effects
Java evaluates operands strictly left to right, so in a++ + a++ the first a++ commits its increment before the second operand is read.
Integer Division Rounds Toward Zero
-7 / 2 gives -3, not -4. Integer division truncates the fraction; it never floors for negative dividends.
Rounding Half Up Without Math.round
For a nonnegative double d, (int)(d + 0.5) rounds to nearest. For negative values it rounds the wrong way, so subtract 0.5 instead.
Percent Change Formula in Code
(newVal - oldVal) / (double) oldVal * 100 requires the cast; without it an int division discards the fraction before multiplying by 100.
Overflow-safe Midpoint
low + (high - low) / 2 avoids the overflow that (low + high) / 2 can cause when both indices are near Integer.MAX_VALUE.
double Cannot Represent Large Integers Exactly
Beyond 2^53 a double loses integer precision, so consecutive whole numbers can compare as equal after conversion.
Epsilon Comparison
Compare doubles with Math.abs(a - b) < 1e-9 rather than ==, because binary rounding leaves tiny residues in sums like 0.1 + 0.2.
Scaling to Fix Money Arithmetic
Storing cents as an int avoids floating-point drift entirely; convert to dollars only when formatting output.
Unicode Ordering of Characters
Digits '0'-'9' precede uppercase 'A'-'Z', which precede lowercase 'a'-'z'. So 'a' - 'A' equals 32 for every letter.
Casting Binds Tighter Than Arithmetic
(double) a / b casts a first and gives a real quotient; (double)(a / b) divides as ints first and merely widens the truncated result.
Modulo Wrap for Circular Indexing
(i + step) % n moves forward around a ring of size n; ((i - step) % n + n) % n is needed to move backward safely when the difference is negative.
Boolean Operators Are Not Bitwise Here
In the AP subset && and || short-circuit; the single-character & and | forms would evaluate both sides and are outside the tested subset.
Constants Prevent Magic Numbers
final double TAX_RATE = 0.065; documents intent and guarantees the compiler rejects any later reassignment.