1 / 65

CS@KKU Java Summer Camp 2011

CS@KKU Java Summer Camp 2011. Day 1-2 Variable, Type Expressions and Flow Control. Wachirawut Thamviset. จุดประสงค์การเรียน. สามารถใช้ หมายเหตุแบบต่างๆ ในภาษาจาวาได้ ทราบกฎเกณฑ์การตั้งชื่อ Identifier ทราบถึงชื่อที่เป็น Keyword รู้ความแตกต่างระหว่าง primitive type และ reference type.

rupert
Download Presentation

CS@KKU Java Summer Camp 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS@KKUJava Summer Camp 2011 Day 1-2 Variable, Type Expressions and Flow Control Wachirawut Thamviset

  2. จุดประสงค์การเรียน • สามารถใช้ หมายเหตุแบบต่างๆ ในภาษาจาวาได้ • ทราบกฎเกณฑ์การตั้งชื่อ Identifier • ทราบถึงชื่อที่เป็น Keyword • รู้ความแตกต่างระหว่าง primitive type และ reference type

  3. Primitive Types • ใน Java จะมีชิดข้อมูลแบบ Primitive อยู่ 8 ชนิดได้แก่ • ตรรก (Logical) - boolean (มีค่าเฉพาะ true หรือ false) • ตัวอักขระ (1 ตัว) - char (ASCII code และ Unicode) • จำนวนเต็ม - byte, short, int และ long • ทศนิยม - double, float

  4. Premitive Data Type

  5. Logical - boolean • ตัวแปรแบบ boolean สามารถมีค่าได้ 2 สถานะคือ true และ false • ตัวอย่างการประกาศ boolean flag = true; • เราสามารถใช้คำสั่งเปรียบเทียบเพื่อสร้างค่า boolean ก็ได้ เช่น boolean flag = 5 < 6;

  6. Char • ตัวอักษรของ Java จะมีขนาด 16 bit (Unicode) • การกำหนดค่าจะอยู่ในเครื่องหมายคำพูดเดียว ‘ ‘ • รูปแบบการใช้ • ‘a’ ตัวอักษร a • ‘\t’ หมายถึง tab • ‘\u0E01 เป็นรหัส unicode โดยใช้เลขฐาน 16 จำนวน 4 ตัวดังตัวอย่าง 0E01 แทน รหัสตัวอักษร ก.ไก่

  7. Java String • ไม่ใช่ primitive type , แต่ string เป็น class • แทนค่าใช้เครื่องหมาย คำพูดคู่ “ “ • ตัวอย่างการใช้ • String name = “John”; • String text = “Hello !!\n John”;

  8. การจัดการ String การจัดการทั้งหมดจะทำผ่าน method ที่อยู่ใน Object String เช่น trim() ตัด space ซ้ายขวาออก toUpperCase() แปลงเป็นตัวใหญ่ toLowerCase() แปลงเป็นตัวพิมพ์เล็ก toCharArray() แปลงเป็น array ของตัวอักษร (เมทอดอื่นๆ สามารถดูได้ใน JDK Document ) String s1 = “Hello”.toLowerCase(); String s2 = s1.toUpperCase(); s1 -> “hello” s2 -> “HELLO”

  9. String • เราไม่สามารถแก้ไขค่าของ String โดยตรง • ถ้าต้องการเปลี่ยนค่าของ String ทำได้โดยสร้าง String ใหม่เท่านั้น เช่น String s = “Hello”; ถ้าต้องการเปลี่ยนให้เป็นตัวอักษรใหญ่ต้องใช้วิธี s = s.toUpperCase();

  10. Object to String ใน จาวาทุก Object จะมีเมทอดที่ชื่อว่า toString() ดังนั้นเราจึง สามารถแปลงข้อมูลของออบเจคต์ทุกชนิดเป็น String ได้เช่น Font f = newFont("Arial",Font.BOLD,20); String fname = f.toString(); fnameจะมีค่าเป็นข้อความ java.awt.Font[family=Arial,name=Arial,style=bold,size=20]

  11. การเชื่อม String String sx = 100 + 10.45; // ผิด String sy = 100+ 10.45+”$”; // 110.45$ String sz = “$”+100+ 10.45; // $10010.45 ถ้า String + กับนิพจน์ นิพนธ์นั้นจะถูกแปลงให้เป็น String ก่อนแล้วเชื่อมกัน 100+10.45+”$” => 110.45+”$” => “110.45”+”$” “$”+100+10.45 => “$”+”100”+10.45 => “$100”+”10.45”

  12. เลขจำนวนเต็ม • มี 4 ระดับ • byte 8 bit • short 16 bit • int 32 bit • long 64 bit

  13. เลขจำนวนเต็ม • มีการแทนค่าได้ 3 รูปแบบ (ฐานสิบ ฐานแปด และ ฐาน 16) • 77 เลข 77 เป็นเลขฐาน 10 • 077 เป็นเลขฐาน 8 มีค่าเท่ากับ 63 ในฐาน 10 • 0x77 เป็นเลขฐาน 16 มีค่าเท่ากับ 119 ในฐาน 10 • ค่าปกติจะเป็น integer • เติมตัวอักษร L หรือ l (แอลเล็ก) ต่อท้ายหมายถึงชนิด long • 77L แนะนำให้ใช้ L เพราะ l(แอล) จะมีรูปร่างเหมือนเลข 1(หนึ่ง)

  14. ทศนิยม • มี 2 ชนิดคือ • float 32 bit • double 64 bit

  15. ทศนิยม • การแทนค่า • ใช้ E กำหนดค่า 10 ยกกำลัง • ใช้ F ต่อท้ายหมายถึง float • ใช้ D ต่อท้ายหมายถึง double (ถ้าไม่กำหนดจะเป็น double) • ตัวอย่าง • 3.14 • 3.14E20 หมายถึง 3.14 x 1020 • 1.2E+30D หมายถึง 1.2 x 1030 • 1.2F หมายถึง 1.2 เป็นแบบ float

  16. Reference Type • ตัวแปรในภาษาจาวาที่ไม่ใช่ Primitive Type จะเป็น Reference Type • ใช้จัดการกับ Object • เช่น Foo a = new Foo(10); Foo b = a; // ให้ b อ้างถึง object เดียวกับ a a.setX(20); หรือ b.setX(20) จะมีผลเหมือนกัน เพราะว่า ทั้ง a และ b อ้างถึง Object เดียวกัน

  17. The this Reference • การใช้คำ this เพื่ออ้างถึงสมาชิกของ Class ที่อยู่ในตำแหน่งที่เขียนคำสั่ง • ใช้เพื่ออ้างตัวแปรเมื่อ มีตัวแปรที่มีชื่อซ้ำกัน • เช่น • class Foo{ int x; void setX(int x){ this.x = x; }} this.x

  18. Identifiers • หมายถึง ชื่อ ที่ใช้ตั้ง ชื่อตัวแปร,class หรือ method • สามารถเริ่มต้นด้วยเครื่องหมาย _ หรือ $ • สามารถตั้งชื่อด้วยตัวอักษร Unicode • เป็น Case-Sensitive และ ไม่จำกัดความยาว • ตัวอย่างexample , username , UserName, FirstName, $money

  19. Identifiers • คุณสามารถตั้งชื่อ Class,ตัวแปร และ method ด้วยภาษาไทยได้ • แต่ไม่ควรใช้ภาษาไทยตั้งชื่อ Class เพราะจะทำให้ไม่สามารถใช้ บน OS ที่ไม่สนับสนุนภาษาไทย • การตั้งชื่อ Class ควรขึ้นต้นด้วยตัวอักษรใหญ่ • ชื่อ Method และ ตัวแปร ควรขึ้นต้นด้วยอักษรเล็ก • Identifiers ไม่สามารถตั้งชื่อเหมือนกับ Keyword ของจาวาได้

  20. Identifier • ไม่สามารถใช้สัญลักษณ์พิเศษอื่น เช่น +, -, *, &, %, # ฯลฯ เป็นส่วนประกอบอยู่ภายในชื่อได้ • สามารถตั้ง Identifier ให้มีความยาวเท่าใดก็ได้ • การตั้ง Identifier จะมีลักษณะเป็น Case Sensitive เช่น MyClass, myClass, MYCLASS และ myclass ต่างก็เป็น Identifier ที่แตกต่างกัน ไม่ใช่ตัวเดียวกัน

  21. Java Keywords • เป็นคำเฉพาะ ที่ถูกสงวนไว้ใช้ในภาษา Java • สำหรับใช้นิยามคลาส และ สร้างคำสั่ง • เช่น • abstract , assert, boolean,break,continue • class,const, if , else • this, super, public, private • ไม่สามารถใช้ตั้งชื่อ Indentifier ได้

  22. Java Keywords ไม่สามารถใช้ ตั้งเป็น Identifier ได้ ตัวอย่างเช่น abstract,boolean,break, byte, case, catch, char, class,const, continue, default, delegate, do, double, else, extends,false, final, finally, float, for, goto, if, implements, import,instance, of, int, interface, long, multicast, native, new, null, package, private, protected, public, return, short, static, super, switch, synchronized, this, throw, throws, transient, true, try, void, volatile, while

  23. Scoping ใช้เครื่องหมาย { } กำหนด Scope ตัวแปรจะใช้ได้เฉพาะในช่วงโปรแกรมที่สร้างมันเท่านั้น { int I = 100; { int G = I+10; } System.out.printf(“%d %d”, I , G ); } Scope จะเป็นตัวบอกอายุของตัวแปร เมื่อออกนอก Scope มันก็จะถูกทำลาย มีตัวแปร I,G มีตัวแปร I แต่ตัวแปร G จะหายไป

  24. Expressions and Flow Control

  25. Operator • In addition to the simple operators in JAVA • addition(+) • subtraction(-) • multiplication(*) • division(/) • modulus(%) • There are many others.

  26. Increment and Decrement Operators: • ++ ใช้เพิ่มค่าตัวแปรขึ้น 1 • -- ใช้ลดค่าตัวแปรลง 1 int    aNumber = 8; aNumber = aNumber + 1; aNumber++; aNumber = aNumber – 1; aNumber -- ;

  27. Increment and Decrement Operators: • ++ ใช้เพิ่มค่าตัวแปรขึ้น 1 • -- ใช้ลดค่าตัวแปรลง 1 int    aNumber = 8; aNumber = aNumber + 1; aNumber++; aNumber = aNumber – 1; aNumber -- ; สิ่งที่แตกต่างระหว่าง i++; และ ++i; int x=5,y=5; System.out.println(x++); 5 System.out.println(++y); 6

  28. Logical Operators: JAVA allows for standard comparison between numbers: • <    less than • <=   less than or equal to • ==   equal to • !=   not equal to • >=   greater than or equal to • >    greater than 5 < 7           //returns true23.321 >= 54.1  //returns false'g' != 'G'      //returns true'g' == 'g'      //returns true จะได้ผลลัพธ์เป็น true หรือ false

  29. Logical Operators: Operator ที่ใช้จัดการกับข้อมูลแบบ boolean ได้แก่ • !    not (prefix) • &&  conditional and • ||   conditional or !(5 < 7)                   //returns false('a' == 'a') && (5 < 7)    //returns true('G' == 'a') && (5 < 7)    //returns false('G' == 'a') || (5 < 7)    //returns true('G' == 'a') || (5 >= 7)   //returns false การคำนวณ จากซ้ายไปขวา สำหรับ && ถ้ามีเงื่อนไขใดเป็น false ก็จะไม่จำเป็นตรวจสอบเงื่อนไขถัดไป สำหรับ || ถ้าพบเงื่อนไขที่เป็น true ก็จะไม่ต้องตรวจสอบเงื่อนไขถัดไป

  30. Bitwise Operators: JAVA also provides bitwise operators for integers and booleans: ~ bitwise complement (prefix unary operator) & bitwise and | bitwise or ^ bitwise exclusive-or << shift bits left, filling in with zeros >> shift bits right, filling in with sign bit >>> shift bits right, filling in with zeros

  31. Assignment Operators: • ใช้คำนวณและกำหนดค่าให้ตัวแปร มีรูปแบบดังนี้ • variable <op>= expression; • ตัวอย่าง • x = x+ 6; สามารถเขียนใหม่เป็น x += 6; • y = y * 2; สามารถเขียนใหม่เป็น y *= 2;

  32. Operator Precedence 30 + 5 * 2 - 18 / 2 - 2 30 + 10 – 18 / 2 – 2 30 + 10 – 9 – 2 40 – 9 – 2 31 – 2 29 • ลำดับการคำนวณ operator • สามารถควบคุมด้วย วงเล็บ ( )

  33. ตารางแสดงลำดับของ operator

  34. ตารางแสดงลำดับของ operator (ต่อ)

  35. Math Functions  • There is a whole library of math functions that can be used on numbers.  • Here are just a few on the common ones: Trigonometric: Math.sin(0)          //returns 0.0 which is a double Math.cos(0)          //returns 1.0 Math.tan(0.5)        //returns 0.5463024898437905

  36. Math Functions  (ต่อ) • Conversion and Rounding: • Math.round(6.6)      //returns 7 • Math.round(6.3)      //returns 6 • Math.ceil(9.2)       //returns 10 • Math.ceil(-9.8)      //returns -9 • Math.floor(9.2)      //returns 9 • Math.floor(-9.8)     //returns -10

  37. Math Functions  (ต่อ) Powers and Exponents: Math.sqrt(144) //returns 12.0 Math.pow(5,2) //returns 25.0 Math.exp(2) //returns 7.38905609893065 Math.log(7.38905609893065) //returns 2.0

  38. Math Functions  (ต่อ) Powers and Exponents: Math.sqrt(144) //returns 12.0 Math.pow(5,2) //returns 25.0 Math.exp(2) //returns 7.38905609893065 Math.log(7.38905609893065) //returns 2.0

  39. Math Functions  (ต่อ) Comparison: Math.max(560, 289)   //returns 560 Math.min(560, 289)   //returns 289 Generation of a Random Number: Math.random()    //returns a double >=0.0 and <1.0

  40. Math Functions  (ต่อ) ตัวอย่าง: เราจะสามารถคำนวณจุดทศนิยม 2 หลักได้อย่างไร 5415.23491872773773 => 5415.24 วิธีการอย่างง่าย คูณด้วย 100 แล้ว round จากนั้น หารด้วย 100 double x = 524.14982232132143; System.out.println("x is: " + x); x = Math.round(x * 100) / 100.0; System.out.println("x rounded is: " + x);

  41. Flow Control • Every programming language must have the ability to: • evaluate code in some specific order • evaluate different code depending on some condition • repeat a selected portion of code a specific number or times, or until some condition occurs

  42. Control Structure • sequence executing code line-by-line. • selection being able to choose as to whether or not a piece of code is to be executed based on some condition. • repetitionexecuting the same code fragment more than once.

  43. Control Structure • if • switch • for • while The control structures will be used to handle the selection and repetition.

  44. The if control structure • ใช้ควบคุมการตัดสินใจ

  45. ตัวอย่าง • สร้างฟังก์ชันคำนวณความชัน ระหว่างจุด 2 จุด public static double slope(int  x1, int x2, int y1, int y2) {     if (x1 == x2)         return Double.NaN;     return ((y2- y1) / (x2 - x1)); } หมายเหตุ Double.NaN เป็นค่าคงที่ สำหรับกำหนดว่า หาค่าไม่ได้

  46. ตัวอย่าง • การแสดงผลสามารถใช้ if เพื่อเลือกวิธีการแสดงได้ double slope = slope(1,1,1,4); if (Double.isNaN(slope))     System.out.println("Slope is undefined"); else    System.out.println("Slope is " + slope); หมายเหตุ Double.isNaN ใช้ทดสอบ ตัวเลขที่หาค่าไม่ได้

  47. ตัวอย่าง : if public class PollutionClassifier { public static String classify(int polutionIndex) {         if ((polutionIndex < 0) || (polutionIndex > 100))             return null;         if ((polutionIndex >= 0) && (polutionIndex <= 34))             return "Pleasant";         if ((polutionIndex >= 35) && (polutionIndex <= 60))             return "Unpleasant";         if (polutionIndex > 60)             return "Hazardous";         return null;     } } (ระดับมลภาวะ)

  48. ตัวอย่าง : if (ระดับมลภาวะ) ลดความซับซ้อน public static String classify(int polutionIndex) {     if ((polutionIndex < 0) || (polutionIndex > 100))         return null;     else if (polutionIndex <= 34)         return "Pleasant";     else if (polutionIndex <= 60)         return "Unpleasant";     else        return "Hazardous"; }

  49. ตัวอย่าง : if (ระดับมลภาวะ) ลดความซับซ้อน ในความเป็นจริง การใช้คำสั่ง return ไม่จำเป็นต้องมี else ก็ได้ public class PollutionClassifier { public static String classify(int polutionIndex) {         if ((polutionIndex < 0) || (polutionIndex > 100)) return null;         if (polutionIndex <= 34) return "Pleasant";         if (polutionIndex <= 60) return "Unpleasant";         return "Hazardous";     } }

  50. The switch Statement • ใช้สร้างสำหรับเงื่อนไขที่มีหลายทางเลือก switch (<switch-expression>) { case value1:   statements for case1; break; case value2:   statements for case2; break;     ... case valueN:   statements for caseN; break; default:       statements for default case; break; } ข้อจำกัด : ใช้ได้เฉพาะ expression ที่เป็น premitive type

More Related