D2: Arithmetic Operators, Precedence, and Escape Sequences
Arithmetic Expressions and Basic Operators
An arithmetic expression is defined as a combination of numbers, variables, and operators that instructs the computer to perform a mathematical calculation.
The primary arithmetic operators in Java include:
+: Addition-: Subtraction*: Multiplication/: Division%: Modulus (Modulo)

Data Types and Storage Rules
Integer (
int) Data Type:Represents whole numbers without fractional components.
When performing integer division using
intvalues, Java truncates or chops off the decimal portion completely.Arithmetic operations involving exclusively
intvalues produce anintresult:(The exact quotient is , but Java truncates the decimal portion to return ).
Double (
double) Data Type:Represents floating-point numbers and is always stored with at least one decimal place.
Internal Promotion: Assigning an integer value to a double variable causes Java to store it with a decimal (e.g.,
double num = 5;is stored internally asdouble num = 5.0;).Data Type Compatibility & Storage Restrictions:
intvalues can be directly stored indoublevariables.doublevalues cannot be stored inintvariables without explicit type casting.Expressions such as
int x = 2.0 / 3;orint x = 4 / 5 * 3.0;produce compilation errors because adoubleresult cannot be assigned to anintvariable type.
Mixed-Type Arithmetic Operations:
Any arithmetic operation involving at least one
doublevalue will automatically evaluate to adoubleresult.Examples:
Modulo Operator
The modulo operator (
%) computes and returns the remainder of integer division performed between two numbers.Examples and Evaluation:
int x = 8 % 3;evaluates to , because with a remainder of, derived from with a remainder of
Order of Precedence and Compound Expressions
Compound Expressions: Expressions that combine multiple numbers, variables, and operators into a single calculation.
Hierarchy of Precedence (Order of Operations for Computers):
Parentheses
()and Type Casting (Highest Precedence)Multiplication
*, Division/, and Modulo%(Middle Precedence)Addition
+and Subtraction-(Lowest Precedence)
Step-by-Step Compound Expression Evaluation:
Expression:
Step 1 (Parentheses): Evaluate , updating the expression to
Step 2 (Modulo): Evaluate (since remainder ), updating the expression to
Step 3 (Addition & Subtraction left to right): Evaluate , then . The final evaluation result is

Type Casting and Shortcut Operators
Type Casting:
Used to convert a value explicitly from one data type to another.
Syntax example:
(int) 4.7truncates the decimal portion and evaluates to integer4.Casting operations share the highest precedence level along with parentheses.
Shortcut Operators:
x++;: Increment operator; incrementsxbyx--;: Decrement operator; decrementsxbyx += 3;: Addition assignment operator; adds toxx -= 1;: Subtraction assignment operator; subtracts fromx
Division by Zero
Dividing an
intby 0:Java stores the evaluation as
undefined(throwing an arithmetic runtime error).An undefined result cannot be used in subsequent arithmetic operations.
Dividing a
doubleby 0:Java stores the evaluation as
infinity.

Literals and Escape Sequences
Literals:
Fixed values written directly into source code that remain constant throughout execution.
Figurative usage employs comparisons to establish meaning.
Numerical literal example:
10String literal example: “I am a string”
Escape Sequences:
Allow you to include special characters in string
\n (line break in middle of string)
\t (tab space)
\\ (one backslash)
\” (for literal qoute)