Top Banner
Integer numerical data types
24

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.

Jan 03, 2016

Download

Documents

Ralf King
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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 numerical data types

Page 2: 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.

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

Page 3: 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 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

Page 4: 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.

Priority and associativity of the integer arithmetic operators

Page 5: 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.

The integer data types 

• The computer can only perform operations on operands of the same date type:

• Correct incorrect

Page 6: 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.

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.

Page 7: 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.

Safe Conversions

• Safe conversions:

• Unsafe conversions:

Page 8: 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.

Automatic type conversion in the assignment operation

•  The safe integer conversions are:

Page 9: 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.

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

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

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

Page 11: 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.

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

Page 12: 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.

Overflow

Page 13: 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.

Reading integer input from the keyboard

Page 14: 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.

Numeric literals (1)

• A numeric literal is a constant value that appears in a Java program.

• Every numerical literal (constant) has a data type

Page 15: 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.

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

Page 16: 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.

Assigning integer constants to byte and short variables (1)

• Strictly speaking, you need to use casting operators:

Page 17: 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.

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

Page 18: 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.

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

Page 19: 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.

The assignment expressions (2)

• When there are multiple assignment operators in an expression, the expression is evaluate from right to left

Page 20: 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.

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

Page 21: 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.

Shorthand operators

• A shorthand operator is a shorter way to express something that is already available in the Java programming language

Page 22: 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.

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.

Page 23: 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.

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

Page 24: 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.

The ++ and -- operators (3)