Casting, Variable Ranges, and Operator Precedence Notes

Data Type Compatibility and Casting Operators

  • Definition of Casting: Casting is used to temporarily change the data type of a value and create compatibility among different data types.

  • Casting Operators:

    • (int): Converts a numerical value to an integer.

    • (double): Converts a numerical value to a floating-point decimal.

  • Truncation Behavior: Casting a double value to an int value causes all digits to the right of the decimal point to be truncated (dropped without rounding).

  • Basic Casting Syntax Examples:

    • (int)3.14159: Evaluates to 33

    • (double)3: Evaluates to 3.03.0

Direct Variable Assignments and Explicit Conversions

  • Data Types and Memory Profiles Involved:

    • int: 32 bit32\,\text{bit} integer

    • long: 64 bit64\,\text{bit} integer

    • double: 64 bit64\,\text{bit} real floating-point number

  • Initial Variable Declarations:

    • int one = 0;

    • long big = 453;

    • double dec = 7.56;

  • Illegal Variable Assignments (Require Explicit Narrowing Casts):

    • one = dec;: Illegal due to potential loss of decimal precision going from double (64 bit64\,\text{bit}) to int (32 bit32\,\text{bit}).

    • one = big;: Illegal due to potential loss of capacity going from long (64 bit64\,\text{bit}) to int (32 bit32\,\text{bit}).

  • Legal Variable Assignments (Using Explicit Casting):

    • one = (int)dec;: Legal; explicitly converts dec to integer value 77.

    • one = (int)big;: Legal; explicitly converts big to integer value 453453

Division Mechanics and Mixed-Type Arithmetic

  • Floating-Point Promotion Rule: As long as at least one operand in a division operation is a floating-point decimal value, the resulting operation evaluates to a decimal.

  • Conversion Sequence in Code:

    • Code snippet: int one = 11; int two = 5; double dec = (double)one / two;

    • Execution detail: one is temporarily converted to a double (11.011.0) before division takes place, yielding 11.0/5=2.211.0 / 5 = 2.2

  • Code Statements and Expected Output:

    • out.println("1/2 = " + (1/2)); -> Outputs 1/2 = 0 (Evaluates integer division 1/2=01 / 2 = 0)

    • out.println("(double)1/2 = " + (double)1/2); -> Outputs (double)1/2 = 0.5 (Casts 11 to 1.01.0 first, then performs 1.0/2=0.51.0 / 2 = 0.5)

    • out.println("5/2 = " + (5/2)); -> Outputs 5/2 = 2 (Evaluates integer division 5/2=25 / 2 = 2)

    • out.println("5/(double)2 = " + 5/(double)2); -> Outputs 5/(double)2 = 2.5 (Casts 22 to 2.02.0 first, then performs 5/2.0=2.55 / 2.0 = 2.5)

Division and Casting Output Examples
  • Associated Program File Names: cast.java, intcast.java

Rounding Algorithms for Floating-Point Numbers

  • Purpose of Rounding: Converts values of type double to the nearest whole integer instead of truncating the decimal digits.

  • Target Results:

    • 2.432342.43234 rounds to 22

    • 4.673844.67384 rounds to 55

  • Algorithm for Non-Negative (Positive) Numbers:

    • Mathematical Formula: (int)(x + 0.5)

    • Step-by-Step Procedure:

    1. Add 0.50.5 to the target positive number xx.

    2. Apply the explicit cast (int) to truncate the remaining fractional digits.

    • Step-by-Step Example for 3.33.3:

    1. Add 0.50.5: 3.3+0.5=3.83.3 + 0.5 = 3.8

    2. Perform cast: (int)3.8 = 33

    • Java Implementation Example:

    • Code: double d = 9.25; int i = (int)(d + 0.5); System.out.println(i);

    • Result: Outputs 99

  • Algorithm for Negative Numbers:

    • Mathematical Formula: (int)(x - 0.5)

    • Step-by-Step Procedure: Subtract 0.50.5 from the negative number xx, then apply an (int) cast.

Representation Limits and Overflow Errors

  • Definition of Integer Overflow Error: Occurs when an arithmetic expression evaluates to an int value outside of its allowed representable range.

  • Consequences: The evaluation wraps around and produces an int value within the allowed range, but it will not match the expected mathematical result.

Floating-Point Memory Limitations and Round-Off Errors

  • Definition of Round-Off Error:

    • Computers allocate a specified, fixed amount of memory to store data based on data type.

    • If an expression evaluates to a double value that is more precise than can be stored in the allotted memory, a round-off error occurs.

  • Outcome: The calculated value is rounded automatically to the nearest representable value.

  • Mitigation Strategy: To avoid round-off errors that naturally occur in floating-point operations, use int values.

Primitive Data Types Memory Usage and Ranges

  • Primitive Type Specifications:

    • byte:

    • Memory Usage: 8 bits8\,\text{bits}

    • Value Range: −128-128 to 127127

    • short:

    • Memory Usage: 16 bits16\,\text{bits}

    • Value Range: −32768-32768 to 3276732767

    • int:

    • Memory Usage: 32 bits32\,\text{bits}

    • Value Range: −2×109-2 \times 10^9 to 2×1092 \times 10^9 (−2 billion-2\,\text{billion} to 2 billion2\,\text{billion})

    • long:

    • Memory Usage: 64 bits64\,\text{bits}

    • Value Range: −big- \text{big} to +big+ \text{big}

    • float:

    • Memory Usage: 32 bits32\,\text{bits}

    • Value Range: −big- \text{big} to +big+ \text{big}

    • double:

    • Memory Usage: 64 bits64\,\text{bits}

    • Value Range: −big- \text{big} to +big+ \text{big}

    • char:

    • Memory Usage: 16 bit unsigned16\,\text{bit unsigned}

    • Value Range: 00 to 6553565535

    • boolean:

    • Memory Usage: 1 bit1\,\text{bit}

    • Allowed Values: true or false

  • General Requirement: Knowing all data types and what each one can store is essential for precise program construction.

Operator Precedence Hierarchy

  • Execution Order from Highest Precedence to Lowest Precedence:

    1. Parentheses (Highest): ()

    2. Unary / Increment / Decrement: !, ++, --

    3. Multiplicative: *, /, %

    4. Additive: +, -

    5. Assignment and Compound Assignment: =, +=, -=, *=, /=, %=

    6. Comma (Lowest): ,