1 / 24

Integer numerical data types

Learn about integer data types in Java, their binary encoding, arithmetic operations, priority, and automatic conversions. Get familiar with assignment operations, automatic type conversions, and shorthand operators.

connies
Télécharger la présentation

Integer numerical data types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Integer numerical data types

  2. The integer data types  • The integer data types use the binary number system as encoding method • There are a number of differentinteger types in Java

  3. Integer operators • Integer operators are arithmetic operators that manipulate (operate on)integer values • A integer operatoronly operates on integer values • The result of an integer operator is always an integer value

  4. Priority and associativity of the integer arithmetic operators

  5. The integer data types  • The computer can only perform operations on operands of the same date type: • Correct incorrect

  6. Automatic conversions in Integer arithmetic • All values are converted to int typebefore an arithmetic operation (+, −, *, /, %) in performed. • If one of the values in an arithmetic operation is long, then all values are converted to long typebefore the arithmetic operation in performed.

  7. Safe Conversions • Safe conversions: • Unsafe conversions:

  8. Automatic type conversion in the assignment operation •  The safe integer conversions are:

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

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

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

  12. Overflow

  13. Reading integer input from the keyboard

  14. Numeric literals (1) • A numeric literal is a constant value that appears in a Java program. • Every numerical literal (constant) has a data type

  15. Numeric literals (2) • Special tags that change the data type of a constant • long tag: The tag L or lafter 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 fafter a decimal literal will change the data type of the literal to floatExample: 123.45f has the type float

  16. Assigning integer constants to byte and short variables (1) • Strictly speaking, you need to use casting operators:

  17. 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 typeif the constant is within the range of the data type of the variable. • Example: The range of the byte type is −128 ... 127

  18. The assignment expressions (1) • Result of var = expr is equal to the value of expr • In addition to updating the variable. the assignment operatoralsoreturns a value

  19. The assignment expressions (2) • When there are multiple assignment operators in an expression, the expression is evaluate from right to left

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

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

  22. 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 varby 1 • The only difference between ++var and var++ is: the value that is returned by the expression.

  23. The ++ and -- operators (2) • The pre-operations++a and --a will: Apply the increment/decrement operationbefore (pre)returning the value in the variable a • The post-operationsa++ and a-- will: Apply the increment/decrement operationafter (pre)returning the value in the variable a

  24. The ++ and -- operators (3)

More Related