chapter2primitivedatatypesandoperations内容摘要:

le, is considered a double value, not a float 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. For example, you can use or for a float number, and or for a double number. Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 25 Scientific Notation Floatingpoint literals can also be specified in scientific notation, for example, +2, same as , is equivalent to , and is equivalent to . E (or e) represents an exponent and it can be either in lowercase or uppercase. Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 26 Arithmetic Expressions )94(9))(5(105 43 y xxx cbayx is translated to (3+4*x)/5 – 10*(y5)*(a+b+c)/x + 9*(4/x + (9+x)/y) Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 27 Example: Converting Temperatures Write a program that converts a Fahrenheit degree to Celsius using the formula: FahrenheitToCelsius Run )32)(( 95  f a h r e n h e i tc e l s i u sLiang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 28 Shortcut Assignment Operators Operator Example Equivalent += i += 8 i = i + 8 = f = f = f *= i *= 8 i = i * 8 /= i /= 8 i = i / 8 %= i %= 8 i = i % 8 Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 29 Increment and Decrement Operators Operator Name Description ++var preincrement The expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++ postincrement The expression (var++) evaluates to the original value in var and increments var by 1. var predecrement The expression (var) decrements var by 1 and evaluates to the new value in var after the decrement. var postdecrement The expression (var) evaluates to the original value in var and decrements var by 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 30 Increment and Decrement Operators, cont. int i = 10。 int newNum = 10 * i++。 int newNum = 10 * i。 i = i + 1。 S a m e e f f ec t as int i = 10。 int newNum = 10 * ( ++i )。 i = i + 1。 int newNum = 10 * i。 S a m e e f f ec t as Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 31 Increment and Decrement Operators, cont. Using increment and decrement operators makes expressions short, but it also makes them plex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i. Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 32 Assignment Expressions and Assignment Statements Prior to Java 2, all the expressions can be used as statements. Since Java 2, only the following types of expressions can be statements: variable op= expression。 // Where op is +, , *, /, or % ++variable。 variable++。 variable。 variable。 Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 33 Numeric Type Conversion Consider the following statements: byte i = 100。 long k = i * 3 + 4。 double d = i * + k / 2。 Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 34 Conversion Rules When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int. Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 35 Type Casting Implicit casting double d = 3。 (type widening) Explicit casting int i = (int)。 (type narrowing) int i = (int)。 (Fraction part is truncated) What is wrong? int x = 5 /。 byte , short, int, long, float, double range increases Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 36 Example: Keeping Two Digits After Decimal Points Write a program that displays the sales tax with two digits after the decimal point. SalesTax Run Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 37 Character Data Type char letter = 39。 A39。 (ASCII) char numChar = 39。 439。 (ASCII) char letter = 39。 \u004139。 (Unicode) char numChar = 39。 \u003439。 (Unicode) Four hexadecimal digits. NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b. char ch = 39。 a39。 (++ch)。 Liang, Introduction to Java Programming, Sixth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 0132221586 38 Unicode Format Java characters use Unicode, a 16bit encoding scheme established by the Unicode Consortium to support the interchange, processing, and display of written texts in the wo。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。