Operators +-.*/.and% 5/2 yields an integer 2 5.0 /2 yields a double value 2.5 5% 2 yields 1(the remainder of the division) Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 21 Operators +, -, *, /, and % 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division)
Remainder operator determine whether a number is even or odd using %o Suppose you know January 1, 2005 is Saturday, you can find that the day for February 1, 2005 is tuesday using the following expression Saturday is the 6 day in a week A week has 7 days (6+31)871s2 The 2 day in a week is Tuesda January has 31 days Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 22 Remainder Operator ➢determine whether a number is even or odd using %. ➢ Suppose you know January 1, 2005 is Saturday, you can find that the day for February 1, 2005 is Tuesday using the following expression: Saturday is the 6th day in a week A week has 7 days January has 31 days The 2nd day in a week is Tuesday (6 + 31) % 7 is 2
NOTE Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy Integers are stored precisely. Therefore, calculations with integers yield a precise integer result Show case FloatlsNotPrecise java Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 23 NOTE ➢Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy. ➢ Integers are stored precisely. Therefore, calculations with integers yield a precise integer result. Show case FloatIsNotPrecise.java
Number literals A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements int 1=34 long X=1000000 double d= 5.0 A compilation error would occur if the literal were too large for the variable to hold byte b=1000. 6 Wrong Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 24 Number Literals A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements: int i = 34; long x = 1000000; double d = 5.0; byte b = 1000 Wrong A compilation error would occur if the literal were too large for the variable to hold
Number literals An integer literal is assumed to be of the int type whose value is between -231(-2147483648 )to 23( 1(2147483647). To denote an integer literal of the long type, append it with the letter L or I long j=10; Is this statement correct correct By default, a floating-point literal is treated as a double type value. You can make a number a float by appending the letter f or F, and make a number a double by appending the letter d or d Floating-point literals can also be specified scientific notation. for example. 1.23456e+2 Liang, Introduction to Java Programming, revised by dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 25 Number Literals ➢An integer literal is assumed to be of the int type, whose value is between -2 31 (-2147483648) to 2 31– 1 (2147483647). To denote an integer literal of the long type, append it with the letter L or l. long j = 10; Is this statement correct? ➢By default, a floating-point literal is treated as a double type value. You can make a number a float by appending the letter f or F, and make a number a double by appending the letter d or D. ➢Floating-point literals can also be specified in scientific notation, for example, 1.23456e+2 correct