Java Programming Language Chapter 2 Primitive Data Type

  • Slides: 12
Download presentation
Java Programming Language Chapter 2 : Primitive Data Type and Operation (ตอ ( Liang,

Java Programming Language Chapter 2 : Primitive Data Type and Operation (ตอ ( Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 1

Example: Converting Temperatures Write a program that converts a Fahrenheit degree to Celsius using

Example: Converting Temperatures Write a program that converts a Fahrenheit degree to Celsius using the formula: 2

Shortcut Assignment Operators Operator Example Equivalent += i += 8 i = i +

Shortcut Assignment Operators Operator Example Equivalent += i += 8 i = i + 8 -= f -= 8. 0 f = f - 8. 0 *= i *= 8 i = i * 8 /= i /= 8 i = i / 8 %= i %= 8 i = i % 8 3

Increment and Decrement Operators Operator ++var Name preincrement var++ postincrement --var predecrement var-- postdecrement

Increment and Decrement Operators Operator ++var Name preincrement var++ postincrement --var predecrement var-- postdecrement Description The expression (++var) increments var by 1 and evaluates to the new value in var after the increment. The expression (var++) evaluates to the original value in var and increments var by 1. The expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. The expression (var--) evaluates to the original value in var and decrements var by 1. 4

Increment and Decrement Operators, cont. 5

Increment and Decrement Operators, cont. 5

Numeric Type Conversion Consider the following statements: byte i = 100; long k =

Numeric Type Conversion Consider the following statements: byte i = 100; long k = i * 3 + 4; =>(i*3)+4 double d = i * 3. 1 + k / 2; => (i*3. 1)+(k/2) Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 6

Type Casting Implicit casting double d = 3; Explicit casting int i = (int)3.

Type Casting Implicit casting double d = 3; Explicit casting int i = (int)3. 0; int i = (int)3. 9; What is wrong? int x = 5 / 2. 0; => double x=5/2. 0; Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 7

Escape Sequences for Special Characters Description Escape Sequence Unicode Backspace b u 0008 Tab

Escape Sequences for Special Characters Description Escape Sequence Unicode Backspace b u 0008 Tab t u 0009 Linefeed n u 000 A Carriage return r u 000 D Backslash \ u 005 C Single Quote ' u 0027 Double Quote " u 0022 Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 8

Appendix B: ASCII Character Set is a subset of the Unicode from u 0000

Appendix B: ASCII Character Set is a subset of the Unicode from u 0000 to u 007 f Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 9

Casting between char and Numeric Types int i = 'a'; // Same as int

Casting between char and Numeric Types int i = 'a'; // Same as int i = (int)'a'; char c = 97; // Same as char c = (char)97; Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 10

The String Type The char type only represents one character. To represent a string

The String Type The char type only represents one character. To represent a string of characters, use the data type called String. For example, String msg = "Welcome to Java"; The String type is not a primitive type. It is known as a reference type. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 11

String Concatenation // Three strings are concatenated String msg = "Welcome " + "to

String Concatenation // Three strings are concatenated String msg = "Welcome " + "to " + "Java"; // String Chapter is concatenated with number 2 String s = "Chapter" + 2; // s becomes Chapter 2 // String Supplement is concatenated with character B String s 1 = "Supplement" + 'B'; // s becomes Supplement. B Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0 -13 -222158 -6 12