📖 Crammy · All study guides
AP Computer Science A · Unit 1

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.

Study this unit free →

More AP Computer Science A guides

Java Primitives
Basic data types: int, double, boolean, char. Store actual values (not references). Eight primitive types total.
int Type
32-bit integer; range ≈ -2 billion to +2 billion. Used for counting, indexing, calculations without decimals.
double Type
64-bit floating-point; represents decimal numbers. Used for precise calculations, measurements, divisions.
boolean Type
True or false; result of comparisons. Used in conditionals (if/else), loops. Single bit conceptually.
char Type
Single character: 'A', '5', '!'. Uses Unicode encoding. Enclosed in single quotes (different from String double quotes).
Type Casting
Convert between types: (int) 3.7 → 3 (truncates). (double) 5 → 5.0. Widening (small to large) safe; narrowing (large to small) risky.
Implicit Type Conversion
Java automatically widens types (int to double). Narrowing requires explicit cast. Math operations promote to larger type.
Arithmetic Operators
+ (add), - (subtract), * (multiply), / (divide), % (modulo/remainder). Precedence: *, /, % then +, -. Left-to-right for same precedence.
Modulo Operator %
Returns remainder: 7 % 3 = 1. Useful for: divisibility checks, cycling values, extracting digits.
Integer Division
5 / 2 = 2 (not 2.5) in Java. One or both operands must be double for decimal result: 5 / 2.0 = 2.5.
Variable Declaration
type name; or type name = value;. Example: int age; int age = 25;. Must declare before using.
Naming Conventions
Variables: camelCase starting lowercase. Constants: ALL_CAPS. Descriptive names improve readability.
Comparison Operators
== (equal), != (not equal), < (less), > (greater), <= (less/equal), >= (greater/equal). Return boolean.
Logical Operators
&& (AND), || (OR), ! (NOT). Used in compound conditions. Short-circuit: && stops if first false, || stops if first true.
Order of Operations
1. Parentheses, 2. Multiplication/Division/Modulo, 3. Addition/Subtraction, 4. Comparison, 5. Logical operators.
String Type (Not Primitive)
String class (capital S) for text. String s = \"Hello\"; Reference type, not primitive. Immutable.
Unit 1 Summary
Primitives: int, double, boolean, char. Casting converts types. Operators: arithmetic (+,-,*,/,%), comparison, logical. Variables store data.
Integer Overflow
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.
Drill these as interactive flashcards →
Integer.MIN_VALUE / MAX_VALUE
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.
Test yourself on this unit →
Scope of a Local Variable
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.
Ask the AI tutor about this unit →
Literal Suffixes and Types
An unsuffixed whole number literal is an int and an unsuffixed decimal literal is a double, which is why 3.0f or 5L are needed for other types.
Division by Zero Differs by Type
Integer division by zero throws ArithmeticException, but double division by zero produces Infinity or NaN with no exception.
NaN Compares False to Everything
NaN == NaN is false, so a NaN result must be detected with Double.isNaN rather than an equality test.
Truncation as a Deliberate Tool
n / 100 * 100 zeroes out the last two digits because the truncating division discards them before the multiplication restores the scale.
Evaluating Nested Casts
(int)(char)(65.9) truncates to 65, converts to 'A', then promotes back to the int 65 — each cast applies to the value produced so far.
Reading Compound Boolean Precedence
! binds tightest, then relational operators, then &&, then ||, so a || b && c is a || (b && c).
Turn these into flashcards & quizzes →