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)

Handwritten computer science notes on arithmetic operators, integer division, modulo operator, order of precedence, and double data type

Data Types and Storage Rules

  • Integer (int) Data Type:

    • Represents whole numbers without fractional components.

    • When performing integer division using int values, Java truncates or chops off the decimal portion completely.

    • Arithmetic operations involving exclusively int values produce an int result:

    • 4×2=84 \times 2 = 8

    • −2+11=9-2 + 11 = 9

    • 23%2=123 \% 2 = 1

    • 10/3=310 / 3 = 3 (The exact quotient is 3.33…3.33\dots, but Java truncates the decimal portion to return 33).

  • 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 as double num = 5.0;).

    • Data Type Compatibility & Storage Restrictions:

    • int values can be directly stored in double variables.

    • double values cannot be stored in int variables without explicit type casting.

    • Expressions such as int x = 2.0 / 3; or int x = 4 / 5 * 3.0; produce compilation errors because a double result cannot be assigned to an int variable type.

  • Mixed-Type Arithmetic Operations:

    • Any arithmetic operation involving at least one double value will automatically evaluate to a double result.

    • Examples:

    • 32.2−3=29.232.2 - 3 = 29.2

    • 4.0×2=8.04.0 \times 2 = 8.0

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 22, because 8/3=28 / 3 = 2 with a remainder of 22

    • 17%5=217 \% 5 = 2, derived from 17/5=317 / 5 = 3 with a remainder of 22

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):

    1. Parentheses () and Type Casting (Highest Precedence)

    2. Multiplication *, Division /, and Modulo % (Middle Precedence)

    3. Addition + and Subtraction - (Lowest Precedence)

  • Step-by-Step Compound Expression Evaluation:

    • Expression: 4+15%(2×3)−24 + 15 \% (2 \times 3) - 2

    • Step 1 (Parentheses): Evaluate (2×3)=6(2 \times 3) = 6, updating the expression to 4+15%6−24 + 15 \% 6 - 2

    • Step 2 (Modulo): Evaluate 15%6=315 \% 6 = 3 (since 15/6=215 / 6 = 2 remainder 33), updating the expression to 4+3−24 + 3 - 2

    • Step 3 (Addition & Subtraction left to right): Evaluate 4+3=74 + 3 = 7, then 7−2=57 - 2 = 5. The final evaluation result is 55

Handwritten computer science notes on shortcuts, literals, escape sequences, arithmetic expressions, compound expressions, and order of operations

Type Casting and Shortcut Operators

  • Type Casting:

    • Used to convert a value explicitly from one data type to another.

    • Syntax example: (int) 4.7 truncates the decimal portion and evaluates to integer 4.

    • Casting operations share the highest precedence level along with parentheses.

  • Shortcut Operators:

    • x++; : Increment operator; increments x by 11

    • x--; : Decrement operator; decrements x by 11

    • x += 3; : Addition assignment operator; adds 33 to x

    • x -= 1; : Subtraction assignment operator; subtracts 11 from x

Division by Zero

  • Dividing an int by 0:

    • Java stores the evaluation as undefined (throwing an arithmetic runtime error).

    • An undefined result cannot be used in subsequent arithmetic operations.

  • Dividing a double by 0:

    • Java stores the evaluation as infinity.

Handwritten computer science notes on double conversions, integer vs double arithmetic operations, and division by zero

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: 10

    • String 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)