Download Chapter 2 Primitive Data Type and Operations

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
Introducing Programming with an
Example
Listing 2.1 Computing the Area of a Circle
This program computes the area of the
circle.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
2
Identifiers

identifier คือคำที่มีควำมหมำยในภำษำ ซึ่งเป็ นตัวอักษรที่เรี ยงต่อเนื่ องกัน
ประกอบด้วย ตัวอักษร(letters), ตัวเลข(digits), ขีดเส้นใต้(_) และ dollar
signs ($).

identifier จะต้องขึ้นต้นด้วยตัวหนังสื อ(letter), ขีดเส้นใต้(_), หรื อ
dollar sign ($).
ไม่สำมำรถขึ้นต้นด้วยตัวอักษรและคำที่เป็ น reserved
word.

identifier ไม่สำมำรถเป็ น true, false หรื อ null.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
3
กำรประกำศตัวแปร
int x;
// Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a;
// Declare a to be a
// character variable;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
4
ประโยคกำหนดค่ำ(Assignment Statements)
x = 1;
// Assign 1 to x;
radius = 1.0;
// Assign 1.0 to radius;
a = 'A';
// Assign 'A' to a;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
5
Declaring and Initializing
in One Step
 int
x = 1;
 double
d = 1.4;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
6
ค่ำคงที่(Constants)
รู ปแบบ :
final datatype CONSTANTNAME = VALUE;
คัวอย่ำง เช่น
final double PI = 3.14159;
final int SIZE = 3;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
7
Primitive Data Types
Range
Name
char
boolean
215 (0 to 255)
true/false
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
Storage Size
16 bit
1 bit
8
Integer Division
+, -, *, /, 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, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
9
Number Literals
Number literal เป็ นค่ำคงที่ที่กำหนดให้โปรแกรมโดยตรง
ตัวอย่ำง เช่น 34, 1,000,000 และ 5.0
ตัวอย่ำง เช่น
int i = 34;
long x = 1000000;
double d = 5.0;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
10
Floating-Point Literals
Floating-point literals คือค่ำคงที่เลขทศนิยม โดยปกติใน
ภำษำจำวำจะกำหนดให้เป็ น double type value.
ตัวอย่ำง เช่น 5.0 ในภำษำจำวำจะเป็ น double value, ไม่ใช่
float value. แต่เรำสำมำรถทำเป็ น float ได้โดยใส่ อกั ษร f หรื อ
F, และสำมำรถทำเป็ น double ได้โดยใส่ อกั ษร d หรื อ D.
ตัวอย่ำงเช่น 100.2f หรื อ 100.2F สำหรับ float number,
และ 100.2d หรื อ 100.2D สำหรับ double number.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
11
Arithmetic Expressions
3  4 x 10( y  5)( a  b  c)
4 9 x

 9( 
)
5
x
x
y
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
12
Example: Converting Temperatures
Write a program that converts a Fahrenheit degree
to Celsius using the formula:
celsius  ( 95 )( fahrenheit  32)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
13
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
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
14
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
15
Increment and
Decrement Operators, cont.
int i = 10;
int newNum = 10 * i++;
Same effect as
int i = 10;
int newNum = 10 * (++i);
int newNum = 10 * i;
i = i + 1;
Same effect as
i = i + 1;
int newNum = 10 * i;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
16
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
17
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;
range increases
byte, short, int, long, float, double
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
18
Escape Sequences for Special Characters
Description
Escape Sequence
Unicode
Backspace
\b
\u0008
Tab
\t
\u0009
Linefeed
\n
\u000A
Carriage return \r
\u000D
Backslash
\\
\u005C
Single Quote
\'
\u0027
Double Quote
\"
\u0022
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
19
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
20
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
21
String Concatenation
// Three strings are concatenated
String msg = "Welcome " + "to " + "Java";
// String Chapter is concatenated with number 2
String s = "Chapter" + 2; // s becomes Chapter2
// String Supplement is concatenated with character B
String s1 = "Supplement" + 'B'; // s becomes SupplementB
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
22
Two Ways to Invoke the Method
There are several ways to use the showInputDialog method. For
the time being, you only need to know two ways to invoke it.
One is to use a statement as shown in the example:
String string = JOptionPane.showInputDialog(null, x,
y, JOptionPane.QUESTION_MESSAGE));
where x is a string for the prompting message, and y is a string for
the title of the input dialog box.
The other is to use a statement like this:
JOptionPane.showInputDialog(x);
where x is a string for the prompting message.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
23
Converting Strings to Integers
input ที่รับมำจำก input dialog box จะเป็ นชนิด String. ถ้ำเรำต้องกำรให้
เป็ นตัวเลข อำทิเช่น 123 เรำต้องทำกำร Convert String ที่รับเข้ำมำเป็ นตัวเลข
โดยใช้ method parseInt สำหรับแปลงสตริ งเป็ น integerและ
parseDouble สำหรับแปลงสตริ งเป็ น double.
กำร convert a string เป็ น int value, เรำสำมำรถใช้ static parseInt
method ที่อยูใ่ น Integer class ตัวอย่ำงเช่น:
int i = Integer.parseInt(intString);
เมื่อ intString เป็ น numeric string อำทิเช่น “123”.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
24
Converting Strings to Doubles
กำรแปลง string เป็ น double value, สำมำรถใช้ static
parseDouble method ที่อยูใ่ น Double class ตัวอย่ำงเช่น:
double d =Double.parseDouble(doubleString);
เมื่อ doubleString เป็ น numeric string อำทิเช่น “123.45”.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
25
ข้อตกลงในกำรตั้งชื่อ
Variables and method names:
– ใช้อกั ษรตัวเล็กในกำรขึ้นต้น. ถ้ำชื่อประกอบด้วยหลำยๆคำต่อกัน, ให้
ใช้อกั ษรตัวเล็กสำหรับคำแรก, และขึ้นต้นด้วยอักษรตัวใหญ่สำหรับคำที่
ต่อกัน ตัวอย่ำงเช่น ตัวแปร radius and area, และ
method computeArea.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
26
ข้อตกลงในกำรตั้งชื่อ
 ชื่อ Class
:
– ขึ้นต้นด้วยอักษรตัวใหญ่ ถ้ำเป็ นคำผสมให้ข้ ึนด้วยอักษร
ตัวใหญ่. ตัวอย่ำงเช่น ชื่อ class
ComputeArea.
 Constants:
– ตัวแปรที่ใช้สำหรับเก็บค่ำคงที่ในภำษำจำวำ จะเป็ น
ตัวอักษรตัวใหญ่ท้ งั หมด. ตัวอย่ำงเช่น ตัวแปร PI
และ MAX_VALUE
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
27
Programming Errors
 Compile
Errors
– Detected by the compiler
 Runtime
Errors
– Causes the program to abort
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
28
Compile Errors
public class ShowSyntaxErrors {
public static void main(String[] args) {
i = 30;
//variable i is no type
System.out.println(i + 4);
}
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
29
Runtime Errors
public class ShowRuntimeErrors {
public static void main(String[] args) {
int i = 1 / 0;
//devid by zero
}
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
30
Programming Exercises
 Class
Work 0.25 point
 ให้นก
ั ศึกษำเขียนโปรแกรมรับข้อมูลจำกผูใ้ ช้โดยใช้ JOptionPane
และทำกำรคำนวณภำษีหกั ณ.ที่จ่ำย 15% และทำกำรแสดงผลข้อมูล
โดยใช้ JOptionPane ข้อมูล ดังนี้




ชื่อ
สังกัดแผนก
เงินเดือน
จำนวนเงินภำษีหกั ณ. ที่จ่ำย 15%
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
31
Related documents