ภาษาจาวาเบื้องต้น (introduction to...
TRANSCRIPT
![Page 1: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/1.jpg)
OOP 1
ปกติ 2/2548
Lecture 3:
พื้นฐานการเขียนโปรแกรมภาษาจาวา II
(Java Programming Basics, II)
![Page 2: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/2.jpg)
OOP 2
เนื้อหา
กฎเกณฑพื้นฐาน
กฎการตั้งชื่อ
ตัวแปร และชนิดขอมลูตัวดาํเนินการ
![Page 3: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/3.jpg)
OOP 3
กฎเกณฑพื้นฐาน
จาวาเปนภาษาแบบไมกําหนดรูปแบบการจัดตําแหนง (free-form layout)
จาวาเปนภาษาที่แยกแยะตัวพิมพเล็กและตัวพิมพใหญ case sensitive
เชน HELLO, hello, Hello ทั้งหมดนี้ถือวาไมเหมือนกัน
statement (ประโยคคําสั่ง) ในภาษาจาวาตองจบดวยเครื่องหมาย ; (semicolon) เสมอ
![Page 4: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/4.jpg)
OOP 4
กฎการตั้งชื่อ
identifier คอืชื่อของสิ่งตางๆ ที่โปรแกรมเมอรกําหนดขึ้นมาใชในโปรแกรม เชน ชื่อของ คลาส เมทอด และ ตัวแปร
กฎการตั้งชื่อ (การกําหนด identifier) มีดังนี้
มีความยาวไมจํากัด
ประกอบดวยตัวอักษร, ตัวเลข, underscore (_) หรือ dollar sign ($)
![Page 5: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/5.jpg)
OOP 5
กฎการตั้งชื่อ (ตอ)
ขึ้นตนดวยตัวอักษร, _ หรือ $
ประกอบดวยชองวางไมได
ตองไมซ้ํากับ keywords, คาบูลีน (true, false) หรือ
reserved words (null)
![Page 6: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/6.jpg)
OOP 6
คียเวิรดในภาษาจาวา
(Java Language Keywords)
abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
![Page 7: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/7.jpg)
OOP 7
ตัวอยาง identifier
Hello World
null
_Hello
1Number
Number1
![Page 8: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/8.jpg)
OOP 8
ตัวแปร (Variables)
ตัวแปรคือชื่อของพื้นที่ในหนวยความจําที่ใชเก็บขอมลู
ตัวแปร ประกอบดวย
ชนิดขอมูล (data type)
ชื่อ (name)
คา (value)
![Page 9: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/9.jpg)
OOP 9
ชนิดขอมูลพื้นฐาน (Primitive Data Types)
ชนิด
ขอมลู
ขนาด คาต่ําสดุ คาสูงสุด
byte 8 บิต -128
-32768
-2147483648
-9223372036854775808
127
short 16 บิต 327687
int 32 บิต 2147483647
long 64 บิต 9223372036854775807
ชนิดขอมลูประเภทเลขจํานวนเต็ม (integer)
![Page 10: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/10.jpg)
OOP 10
ชนิดขอมูลพื้นฐาน (ตอ)
ชนิด
ขอมลู
ขนาด คาต่ําสดุ คาสูงสุด
float 32 บิต -3.40282e+38
-1.79769e+308
3.40282e+38
double 64 บิต 1.79769e+308
ชนิดขอมลูประเภทเลขจํานวนจริง (real number)
![Page 11: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/11.jpg)
OOP 11
ชนิดขอมูลพื้นฐาน (ตอ)
ชนิดขอมลูประเภทอื่นๆ (other types)
ชนิดขอมูล ขนาด ขอมลู
char 16 bits ตัวอักขระตามรหัส Unicode
true หรือ falseboolean
![Page 12: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/12.jpg)
OOP 12
การประกาศตัวแปร (Variable Declaration)
รูปแบบ<data type> <name>;
หลักการตั้งชื่อตัวแปร
ชื่อตัวแปรควรขึ้นตนดวยตัวพิมพเล็ก
ถาชื่อตัวแปรมีมากกวาหนึ่งคํา แตละคําที่ตามหลังคําแรกใหขึ้นตนดวยตัวพิมพใหญ เชน isVisible
ควรตั้งชื่อใหสื่อความหมาย
![Page 13: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/13.jpg)
OOP 13
การกําหนดคาตัวแปร (Variable Assignment)
รปูแบบ
<variable name> = <expression>;
![Page 14: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/14.jpg)
OOP 14
การกําหนดคาตัวแปร (ตอ)
การประกาศและการกําหนดคาตวัแปรสามารถรวมไวใน
statement เดียวได
float f = 3.5F; // 3.5f
![Page 15: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/15.jpg)
OOP 15
คาคงที่ (Constant)
คาคงที่ (constant) คือ ตัวแปรที่เก็บคาซึ่งไมสามารถเปลี่ยนแปลงได
เชน
final double SALES_TAX_RATE =7.5;
หลักการกําหนดชื่อคาคงที่
ควรใชตัวเปนใหญทั้งหมด
ใชเครื่องหมาย underscore ( _ ) เพื่อแยกคํา
![Page 16: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/16.jpg)
OOP 16
ตัวดําเนินการทางคณิตศาสตร
(Arithmetic Operators)
โอเปอเรเตอร
(operator)
ความหมาย ตัวอยาง ผลลัพธ
11 + 2
11 - 2
11 * 2
11 / 2
11 % 2
+ บวก
- ลบ
คูณ
หาร
หารเอาเศษ
*
/
%
![Page 17: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/17.jpg)
OOP 17
ตัวดําเนินการกําหนดคา
(Assignment Operators)
โอเปอเรเตอร รูปแบบ ความหมาย
= a = b;
a += b;
a -= b;
a *= b;
a /= b;
a %= b;
+= a = a + b;
-= a = a - b;
*= a = a * b;
/= a = a / b;
%= a = a % b;
![Page 18: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/18.jpg)
OOP 18
ลําดับความสําคัญของตัวดําเนินการ
ลําดับความสําคัญ โอเปอเรเตอร
สูงสุด ()
-, +
*, /, %
ต่ําสุด +, -
![Page 19: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/19.jpg)
OOP 19
การเปลี่ยนชนิดขอมูล (Type Casting)
รปูแบบ(<data type>) <expression>;
ตัวอยาง
float a = 1.0F,b = 2.5F;int c;c = (int) (a + b);
![Page 20: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/20.jpg)
OOP 20
โปรแกรมตัวอยางการใชตัวแปร
public class VariableDemo{public static void main(String[] args){
// declare variables & initialize them
char c = 'A';int i = 1;double d = 2.5;boolean b = true;final double SALES_TAX_RATE = 7.5;
![Page 21: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/21.jpg)
OOP 21
โปรแกรมตัวอยางการใชตัวแปร (ตอ)
// display variable contentsSystem.out.println("c = " + c);System.out.println("i = " + i);System.out.println(“d = " + d);System.out.println("b = " + b);System.out.println("SALES_TAX_RATE =
" + SALES_TAX_RATE);}
}
![Page 22: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/22.jpg)
OOP 22
โปรแกรมตัวอยางการคํานวณ
public class ComputationDemo{public static void main(String[] args){
// illustrate arithmetic operators int a = 11;int b = 2;int c = 4;System.out.println("a = " + a);System.out.println("b = " + b);System.out.println("c = " + c);
![Page 23: ภาษาจาวาเบื้องต้น (Introduction to Java)cit.snru.ac.th/UserFiles/File/pdf/oop/Lec03.pdf · OOP 3 กฎเกณฑ พื้นฐาน จาวาเป](https://reader034.vdocuments.mx/reader034/viewer/2022050213/5f5f6ccbdb97a47231688b69/html5/thumbnails/23.jpg)
OOP 23
โปรแกรมตัวอยางการคํานวณ (ตอ)
System.out.println("a + b = " + (a + b));System.out.println("a - b = " + (a - b));System.out.println("a * b = " + (a * b));System.out.println("integer division a / b = " + (a / b));
// cast a & b to double before divideSystem.out.println("double division a / b = " + (double) a / (double) b);
System.out.println("a % b = " + (a % b));}
}