งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

ตัวแปรและการคำนวณ Variables and Calculation

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ตัวแปรและการคำนวณ Variables and Calculation"— ใบสำเนางานนำเสนอ:

1 ตัวแปรและการคำนวณ Variables and Calculation
chapter02

2 ห้ามนำไปตั้งเป็นชื่อตัวแปร ค่าคงที่
Java keywords ห้ามนำไปตั้งเป็นชื่อตัวแปร ค่าคงที่

3 ในที่นี้ใช้เก็บ attribute / state ของ object
ตัวแปร variable ในที่นี้ใช้เก็บ attribute / state ของ object เพื่อเก็บค่าในหน่วยความจำสำหรับประมวลผลระหว่าง โปรแกรมทำงาน ตัวแปรจะต้องมีการระบุชนิดของข้อมูลที่ต้องการเก็บ (ประเภทข้อมูล) เพื่อการจองเนื้อที่ในหน่วยความจำให้เหมาะสม

4 ประกาศ Variable Declaration กำหนดค่า Variable Initialization
การสร้างตัวแปร ประกาศ Variable Declaration กำหนดค่า Variable Initialization ประกาศ รวม กำหนดค่า ในคราวเดียวกัน combination of declaration and intialization

5 Variable Declaration:
int a,b,c; float pi; double d; char a;

6 Variable Initialization
pi =3.14f; do =20.22 d; a=’v’;

7 combine variable declaration and initialization
int a=2,b=4,c=6; float pi=3.14f; double do=20.22d; char a=’v’;

8 ประเภทตัวแปร Types of variables
In Java, there are three types of variables: Local Variables Instance Variables Static Variables

9 1) Local Variables เป็นตัวแปรที่ประกาศไว้ภายใน method รวมถึงตัวแปรที่เป็น parameter 2) Instance Variables เป็นตัวแปรที่ประกาศโดยห้ามใช้คีย์เวิร์ด STATIC นำหน้า และต้องประกาศอยู่นอก method อาจเรียกตัวแปรประเภทนี้ว่า instance variables หรือ object variables

10 3) Static Variables เป็นตัวแปรที่กำหนดค่าแค่ครั้งเดียวตอนโปรแกรมเริ่มทำงาน (กำหนดค่าก่อน instance variable ตัวใดๆ)

11 Example: Types of Variables in Java
class TypeOfVariable { int data = 99; //instance variable static int a = 1; //static variable void method() { int b = 90; //local variable }

12 จากคลาส Dog ตัวแปรที่ประกาศเป็น attribute เป็นประเภทใด?

13 Non-primitive Data Types

14 DATA TYPES MAP

15 เป็นชนิดข้อมูลที่กำหนดจากจาวามาล่วงหน้าแล้ว และเก็บค่าโดยตรง
Primitive Data Types เป็นชนิดข้อมูลที่กำหนดจากจาวามาล่วงหน้าแล้ว และเก็บค่าโดยตรง มี 8 ชนิดคือ: byte, short, int, long, char, float, double, boolean

16 Integer byte (1 byte) short (2 bytes) int (4 bytes) long (8 bytes)

17 char Boolean boolean (1 byte) (true/false) float (4 bytes)
Floating float (4 bytes) double (8 bytes) char char (2 bytes) Boolean boolean (1 byte) (true/false)

18 สรุป primitive

19 ค่าตัวเลขทุกตัวเป็นแบบมีเครื่องหมาย(+/-).
Points to Remember: ค่าตัวเลขทุกตัวเป็นแบบมีเครื่องหมาย(+/-). ขนาดของจะเท่ากันทุก platforms (standardized) char data type จะใช้รหัส UNICODE character set

20 การเปลี่ยนชนิดข้อมูล type conversion
Case 1) เปลี่ยนจากขนาดเล็กไปสู่ชนิดที่ใหญ่กว่า. โดยปกติจะถูกทำโดยอัตโนมัตเรียกว่า Conversion

21 Case 2) เปลี่ยนจากขนาดใหญ่ไปสู่ขนาดเล็ก ซึ่งโปรแกรมเมอร์ต้องกำหนดชนิด(type cast operator)ที่ต้องการแปลงเอง เรียกกระบวนการนี้ว่า Type Casting

22 workshop Type Casting typecasting.txt

23 Primitive wrapper class
Wrapper class มีความสามารถเปลี่ยนชนิดข้อมูลจากprimitive เป็น object หรือจาก object เป็น primitive. primitive  object = autoboxing object  primitive = unboxing โดยในแต่ละคลาสจะมีเมธอดในการทำงานที่แตกต่างกันออกไป โดยส่วนมากแล้วเมธอดเหล่านั้นจะใช้กับข้อมูลทีเป็น string เป็นส่วนใหญ่

24 สรุป Primitive wrapper class

25 Primitive to Wrapper public class WrapperExample1{ public static void main(String args[]){ //Converting int into Integer int a=20; Integer i=Integer.valueOf(a);//converting int into Integer Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally System.out.println(a+" "+i+" "+j); }} Output:

26 Wrapper to Primitive public class WrapperExample2{ public static void main(String args[]){ //Converting Integer to int Integer a=new Integer(3); int i=a.intValue();//converting Integer to int int j=a;//unboxing, now compiler will write a.intValue() internally System.out.println(a+" "+i+" "+j); }} Output: 3 3 3

27 public class PrimitiveTypeClass { public static void main (String[] args) { // Convert from string System.out.println("Covert string to int = " + Integer.parseInt("15")); System.out.println("Covert string to short = " + Short.parseShort("15")); System.out.println("Covert string to long = " + Long.parseLong("15")); System.out.println("Covert string to float = " + Float.parseFloat("11.54f")); System.out.println("Covert string to double = " + Double.parseDouble("11.54")); // Convert to string System.out.println("Covert int to string = " + Integer.toString(10)); System.out.println("Covert int to base 16 = " + Integer.toHexString(10)); System.out.println("Covert int to base 8 = " + Integer.toOctalString(10)); System.out.println("Covert int to base 2 = " + Integer.toBinaryString(10)); }

28 ควรตั้งชื่อเป็นอักษรใหญ่ทั้งหมด
Java Constants ควรตั้งชื่อเป็นอักษรใหญ่ทั้งหมด ควรมีคีย์เวิร์ด final และ static นำหน้า public class anyClass { static final int SIMPLE = 0, ONE_SPRING = 1, TWO_SPRING = 2; ... }

29 enum as constant สามารถประกาศ enum constant type ภายในหรือภายนอก class , interfaces.

30

31 special sequence characters

32 Non primitive data types
เป็นชนิดของข้อมูลที่ใช้เก็บตำแหน่งของข้อมูล primitive ในหน่วยความจำที่เก็บแบบความซับซ้อน ใช้สำหรับสร้างตัวแปรที่เรียกว่า ตัวแปรอ้างอิง reference variable มี 3 ประเภทคือ Class ข้อมูลชนิดนี้มักใช้ชี้ object ในหน่วยความจำที่เกิดจากคลาส Interface ข้อมูลชนิดนี้มักใช้ชี้ object ในหน่วยความจำที่เกิดจากอินเทอร์เฟส Array ข้อมูลชนิดนี้มักใช้ชี้ชุดของข้อมูล primitive ที่เรียกว่า อาร์เรย์ array

33 Calculation and expression
expression หมายถึงวลีที่บ่งบอกถึงการประมวลผลที่ต้องใช้ค่าข้อมูล ตัวแปร มากระทำกันด้วยเครื่องหมายคำนวณทางคณิตศาสตร์(arithematic operator) ทางความสัมพันธ์(relation operator) ทางตรรกะ(logic operator) หรือเป็นส่วนหนึ่งของการเรียก method สุดท้าย expression จะถูกประมวลผลได้ค่าๆหนึ่ง เช่น (a * 2), pi + (10 * 2) , 5% 3

34 คำสั่ง Statement Statements เป็นคำสั่งที่สมบูรณ์ อาจประกอบด้วยหลายๆ expression ต้องจบด้วย semi colon ; Expression อาจเป็นคำสั่งได้โดยเติม semi colon เช่น b + (a * 2);

35 เครื่องหมายคำนวณ Operators

36 Precedence of Expression Operators
เช่น x = * * 4; // จะทำ คูณ ก่อนจากซ้ายไปขวาได้ผลลัพธ์ ได้ผลลัพธ์ของ 8*9 = 72 และ 6*4 = 24. ซึ่งในที่สุดexpression จะเหลือการคำนวณดังนี้ x = ; ได้ผลลัพธ์เท่ากับ x=104;

37

38 Formatting with printf()
Link to java format with printf()

39 สรุปการใช้ 3 step process - 1) ประกาศตัวแปร Array 2) สร้าง Array

40 ต.ย ประกาศตัวแปร Array <elementType>[] <arrayName>;
int intArray[]; int [] intArray;

41 ต.ย สร้าง Array arrayname = new dataType[];
intArray = new int[10]; //ประกาศอาร์ int 10 อีเลเมนต์ Declaration and Construction combined int intArray[] = new int[10];

42 กำหนดค่าให้ Array [] = {}; int intArray[] = {1, 2, 3, 4};

43 class ArrayDemo{ public static void main(String args[]){ int array[] = new int[7]; for (int count=0;count<7;count++){ array[count]=count+1; } System.out.println("array["+count+"] = "+array[count]); //System.out.println("Length of Array = "+array.length); // array[8] =10;

44 Multidimensional arrays
int twoD[ ][ ] = new int[4][5] ; public class MultiArray { public static void main(String[] args) { // Create 2-dimensional array. int[][] twoD = new int[4][4]; // Assign three elements in it. twoD[0][0] = 1; twoD[1][1] = 2; twoD[3][2] = 3; System.out.print(twoD[0][0] + " "); }

45 การทำงานของ method กับ attribute
Class Atrribute1 Atrribute3 Atrribute2 Method1() Method3() Method2()

46 ลักษณะคำสั่งที่ใช้ในคลาส
class คำสั่งประกาศตัวแปรเพื่อเก็บ state เท่านั้น Attribute /state Method/ action ได้ทุกคำสั่ง

47 Workshop for array Arraytest1.java


ดาวน์โหลด ppt ตัวแปรและการคำนวณ Variables and Calculation

งานนำเสนอที่คล้ายกัน


Ads by Google