Verified for the 2025 AP Computer Science A exam•Citation:
A literal is the source code representation of a fixed value. These are values that appear directly in your code.
Examples:
5
- integer literal3.14
- double literal'a'
- character literal"Hello"
- string literalArithmetic expressions in Java can be of type int or double:
Type | Example | Description |
---|---|---|
int | 5 + 3 | Whole numbers without decimal points |
double | 5.2 - 1.7 | Numbers that can have decimal points |
Java provides five basic arithmetic operators:
| Operator | Description | Example | |----------|---------------------|----------| | + | Addition | a + b | | - | Subtraction | a - b | | * | Multiplication | a * b | | / | Division | a / b | | % | Modulus (remainder) | a % b |
Understanding how types interact in expressions is crucial:
int
values will produce an int
resultint result = 7 / 2; // result is 3, not 3.5
double
value will produce a double
resultdouble result = 7 / 2.0; // result is 3.5
int x = 5 / 2; // x is 2, not 2.5
Be careful with division operations:
int
by zero results in an ArithmeticException
int badResult = 5 / 0; // Causes ArithmeticException
double
by zero doesn't throw an exception but gives special values:double result1 = 5.0 / 0.0; // result is Infinity double result2 = -5.0 / 0.0; // result is -Infinity double result3 = 0.0 / 0.0; // result is NaN (Not a Number)
When an expression contains multiple operators, Java follows these precedence rules:
()
+
, -
, !
)*
, /
, %
)+
, -
)Example:
int result = 2 + 3 * 4; // result is 14, not 20 int result2 = (2 + 3) * 4; // result is 20
When operators of the same precedence appear, Java evaluates them according to their associativity:
int a = 10 - 5 - 2; // Evaluated as (10 - 5) - 2 = 3
int a, b, c; a = b = c = 5; // Evaluated as a = (b = (c = 5))
The assignment operator (=
) stores the value of the right-side expression into the variable on the left:
int score = 95; // Assigns the value 95 to the variable score
Java provides shorthand operators that combine arithmetic and assignment:
| Operator | Example | Equivalent to | |----------|----------|-----------------| | += | x += 5 | x = x + 5 | | -= | x -= 5 | x = x - 5 | | *= | x *= 5 | x = x * 5 | | /= | x /= 5 | x = x / 5 | | %= | x %= 5 | x = x % 5 |
Example:
int count = 10; count += 5; // count is now 15 count *= 2; // count is now 30
When assigning values of different types, Java performs automatic type conversion if the assignment is allowed:
int i = 5; double d = i; // Implicit conversion from int to double (allowed) double pi = 3.14159; int approx = pi; // This will cause a compilation error - explicit cast needed int approx = (int)pi; // Explicit cast - truncates to 3
One of the most common mistakes is expecting decimal results from integer division:
// What's wrong with this average calculation? int score1 = 95; int score2 = 98; double average = (score1 + score2) / 2; // Will be 96.0, not 96.5 // Correct approach: double average = (score1 + score2) / 2.0; // Will be 96.5
The ++
and --
operators increase or decrease a variable by 1:
int count = 5; count++; // count is now 6 count--; // count is back to 5
These can be used as prefix or postfix, with different behaviors in expressions:
int a = 5; int b = ++a; // a is incremented to 6, then b is assigned 6 // Now a is 6, b is 6 int c = 5; int d = c++; // d is assigned 5, then c is incremented to 6 // Now c is 6, d is 5
Be careful with expressions that modify the same variable multiple times:
int x = 5; int result = x * (x++); // Behavior can be confusing, avoid this pattern
Remember that understanding expressions and assignments is fundamental to programming in Java. Pay close attention to the types involved in your expressions and be mindful of operator precedence when writing compound expressions.