ภาษาจาวาเบื้องต้น (introduction to...

Post on 20-Jul-2020

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

OOP 1

ปกติ 2/2548

Lecture 3:

พื้นฐานการเขียนโปรแกรมภาษาจาวา II

(Java Programming Basics, II)

OOP 2

เนื้อหา

กฎเกณฑพื้นฐาน

กฎการตั้งชื่อ

ตัวแปร และชนิดขอมลูตัวดาํเนินการ

OOP 3

กฎเกณฑพื้นฐาน

จาวาเปนภาษาแบบไมกําหนดรูปแบบการจัดตําแหนง (free-form layout)

จาวาเปนภาษาที่แยกแยะตัวพิมพเล็กและตัวพิมพใหญ case sensitive

เชน HELLO, hello, Hello ทั้งหมดนี้ถือวาไมเหมือนกัน

statement (ประโยคคําสั่ง) ในภาษาจาวาตองจบดวยเครื่องหมาย ; (semicolon) เสมอ

OOP 4

กฎการตั้งชื่อ

identifier คอืชื่อของสิ่งตางๆ ที่โปรแกรมเมอรกําหนดขึ้นมาใชในโปรแกรม เชน ชื่อของ คลาส เมทอด และ ตัวแปร

กฎการตั้งชื่อ (การกําหนด identifier) มีดังนี้

มีความยาวไมจํากัด

ประกอบดวยตัวอักษร, ตัวเลข, underscore (_) หรือ dollar sign ($)

OOP 5

กฎการตั้งชื่อ (ตอ)

ขึ้นตนดวยตัวอักษร, _ หรือ $

ประกอบดวยชองวางไมได

ตองไมซ้ํากับ keywords, คาบูลีน (true, false) หรือ

reserved words (null)

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

OOP 7

ตัวอยาง identifier

Hello World

null

_Hello

1Number

Number1

OOP 8

ตัวแปร (Variables)

ตัวแปรคือชื่อของพื้นที่ในหนวยความจําที่ใชเก็บขอมลู

ตัวแปร ประกอบดวย

ชนิดขอมูล (data type)

ชื่อ (name)

คา (value)

OOP 9

ชนิดขอมูลพื้นฐาน (Primitive Data Types)

ชนิด

ขอมลู

ขนาด คาต่ําสดุ คาสูงสุด

byte 8 บิต -128

-32768

-2147483648

-9223372036854775808

127

short 16 บิต 327687

int 32 บิต 2147483647

long 64 บิต 9223372036854775807

ชนิดขอมลูประเภทเลขจํานวนเต็ม (integer)

OOP 10

ชนิดขอมูลพื้นฐาน (ตอ)

ชนิด

ขอมลู

ขนาด คาต่ําสดุ คาสูงสุด

float 32 บิต -3.40282e+38

-1.79769e+308

3.40282e+38

double 64 บิต 1.79769e+308

ชนิดขอมลูประเภทเลขจํานวนจริง (real number)

OOP 11

ชนิดขอมูลพื้นฐาน (ตอ)

ชนิดขอมลูประเภทอื่นๆ (other types)

ชนิดขอมูล ขนาด ขอมลู

char 16 bits ตัวอักขระตามรหัส Unicode

true หรือ falseboolean

OOP 12

การประกาศตัวแปร (Variable Declaration)

รูปแบบ<data type> <name>;

หลักการตั้งชื่อตัวแปร

ชื่อตัวแปรควรขึ้นตนดวยตัวพิมพเล็ก

ถาชื่อตัวแปรมีมากกวาหนึ่งคํา แตละคําที่ตามหลังคําแรกใหขึ้นตนดวยตัวพิมพใหญ เชน isVisible

ควรตั้งชื่อใหสื่อความหมาย

OOP 13

การกําหนดคาตัวแปร (Variable Assignment)

รปูแบบ

<variable name> = <expression>;

OOP 14

การกําหนดคาตัวแปร (ตอ)

การประกาศและการกําหนดคาตวัแปรสามารถรวมไวใน

statement เดียวได

float f = 3.5F; // 3.5f

OOP 15

คาคงที่ (Constant)

คาคงที่ (constant) คือ ตัวแปรที่เก็บคาซึ่งไมสามารถเปลี่ยนแปลงได

เชน

final double SALES_TAX_RATE =7.5;

หลักการกําหนดชื่อคาคงที่

ควรใชตัวเปนใหญทั้งหมด

ใชเครื่องหมาย underscore ( _ ) เพื่อแยกคํา

OOP 16

ตัวดําเนินการทางคณิตศาสตร

(Arithmetic Operators)

โอเปอเรเตอร

(operator)

ความหมาย ตัวอยาง ผลลัพธ

11 + 2

11 - 2

11 * 2

11 / 2

11 % 2

+ บวก

- ลบ

คูณ

หาร

หารเอาเศษ

*

/

%

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;

OOP 18

ลําดับความสําคัญของตัวดําเนินการ

ลําดับความสําคัญ โอเปอเรเตอร

สูงสุด ()

-, +

*, /, %

ต่ําสุด +, -

OOP 19

การเปลี่ยนชนิดขอมูล (Type Casting)

รปูแบบ(<data type>) <expression>;

ตัวอยาง

float a = 1.0F,b = 2.5F;int c;c = (int) (a + b);

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;

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);}

}

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);

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));}

}

top related