Integer numerical data types The integer data types • The integer data types use the binary number system as encoding method • There are a number of different integer types in Java Integer operators • Integer operators are arithmetic operators that manipulate (operate on) integer values • A integer operator only operates on integer values • The result of an integer operator is always an integer value Priority and associativity of the integer arithmetic operators The integer data types • The computer can only perform operations on operands of the same date type: • Correct incorrect Automatic conversions in Integer arithmetic • All values are converted to int type before an arithmetic operation (+, −, *, /, %) in performed. • If one of the values in an arithmetic operation is long, then all values are converted to long type before the arithmetic operation in performed. Safe Conversions • Safe conversions: • Unsafe conversions: Automatic type conversion in the assignment operation • The safe integer conversions are: Summary of automatic conversion rules (1) • Integers: When an arithmetic operation contains 2 integer values, convert any lower precision integer type to int type When an arithmetic operation contains a long value, convert any lower precision integer type to long type • Floating point numbers (float and double): When an arithmetic operation contains 2 floating point values, convert any lower precision float type to double type Summary of automatic conversion rules (2) • When an arithmetic operation contains one floating point value and one integer value: convert the integer value to double convert the lower precision float type to double type Summary of automatic conversion rules (3) • If the range of values of type1 contains the range of values of type2, then Java will perform an automatic promotion from type2 ⇒ type1 for the assignment operation Overflow Reading integer input from the keyboard Numeric literals (1) • A numeric literal is a constant value that appears in a Java program. • Every numerical literal (constant) has a data type Numeric literals (2) • Special tags that change the data type of a constant • long tag: The tag L or l after an integer literal will change the data type of the literal to longExample: 12345L has the type long • The float tag: The tag F or f after a decimal literal will change the data type of the literal to floatExample: 123.45f has the type float Assigning integer constants to byte and short variables (1) • Strictly speaking, you need to use casting operators: Assigning integer constants to byte and short variables (2) • If an integer literal (constant) is assigned to a byte typed or short typed variable, the literal (constant) is automatically converted into the appropriate type if the constant is within the range of the data type of the variable. • Example: The range of the byte type is −128 ... 127 The assignment expressions (1) • Result of var = expr is equal to the value of expr • In addition to updating the variable. the assignment operator also returns a value The assignment expressions (2) • When there are multiple assignment operators in an expression, the expression is evaluate from right to left Assignment statements with the same variable on the LHS and RHS • The symbol "=" denotes an assignment operation: 1. The computer will first evaluate the RHS "x + 4.0": RHS = x + 4.0 (x contains 1.0) = 1.0 + 4.0 = 5.0 2. Then the result 5.0 is assigned to the receiving variable x: x = 5.0; Therefore, after executing the statement "x = x + 4.0", the variable x contains the value 5.0 Shorthand operators • A shorthand operator is a shorter way to express something that is already available in the Java programming language The ++ and -- operators (1) • Pre-operation: the ++ and -- operator appear before the variable • Post-operation: the ++ and -- operator appear after the variable • Both operations (++var and var++) will increment the variable var by 1 • The only difference between ++var and var++ is: the value that is returned by the expression. The ++ and -- operators (2) • The pre-operations ++a and --a will: Apply the increment/decrement operation before (pre) returning the value in the variable a • The post-operations a++ and a-- will: Apply the increment/decrement operation after (pre) returning the value in the variable a The ++ and -- operators (3)