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
doublevalue to anintvalue 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(double)3: Evaluates to
Direct Variable Assignments and Explicit Conversions
Data Types and Memory Profiles Involved:
int: integerlong: integerdouble: 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 fromdouble() toint().one = big;: Illegal due to potential loss of capacity going fromlong() toint().
Legal Variable Assignments (Using Explicit Casting):
one = (int)dec;: Legal; explicitly convertsdecto integer value .one = (int)big;: Legal; explicitly convertsbigto integer value
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:
oneis temporarily converted to adouble() before division takes place, yielding
Code Statements and Expected Output:
out.println("1/2 = " + (1/2));-> Outputs1/2 = 0(Evaluates integer division )out.println("(double)1/2 = " + (double)1/2);-> Outputs(double)1/2 = 0.5(Casts to first, then performs )out.println("5/2 = " + (5/2));-> Outputs5/2 = 2(Evaluates integer division )out.println("5/(double)2 = " + 5/(double)2);-> Outputs5/(double)2 = 2.5(Casts to first, then performs )

Associated Program File Names:
cast.java,intcast.java
Rounding Algorithms for Floating-Point Numbers
Purpose of Rounding: Converts values of type
doubleto the nearest whole integer instead of truncating the decimal digits.Target Results:
rounds to
rounds to
Algorithm for Non-Negative (Positive) Numbers:
Mathematical Formula:
(int)(x + 0.5)Step-by-Step Procedure:
Add to the target positive number .
Apply the explicit cast
(int)to truncate the remaining fractional digits.
Step-by-Step Example for :
Add :
Perform cast:
(int)3.8=
Java Implementation Example:
Code:
double d = 9.25; int i = (int)(d + 0.5); System.out.println(i);Result: Outputs
Algorithm for Negative Numbers:
Mathematical Formula:
(int)(x - 0.5)Step-by-Step Procedure: Subtract from the negative number , then apply an
(int)cast.
Representation Limits and Overflow Errors
Definition of Integer Overflow Error: Occurs when an arithmetic expression evaluates to an
intvalue outside of its allowed representable range.Consequences: The evaluation wraps around and produces an
intvalue 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
doublevalue 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
intvalues.
Primitive Data Types Memory Usage and Ranges
Primitive Type Specifications:
byte:Memory Usage:
Value Range: to
short:Memory Usage:
Value Range: to
int:Memory Usage:
Value Range: to ( to )
long:Memory Usage:
Value Range: to
float:Memory Usage:
Value Range: to
double:Memory Usage:
Value Range: to
char:Memory Usage:
Value Range: to
boolean:Memory Usage:
Allowed Values:
trueorfalse
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:
Parentheses (Highest):
()Unary / Increment / Decrement:
!,++,--Multiplicative:
*,/,%Additive:
+,-Assignment and Compound Assignment:
=,+=,-=,*=,/=,%=Comma (Lowest):
,