primitive data types and operationsnutthanon/886201/slide2.pdf · final int size = 3; 10...

41
1 Primitive data types and operations

Upload: others

Post on 06-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

1

Primitive data types and operations

Page 2: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

เราจะมาเรยนรเกยวกบ Java primitive data types และเรองทเกยวของ เชน ตวแปร, ชนดของตวแปร (ชนดของขอมล), operators, expressions (นพจน), และ input and output.

Page 3: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

3

วตถประสงค • ใหรจกการใชตวแปรเกบขอมล • ใหรจกค าสงก าหนดคาตวแปร (assignment statements) และ นพจนท

เกยวของ • ใหรจกการใชคาคงท (constants) เกบขอมลทมคาถาวร • ใหรจกการประกาศ Java primitive data types: byte, short, int,

long, float, double, and char • ใหรจกการใช operators ในการเขยนนพนจทเกยวกบตวเลข (numeric

expressions) • ใหรจกการใชตวอกขระ (char) โดยใชชนด char • ใหรจการใชสายอกขระ (string) โดยใชชนด String • ใหรจกการรบขอมลเขาจาก console โดยใช class Scanner

Page 4: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

4

Identifiers

• identifier เปนค าทประกอบทประกอบไปดวย ตวอกษร, ตวเลข, ขดเสนใต (_), และ dollar signs ($).

• identifier ตองเรมตนดวยตวอกษร, ขดเสนใต (_), หรอ dollar sign ($) เรมดวยตวเลขไมได

– identifier เปนค าท Javฤ จองไว (reserved word) ไมได

• identifier ไมสามารถเปน true, false, หรอ null.

• identifier จะมความยาวเทาไหรกได

Page 5: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

5

Variables

// Compute the first area

radius = 1.0;

area = radius * radius * 3.14159;

System.out.println("The area is “ +

area + " for radius "+radius);

// Compute the second area

radius = 2.0;

area = radius * radius * 3.14159;

System.out.println("The area is “ +

area + " for radius "+radius);

Page 6: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

6

การประกาศตวแปร (Declaring Variables)

int x; // ประกาศตวแปร x ชนด int (integer) // (เกบคาจ านวนเตม)

double radius; // ประกาศตวแปร radius ชนด double // (เกบคาทศนยม)

char a; // ประกาศตวแปร a ชนด char (character) // (เกบคาอกขระ)

Page 7: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

7

Assignment Statements (ค าสงการก าหนดคา)

x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;

a = 'A'; // Assign 'A' to a;

= แปลวา น าคาทางดานขวามอไปใสไวในตวแปร (หนวยความจ า) ดาน ซายมอ

Page 8: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

8

ประกาศ (Declaring) และ ก าหนดคาเรมตน (Initializing) ใน step เดยว

• int x = 1;

• double d = 1.4;

Page 9: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

9

คาคงท (Constants)

final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159;

final int SIZE = 3;

Page 10: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

10

ชนดของขอมลชนดตวเลข (Numerical Data Types)

Name Range Storage Size

byte –2

7 (-128) to 2

7–1 (127) 8-bit signed

short –2

15 (-32768) to 2

15–1 (32767) 16-bit signed

int –2

31 (-2147483648) to 2

31–1 (2147483647) 32-bit signed

long –2

63 to 2

63–1 64-bit signed

(i.e., -9223372036854775808

to 9223372036854775807)

float Negative range: 32-bit IEEE 754

-3.4028235E+38 to -1.4E-45

Positive range:

1.4E-45 to 3.4028235E+38

double Negative range: 64-bit IEEE 754

-1.7976931348623157E+308 to

-4.9E-324

Positive range:

4.9E-324 to 1.7976931348623157E+308

Page 11: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

11

ตวด าเนนการส าหรบตวเลข (Numeric Operators)

Name Meaning Example Result

+ Addition 34 + 1 35

- Subtraction 34.0 – 0.1 33.9

* Multiplication 300 * 30 9000

/ Division 1.0 / 2.0 0.5

% Remainder 20 % 3 2

Page 12: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

12

การหารจ านวนเตม (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)

Page 13: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

13

การหาเศษ (Remainder Operator) เศษจากการหารเปนเรองส าคญส าหรบการเขยนโปรแกรม เชน - ถาเราจะตรวจสอบวา ตวเลขเปนเลขคหรอปลาว เราสามารถท าไดโดยดวา เศษเหลอจากการ

หาร 2 แลวได 0 หรอปลาว (เศษเหลอจากการเอาเลขคหาร 2 จะได 1 เสมอ) - สมมตวา วนนเปนวนเสารแลวเราอยากรวาอก 10 วนเปนวนอะไร เราสามารถหาได

โดยใชนพจนดานลางน:

Saturday is the 6th day in a week

A week has 7 days

After 10 days

The 2nd day in a week is Tuesday (6 + 10) % 7 is 2

Page 14: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

14

Exercise: Displaying Time เขยนนพจนหา ชวโมง กบ นาท จากจ านวนวนาท

Page 15: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

15

Literals ตวเลข (Number Literals)

literal เปนคาคงททปรากฏขนในโปรแกรมโดยตรง (ตวอกขระทไมใชตวแปร ไมใชค าสง ไมใช operators) ดงตวอยางดานลาง 34, 1,000,000, and 5.0 เปน literal

i, x, d เปนชอตวแปร

int, long, double เปนชนดของตวแปร

= เปน operator ส าหรบการก าหนดคา

int i = 34;

long x = 1000000;

double d = 5.0;

Page 16: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

16

Literal เลขจ านวนเตม (Integer Literals) - Literal เลขจ านวนเตมจะถกก าหนดใหกบตวแปรทประกาศไวรองรบเลขจ านวนเตมเทานน (นน

คอ ชนด byte, short, int, long ขนอยกบขนาดของตวเลข)

- จะเกดขอผดพลาดเมอ compile ถาเราก าหนดตวแปรโดยใช literal ทใหญกวาหรอเลกกวาคาทตวแปรจะรบได เชน byte b = 1000 จะท าใหเกด compile error, เพราะ 1000 ใหญเกนกวาตวแปรชนด byte จะเกบมนได

- ชวงทเปนไปไดของคาทจะถกเกบอยในตวแปรชนด int คอ ตงแต -231

(-2147483648) ถง 231–1 (2147483647)

- ใน Java, Java จะให literal ทเปนจ านวนเตมเปนชนด int หมด

- ถาเราอยากจะเขยน literal เลขจ านวนเตม แตจะให Java มองเปนชนด

long เราตองใสตวอกษร L หรอ l ตามหลง literal นน (L ดกวาเพราะเราจะไดไมสบสน l กบเลข 1)

Page 17: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

17

Literal ทศนยม (Floating-Point Literals)

- ตวอยางการเขยน literal ทศนยม เชน 10.5

- โดยปกต, Java จะให literal ทศนยม เปนชนด double

เชน 10.5 เปนชนด double ไมใชชนด float

- แตเราสามารถก าหนดให literal เปนชนด float ไดโดยการเตม f หรอ F ตอทาย, หรอใหเปนชนด double ไดโดยการเตม d หรอ D ตอทาย เชน 100.2f หรอ 100.2F เปนทศนยมชนด float, และ 100.2d หรอ 100.2D เปนทศนยมชนด double

Page 18: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

18

ตวเลขทางวทยาศาสตร (Scientific Notation)

Literal ทศนยมสามารถถกเขยนในรปแบบตวเลขทางวทยาศาสตรไดเชน

• 1.23456e+2 หรอ 1.23456e2 หรอ 123.456 มคาเทากน

• 1.23456e-2 เทากบ 0.0123456

• E (หรอ e) แทนการยกก าลง 10

Page 19: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

19

วธการค านวณคานพจน (Expression

Evaluation)

Java จะมวธค านวณ expression ของตวเอง แตผลลพธกจะเหมอนกบผลลพธทาง arithmetic ดงนนเราใชวธการค านวณทางคณตศาสตรเพอหาคานพจนของ Java ได

3 + 4 * 4 + 5 * (4 + 3) - 1

3 + 4 * 4 + 5 * 7 – 1

3 + 16 + 5 * 7 – 1

3 + 16 + 35 – 1

19 + 35 – 1

54 - 1

53

(1) inside parentheses first

(2) multiplication

(3) multiplication

(4) addition

(6) subtraction

(5) addition

Page 20: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

20

Exercise: แปลงอณหภม

จงเขยนโปรแกรมเพอแปลง องศา Fahrenheit ไปเปน Celsius โดยใชสตรตอไปน:

)32)((95 fahrenheitcelsius

Page 21: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

21

ตวด าเนนการการก าหนดคาอยางยอ (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

Page 22: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

22

Increment and Decrement Operators

Operator ชอ ค าอธบาย

++var preincrement เพมคาตวแปร var ไปหนง แลวตคานพจนเทากบคาใหมท

ได

var++ postincrement จะตคานพจนนเทากบคาแรกของตวแปร var จากนนจะเพม

คาของตวแปร var ขนหนง

--var predecrement ลดคาตวแปร var ไปหนง แลวตคานพจนเทากบคาใหมท

ได

var-- postdecrement จะตคานพจนนเทากบคาแรกของตวแปร var จากนนจะลด คาของตวแปร var ลงไปหนงคา

Page 23: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

23

Increment and Decrement Operators, cont.

int i = 10;

int newNum = 10 * i++;

int newNum = 10 * i;

i = i + 1;

Same effect as

int i = 10;

int newNum = 10 * (++i);

i = i + 1;

int newNum = 10 * i;

Same effect as

Page 24: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

24

การแปลงชนดของตวเลข (Numeric Type

Conversion) พจารณา statement ตอไปน:-

byte i = 100;

long k = i * 3 + 4;

double d = i * 3.1 + k / 2;

Page 25: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

25

กฎการแปลง (Conversion Rules)

เมอเราใชตวด าเนนการแบบ binary (มตวถกด าเนนการ 2 ตว) กบตวถกด าเนนการทมชนดตางกน, Java จะแปลงชนดของตวถกด าเนนการโดยอตโนมตตามกฎตอไปน:

1. ถาตวถกด าเนนการตวใดตวหนงเปนชนด double, อกตวกจะถกแปลงเปน

double 2. ถาไมใชขอขางตน, ถาตวถกด าเนนการตวใดตวหนงเปนชนด float, อกตวกจะ

ถกแปลงเปน float 3. ถาไมใชขอขางตน, ถาตวถกด าเนนการตวใดตวหนงเปนชนด long, อกตวกจะ

ถกแปลงเปน long 4. ถาไมใชขอขางตน, ตวถกด าเนนการทงสองจะถกแปลงเปน int

Page 26: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

26

Type Casting

การ cast โดยปรยาย (Implicit casting) double d = 3; (แปลงชนดใหใหญขน จาก literal ชนด int ไปเปนชนด double กอน แลวคอยไปก าหนดคาในตว แปร d)

การ cast โดยโปรแกรมเมอรตงใจ (Explicit casting) int i = (int)3.0; (แปลงชนดใหเลกลง จาก literal ชนด double เปนชนด int)

int i = (int)3.9; (สวนทเปนทศนยมจะถกตดไป)

What is wrong? int x = 5 / 2.0;

byte, short, int, long, float, double

range increases

Page 27: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

27

ชนดขอมลตวอกขระ (Character Data Type)

char letter = 'A'; (ASCII)

char numChar = '4'; (ASCII)

char letter = '\u0041'; (Unicode)

char numChar = '\u0034'; (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 = 'a';

System.out.println(++ch);

Page 28: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

28

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

Page 29: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

29

ASCII Character Set

ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

Page 30: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

30

ASCII Character Set, cont.

ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

Page 31: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

31

Casting ระหวาง ชนด char และ ชนดตวเลข

int i = 'a'; // เหมอนกบ int i = (int)'a';

char c = 97; // เหมอนกบ char c = (char)97;

Page 32: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

32

ชนดสายอกขระ (String Type)

ตวแปรชนด char เกบขอมลไดแคหนงตวอกขระ ถาเราจะเกบขอมลทเปนสายอกขระเราตองใชตวแปรชนด String. เชน,

String message = "Welcome to Java";

Page 33: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

33

การตอสายอกขระ (String Concatenation)

// น าสายอกขระ 3 สายมาตอกน

String message = "Welcome " + "to " + "Java";

// น าสายอกขระ Chapter มาตอดวยเลข 2

String s = "Chapter" + 2; // s กลายเปน Chapter2

// สายอกขระ Supplement มาตอดวยอกขระ B

String s1 = "Supplement" + 'B'; // s1 กลายเปน SupplementB

Page 34: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

34

การรบ input จาก keyboard โดยใช Scanner

1. สราง object Scanner

Scanner scanner = new Scanner(System.in);

2. เรยกใช methods next(), nextByte(), nextShort(), nextInt(),

nextLong(), nextFloat(), nextDouble(), or nextBoolean() เพอรบคา string, byte, short, int, long, float, double, หรอ boolean

เชน,

System.out.print("Enter a double value: ");

Scanner scanner = new Scanner(System.in);

double d = scanner.nextDouble();

TestScanner

Page 35: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

35

Trace ComputeChange

int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount;

1156 remainingAmount

remainingAmount initialized

Suppose amount is 11.56

Page 36: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

36

Trace ComputeChange

int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount;

1156 remainingAmount

Suppose amount is 11.56

11 numberOfOneDollars

numberOfOneDollars assigned

Page 37: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

37

Trace ComputeChange

int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount;

56 remainingAmount

Suppose amount is 11.56

11 numberOfOneDollars

remainingAmount updated

Page 38: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

38

Trace ComputeChange

int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount;

56 remainingAmount

Suppose amount is 11.56

11 numberOfOneDollars

2 numberOfOneQuarters

numberOfOneQuarters assigned

Page 39: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

39

Trace ComputeChange

int remainingAmount = (int)(amount * 100); // Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; // Find the number of quarters in the remaining amount int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; // Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // Find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount;

6 remainingAmount

Suppose amount is 11.56

11 numberOfOneDollars

2 numberOfQuarters

remainingAmount updated

Page 40: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

40

การแปลง String ใหเปน Integer

เพอทจะแปลงคาชนด String ใหเปนคาชนด int, เราสามารถใช

method parseInt ของ class Integer ไดดงน:

int intValue = Integer.parseInt(“123”);

Page 41: Primitive data types and operationsnutthanon/886201/Slide2.pdf · final int SIZE = 3; 10 ชนิดของข้อมูลชนิดตัวเลข (Numerical Data Types)

41

การแปลง String ใหเปน Double

เพอทจะแปลงคาชนด string ใหเปนคาชนด double, เราสามารถใช method parseDouble ของ class Double ไดดงน:

double doubleValue =Double.parseDouble(“123.45”);