Transcript Slide 1

ทส213การเขี
ทส213
การเขียยนโปรแกรมเช
นโปรแกรมเชิงวังิ ตวถุัตถุ11
Object-Oriented Programming 1
Language
Fundamentals
ั
อาจารย์อรรถวิท ชงคมานนท์
[email protected]
สาขาวิชาเทคโนโลยีสารสนเทศ
คณะวิทยาศาสตร์
www.itsci.mju.ac.th
The First Java Program
สว่ นนีจ
้ ะเป็ นขอบเขตของ
class Welcome {
comments
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
[email protected]
The First Java Program
class Welcome {
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
ส่ วนนี้จะเป็ นเมธอด main()
ที่ใช้ในการรันโปรแกรม
[email protected]
The First Java Program
class Welcome {
เมธอดอาจรับค่า input จากการ
้ ปของ arguments หรือ
เรียกใชในรู
/* The Welcome Program
parameters
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
[email protected]
The First Java Program
class Welcome {
เมธอดอาจมีการคืนค่า output กลับไป
/* The Welcome Program
ยัง caller ทีเ่ รียกว่า return value
------------------Illustrates a simple program displaying
a message.
*/
}
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
[email protected]
The First Java Program
class Welcome {
คียเ์ วิรด
์ static จะเป็ นตัว
ระบุวา่ นีค
่ อ
ื คลาสเมธอด
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
}
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
[email protected]
The First Java Program
class Welcome {
ิ ธิในการเข ้าถึงข ้อมูลแบบ public
เมธอดนีม
้ ส
ี ท
้ ้โดยตรง
ทีส
่ ามารถเรียกใชได
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
}
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
[email protected]
The First Java Program
class Welcome {
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
}
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
[email protected]
ชุดคาสั่งนี้จะใช้ในการ
แสดงผลลัพธ์
Java First program (2)
public static void main( String args[] )
 ถือเป็ นสว่ นหนึง่ ของจาวาโปรแกรมโดยเฉพาะทีเ่ ป็ น application
 Applications จะเริม
่ ทาการประมวลผลทีต
่ าแหน่ง main เป็ น
ต ้นไป
 วงเล็บจะเป็ นการระบุวา
่ main เป็ นเมธอด
 Java applications จะประกอบไปด ้วยเมธอดตัง
้ แต่ 1 หรือ
มากกว่า
 เมธอดหนึง
่ จะถูกกาหนดขึน
้ และเรียกว่า main
 เมธอดสามารถทางานและคืนค่าได ้ โดย

void จะเป็ นการระบุวา่ ไม่มก
ี ารคืนค่าข ้อมูลใด ๆ
[email protected]
Java First program (3)
System.out.println(“Welcome to Java!");


System.out - เป็ น standard output object
้
เมธอด System.out.println ใชแสดงผลข
้อความทีอ
่ ยู่
ิ้ สุดชุดคาสงั่ ด ้วย
ภายในเครือ
่ งหมายคาพูดและสน
เครือ
่ งหมาย semicolon ;
[email protected]
Program Component: Comment
/*
Program Hello
This program displays a message “Hello Test” on
the screen.
*/
import java.lang.*;
class Hello {
public static void main(String[] args) {
System.out.println("Hello Test");
}
}
[email protected]
Comment
Matching Comment Markers
/* This is a comment on one line */
/*
Comment number 1
*/
/*
Comment number 2
*/
These are part of the
comment.
/*
/*
/*
This is a comment
*/
*/
[email protected]
Error: No matching
beginning marker.
Three Types of Comments
/*
This is a comment
Multiline Comment
with three lines of
text.
*/
// This is a comment
// This is another comment
// This is a third comment
[email protected]
Single line Comments
Component: Import Statement
/*
Program Hello
This program displays a message “Hello Test” on
the screen.
*/
import java.lang.*;
Import Statement
class Hello {
public static void main(String[] args) {
System.out.println("Hello Test");
}
}
[email protected]
Component: Class Declaration
/*
Program Hello
This program displays a message “Hello Test” on
the screen.
*/
Class Declaration
import java.lang.*;
class Hello {
public static void main(String[] args) {
System.out.println("Hello Test");
}
}
[email protected]
Component: Method Declaration
/*
Program Hello
This program displays a message “Hello Test” on
the screen.
*/
import java.lang.*;
Method Declaration
class Hello {
public static void main(String[] args) {
System.out.println("Hello Test");
}
}
[email protected]
Method Declaration Elements
Modifier
Modifier
Return Type
Method Name
Parameter
public static void main(String[] args) {
System.out.println("Hello Test");
}
[email protected]
Method Body
The First Java Program
class Welcome {
/*
* The Welcome Program
*
* Illustrates a simple program displaying a message.
*/
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
[email protected]
JAVA 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
(added in 5.0)
instanceof
return
transient
catch
extends
int
short
try
char
final
interface
static
void
class
finally
long
strictfp**
volatile
const*
float
native
super
while
case
enum****
้
ื่ Class, method, variable
50 keywords ห ้ามใชในการน
าไปตัง้ ชอ
[email protected]
Access Modifiers (3)
 private : Makes a method or a variable accessible only
from within its own class.
 protected : Makes a method or a variable accessible
only to classes in the same package or subclasses of the
class.
 Public : Makes a class, method, or variable accessible
from any other class.
[email protected]
Class, Method & Variable Modifiers (13)
 abstract : Used to declare a class that cannot be
instantiated, or a method that must be implemented by
a nonabstract subclass.
 class : Keyword used to specify a class.
 extends : Used to indicate the superclass that a
subclass is extending.
 final : Makes it impossible to extend a class, override a
method, or reinitialize a variable.
 implements : Used to indicate the interfaces that a
class will implement.
 interface : Keyword used to specify an interface.
 native : Indicates a method is written in a platformdependent language, such as C.
[email protected]
Class, Method & Variable Modifiers (…)
 new : Used to instantiate an object by invoking the
constructor.
 static : Makes a method or a variable belong to a class
as opposed to an instance.
 strictfp : Used in front of a method or class to indicate
that floating-point numbers will follow FP-strict rules in
all expressions.
 synchronized : Indicates that a method can be
accessed by only one thread at a time.
 transient : Prevents fields from ever being serialized.
Transient fields are always skipped when objects are
serialized.
 volatile : Indicates a variable may change out of sync
because it is used in threads.
[email protected]
Flow Control (12)
 break : Exits from the block of code in which it resides.
 case : Executes a block of code, dependent on what
the switch tests for.
 continue : Stops the rest of the code following this
statement from executing in a loop and then begins the
next iteration of the loop.
 default : Executes this block of code if none of the
switch-case statements match.
 do : Executes a block of code one time. It performs a
test to determine whether the block should be executed
again.
 else : Executes an alternate block of code if an if test is
false.
 for : Used to perform a conditional loop for a block of
code.
[email protected]
Flow Control (…)
 if : Used to perform a logical test for true or
false.
 instanceof : Determines whether an object is an
instance of a class, superclass, or interface.
 return : Returns from a method without executing any
code that follows the statement (can optionally return a
variable).
 switch : Indicates the variable to be compared with the
case statements.
 while : Executes a block of code repeatedly while a
certain condition is true.
[email protected]
Error Handling (6)
 catch : Declares the block of code used to handle an
exception.
 finally : Block of code, usually following a try-catch
statement, which is executed no matter what program
flow occurs when dealing with an exception.
 throw : Used to pass an exception up to the method
that called this method.
 throws : Indicates the method will pass an exception
to the method that called it.
 try : Block of code that will be tried, but which may
cause an exception.
 assert : Evaluates a conditional expression to verify the
programmer’s assumption.
[email protected]
Package Control (2)


import : Statement to import packages or classes
into code.
package : Specifies to which package all classes in a
source file belong.
[email protected]
Primitives (8)***
 boolean : A value indicating true or false.
 byte : An 8-bit integer (signed).
 char : A single Unicode character (16-bit unsigned)
 double : A 64-bit floating-point number (signed).
 float : A 32-bit floating-point number (signed).
 int : A 32-bit integer (signed).
 long : A 64-bit integer (signed).
 short : A 16-bit integer (signed).
[email protected]
Variable Keywords (2)
 super : Reference variable referring to the immediate
superclass.
 this : Reference variable referring to the current
instance of an object.
[email protected]
Void Return Type Keyword (1)
 void : Indicates no return type for a method.
[email protected]
Unused Reserved Words (2)
 const : Do not use to declare a constant; use public
static final.
 goto : Not implemented in the Java language. It’s
considered harmful.
[email protected]
VARIABLES &
DECLARATIONS
[email protected]
Variables & Declarations
้ ัวแปรจะต้องทาการประกาศก่อนเสมอ
 ในการใชต
ื่ ของต ัวแปรจะต้องทาตามทีก
ื่ ของจาวา
 ชอ
่ าหนดไว้ในเงือ
่ นไขการตงช
ั้ อ
 โดยสามารถเป็ นตัวอักษร ตัวเลข $ และเครือ
่ งหมาย underscores
 ไม่สามารถเริม
่ ต ้นด ้วยตัวเลข
 โดยปกติจะเริม
่ จากอักษรตัวเล็ก และอักษรตัวใหญ่ต ้นคาถัดไป
 เป็ นแบบ case sensitive
 รูปแบบการประกาศต ัวแปร:
<type> <name>;
 หรือ
<type> <name> = <value>;
[email protected]
Data Types
 Data types: เป็นการระบุให้คอมไพเลอร์ทราบถึงวิธก
ี ารนาเสนอค่า
และสงิ่ ทีส
่ ามารถมากระทาก ับค่านน
ั้ ๆ โดยปกติชนิดของข้อมูลจะมี
สองแบบ คือ
 primitive types ได ้แก่ integer, real, boolean และ character
 user-defined types ได ้แก่ classes ต่างๆ
 Primitive types – ในจาวาจะมีดว้ ยก ัน 8 แบบ คือ:
 Integers: byte, short, int, long
 Real numbers: float, double
 Boolean (logical) values: boolean
 Characters: char
[email protected]
integer types
้ น
้ื ทีใ่ นการจ ัดเก็บค่า 4 bytes ( -231 to 231 - 1 )
 โดยปกติจะใชพ
Type
Storage
byte
short
int
long
1
2
4
8
byte
bytes
bytes
bytes
 ต ัวอย่างการประกาศต ัวแปร
int i;
short rocketFuel;
long angle, magnitude;
byte red, green, blue;
[email protected]
Range
-128 to 127
-32678 to 32767
-2147483648 to 2147483647
-9233372036854775808
to 9233372036854775807
Float Types
 Java Floating Point types
้ าหรับการนาเสนอตัวเลขแบบทศนิยมซงึ่ จะแบ่งออกเป็ นสองแบบ
 ใชส
ย่อย ได ้แก่ float และ double
 float type จะสารองพืน
้ ทีเ่ มมโมรีไ่ ว ้เป็ นจานวน 32-bit สาหรับ
นาเสนอsingle-precision number
 double type จะสารองพืน
้ ทีเ่ มมโมรีไ่ ว ้เป็ นจานวน 64-bit สาหรับ
นาเสนอsingle-precision number reserves storage for a 64-bit
 การประกาศต ัวแปรแบบ floating-point จะเหมือนการประกาศต ัว
่
แปรแบบอืน
่ ๆ ต ัวอย่างเชน
 float temperature;
 double windSpeed, barometricPressure;
[email protected]
Ranges of Primitive Numbers
[email protected]
Character
้ าหร ับนาเสนออ ักขระเดีย
 char: ใชส
่ ว
 การนาเสนอแบบภายใน: เรียกว่า Unicode ทีป
่ ระกอบไปด ้วยอักขระทีไ่ ม่
ซ้ากันเป็ นจานวน 65536 ค่า



ั ลักษณ์อก
่
สญ
ั ขระของ Unicode ทีน
่ าเสนอโดย \u เชน
 char letterH = '\u0048'; // The letter ‘H'
 char letterN = '\u004E'; // The letter 'N‘
อักขระจะถูกนาเสนอภายในเครือ
่ งหมาย single quotation marks สว่ น
อักขระพิเศษประเภท control characters จะไม่สามารถพิมพ์ผา่ น
หน ้าจอได ้
การนาเสนอแบบภายนอก
่ : ‘a’, ‘*’, ‘$’, ‘’
 printable characters เชน
 escape sequences ใชร่้ วมกับอักขระเพือ
่ แสดงความหมายพิเศษ
[email protected]
boolean Data Type
้ าหร ับการเก็บค่าทีม
 ใชส
่ เี พียงสองสถานะ ได้แก่ true หรือ false
้ าจมอง boolean type ในรูปของต ัวเลขทีม
 ผูใ้ ชอ
่ ค
ี า่ เพียงหนึง่ บิต ทีม
่ ี
ค่าทีเ่ ป็นไปได้เพียงสองค่าได้แก่ 1 หรือ 0 อย่างไรก็ตามแทนทีจ
่ ะ
้ ย
แทนค่าด้วย 1 และ 0 จะต้องใชค
ี เ์ วิรด
์ true และ false เพือ
่ ป้องก ัน
้ าชนิดข้อมูลแบบนีไ้ ปใชร้ ว
ไม่ให้ผใู ้ ชน
่ มก ับชนิดของข้อมูลแบบต ัวเลข
ทว่ ั ๆ ไป
้ ย
่
 การประกาศต ัวแปรจะใชค
ี เ์ วิรด
์ boolean เชน
boolean gameOver;
boolean t = true; // Legal
boolean f = 0; // Compiler error!
[email protected]
Integer Literals
การกาหนดค่าต ัวแปลแบบ int สามารถทาได้ 3 รูปแบบ
 Decimal Literals (base 10)
int length = 343;
 Octal Literals (base 8)
int
int
int
int
five = 06; // Equal to decimal 6
seven = 07; // Equal to decimal 7
eight = 010; // Equal to decimal 8
nine = 011; // Equal to decimal 9
 Hexadecimal Literals (base 16) “0 1 2 3 4 5 6 7 8 9 a b c d e f”
int length = 343;
int x = 0X0001;
int y = 0x7fffffff;
int z = 0xDeadCafe;
x = 1;
y = 2147483647;
z [email protected]
= -559035650;
Floating-Point Literals
FLOAT ต้องมี ‘F’ หรือ ‘f’ ต่อท้ายต ัวแปลแบบ float
 float f = 23.467890;
// Compiler error, possible
loss of precision
 float g = 49837849.029847F;
// OK; has the suffix "F"
DOUBLE
 double d = 110599.995011D;
// Optional, not required
 double g = 987.897;
// No 'D' suffix, but OK.
 double d1 = 123.4;
 double d2 = 1.234e2;
// same value as d1, but in scientific notation
[email protected]
Variables
 ข้อมูล (data) จะถูกนาเสนอผ่านต ัวแปร (variables)
ื่ ไว้แทนข้อมูล
 ต ัวแปรจะถูกกาหนดชอ
 ต ัวแปรจะเป็นการอ้างตาแหน่งในเมมโมรีเ่ ป็นหล ัก และข้อมูลใน
ตาแหน่งนนอาจเปลี
ั้
ย
่ นแปลงได้ ด ังนนจึ
ั้ งเรียกว่า variable
ื่ ของต ัวแปรจะต้องเริม
 ชอ
่ ต้นด้วยต ัวอ ักษร (a-z)และเครือ
่ งหมาย $
และ _ ซงึ่ อาจประกอบไปด้วยต ัวเลขได้
ื่ ของต ัวแปรใน Java จะเริม
 โดยเงือ
่ นไข ชอ
่ ต้นด้วยอ ักษรต ัวเล็ก สว่ น
อ ักษรต ัวแรกของคาควรจะเป็นต ัวใหญ่
 ต ัวแปร Variables จะต้องมี type เสมอ
 type อาจอยูใ่ นรูปของ primitive data type หรือ class name
[email protected]
Default Values
Data Type
Default Value (for fields)
byte
0
short
0
int
0
long
0L
float
0.0f
double
0.0d
char
'\u0000'
String (or any object)
null
boolean
false
[email protected]
Declaring Variables
 ทุก ๆ ต ัวแปรทีถ
่ ก
ู นาไปใชใ้ นโปรแกรมจะต้องถูกประกาศไว้กอ
่ นเสมอ
 Syntax:
type name;
่
 ต ัวอย่างเชน
int n;
// primitive
double x; // primitive
boolean completed;
// primitive
String name;
// class
Button closeBtn;
// class
[email protected]
Assignment Statements
 ต ัวแปรอาจถูกกาหนดค่าเริม
่ ต้นตงแต่
ั้
การประกาศต ัวแปร
int a = 1;
int x = 24, y = 36, z = -12, time;
String name, greeting = "hello";
double salary = 50000.;
bonus = 10000;
totalSalary = salary + bonus;
้ าหร ับการกาหนดค่า ไม่ใชก
่ ารเปรียบเทียบ
 เครือ
่ งหมาย = จะใชส
int a=5;
a = a + 2; // a จะมีค่าเป็ น 7
[email protected]
Casting
้ ะไม่สามารถกาหนดค่าข้อมูลชนิดหนึง่ ให้ก ับต ัวแปรทีม
 โดยปกติผใู ้ ชจ
่ ี
ชนิดข้อมูลทีแ
่ ตกต่างก ันได้ เว้นแต่จะมีการแปลงชนิดข้อมูลเพือ
่ ให้
เหมาะสมก ับชนิดข้อมูลของต ัวแปรก่อน
 Casting จะเป็นเพียงการเปลีย
่ นแปลงชนิดข้อมูลทีไ่ ด้จากการคืนค่า
่ ารเปลีย
ไม่ใชก
่ นชนิดข้อมูลทีแ
่ ท้จริงของต ัวแปร
 Casting จะมีอยูส
่ องแบบได้แก่
 Implicit Casting
 Explicit Casting
[email protected]
Implicit Casting
 Casting จะถูกกระทาโดยอ ัตโนม ัติเมือ
่ ชนิดข้อมูลทีม
่ ข
ี นาดเป็นจานวน
ไบต์ตา
่ กว่าถูกกาหนดให้แก่ ข้อมูลทีม
่ ข
ี นาดเป็นจานวนไบต์สง
ู ว่า
 โดยมีลาด ับด ังนี้ (lowest -> highest):
 byte --> short --> int --> long --> float --> double
่ :
 ต ัวอย่างเชน
double x;
int n = 5;
x = n;


ค่าทีค
่ น
ื จาก n จะถูก cast ให ้มีชนิดข ้อมูลเป็ นแบบ double และ
กาหนดให ้แก่ x
ชนิดข ้อมูลของตัวแปร n ยังคงเป็ น int
[email protected]
Explicit Casting
 ขนาดของชนิดข้อมูลจะถือเป็นสงิ่ สาค ัญในการ cast
ต ัวอย่างเช่น
การ cast ชนิดข้อมูลแบบ long ให้เป็นแบบ int
 ชนิดข้อมูลแบบ long จะมีขนาด 64 บิตและชนิดข้อมูลแบบ int จะมี
้ จาก long เป็น int หากมีขอ
ขนาด 32 บิต เมือ
่ มีการ cast เกิดขึน
้ มูล
ทีส
่ าค ัญอยูใ่ น 32 บิตบน ข้อมูลเหล่านนก็
ั้ จะหายไปโดยอ ัตโนม ัติ
 แม้วา
่ การ casting จะกระทาจากชนิดข้อมูลทีม
่ ข
ี นาดเล็ กกว่าไปย งั
ชนิด ข้อ มูล ทีเ่ ท่า ก น
ั หรือ มากกว่า แต่จ ะต้อ งระม ด
ั ระว งั ในการ cast
่ จาก long ไปเป็น Float ซงึ่ มี
ข้อมูลทีเ่ ท่าก ันแต่แตกต่างชนิดก ัน เชน
ขนาด 32 บิตเท่าก ัน แต่เป็นชนิดแตกต่างก ัน
[email protected]
Explicit Casting
 Explicit casting จะเป็นการเปลีย
่ นแปลงชนิดของข้อมูลของค่าเพียง
ครงเดี
ั้ ยวสาหร ับการใชง้ านของต ัวแปร
 การทางานสามารถใสเ่ ครือ
่ งหมายวงเล็บหน้าพร้อมชนิดข้อมูลที่
ื่ ของต ัวแปร
ต้องการวางหน้าชอ
่ :
 ต ัวอย่างเชน
int n;
double x = 2.0;
n = (int) x;


n จะถูกกาหนดค่าโดยการ cast จากค่าของ x (จาก double เป็ น
integer)
x จะไม่มก
ี ารเปลีย
่ นแปลง
[email protected]
OPERATOR &
ASSIGNMENT
[email protected]
Priority of Operators
 ลาด ับความสาค ัญของ Boolean operators จะสูงกว่า relational
operators ด ังนี:้
1
2
3
4
not
*
+
<
/
>
div
or
<=
mod
and
>=
=
<>
 3 > 5 or 650 < 1 จะถูกประมวลผลเป็น 3 > (5 or 650) < 1
ซงึ่ ในกรณีนจ
ี้ ะถือว่าผิดปกติ เนือ
่ งจาก Boolean operator or จะ
ทางานทีค
่ า่ Boolean แทนทีจ
่ ะเป็นค่า integers.
่
 ด ังนนในกรณี
ั้
นค
ี้ วรใชว้ งเล็บร่วมด้วย เชน
(3>5) or (650<1)
[email protected]
Division
การหารต ัวเลขแบบ integer :
 เครือ
่ งหมาย / จะหมายถึงการ truncate

9 / 4 ===> 2
 เครือ
่ งหมาย % จะหมายถึง remainder

9 % 4 ===> 1
การหารต ัวเลขแบบ floating point :
 เครือ
่ งหมาย / จะหมายถึงการหารแบบปกติ

9.0 / 4.0 ===> 1.25
 แต่เครือ
่ งหมาย % ย ังทางานได้
 8.4 % 4.1 ===> 0.2
[email protected]
Operator Precedence
class Arithmetic {
public static void main(String args[]) {
int x = 17, y = 5;
System.out.print("x = " + x);
System.out.println(" y = " + y);
System.out.println(" x + y = " + (x
System.out.println(" x - y = " + (x
System.out.println(" x * y = " + (x
System.out.println(" x / y = " + (x
System.out.println(" x % y = " + (x
}
}
[email protected]
+
*
/
%
y));
y));
y));
y));
y));
x
y
x
x
x
x
x
= 17
=5
+ y = 22
- y = 12
* y = 85
/y=3
%y=2
Unary
้ ัวกระทา
 โดยปกติการทางานของเครือ
่ งหมายทางคณิตศาสตร์จะใชต
เป็นคูเ่ สมอ แต่บางเครือ
่ งหมายทีต
่ อ
้ งการต ัวกระทาเพียงต ัวเดียวจะ
ถูกเรียกว่า Unary Operator
 unary integer operators จะมีด ังนี้
Increment
++
Decrement
-Negation
Bitwise complement ~
[email protected]
Increment & Decrement
++ หมายถึงบวกค่าไปอีกหนึง่
-- หมายถึงลบไปหนึง่
int x = 4;
x++; // x is 5;
x += 5; มีคา่ เท่ากับ x = x + 5;
้ ้ดังนี:้
ประยุกต์ใชได
-=
*=
/=
%=
[email protected]
Unary Example
class IncDec {
public static void main(String args[]) {
int x = 8, y = 13;
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("++x = " + ++x);
System.out.println("y++ = " + y++);
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
[email protected]
x=8
y = 13
++x = 9
y++ = 13
x=9
y = 14
Block Statement
 เป็นกลุม
่ ของชุดคาสง่ ั ทีอ
่ ยูภ
่ ายในเครือ
่ งหมายปี กกา ({})
 เมือ
่ มีการสร้างต ัวแปรแบบ local ภายใน Block Statement ต ัวแปร
ิ้ สุดการทางานหล ังจากที่ block สน
ิ้ สุดการประมวลผล
ด ังกล่าวจะสน
่ ต ัวแปร y ถูกประกาศไว้ใน block ภายในเมธอด
 ต ัวอย่างเชน
้ ัวแปร y ได้ภายนอก block
testblock() ด ังนนจะไม่
ั้
สามารถเรียกใชต
ทีก
่ าหนดไว้:
void testblock() {
int x = 10;
{ // start of block
int y = 50;
System.out.println("inside the block:");
System.out.println("x:" + x);
System.out.println("y:" + y);
} // end of block
}
[email protected]
Operator Precedence
 การทางานของเครือ
่ งหมายทางคณิตศาสตร์โดยปกติจะมีลาด ับ
่
ความสาค ัญทีแ
่ ตกต่างก ัน เชน
x = 2 * 5 + 12 / 4
 ในกรณีน ี้ 12 / 4 จะทาก่อนได้ผลล ัพธ์ 3 จากนนจึ
ั้ งบวกค่า 5 + 3
เข้าด้วยก ัน และคูณค่า 8 ด้วย 2 ได้ผลล ัพธ์เป็น 16
 ลาด ับความสาค ัญของเครือ
่ งหมายจากมากไปน้อยจะมีด ังต่อไปนี:้
.
[]
()
< > <=
== !=
++
>=
?:
[email protected]
&
=
--
! ~
*/%
+-
<< >>
^
&&
||
>>>
JAVA
CODING
STANDARD
[email protected]
/**
* Copyright (C) 2000, Intalio Inc.
*
*
*
*
*
The program(s) herein may be used and/or copied only with
the written permission of Intalio Inc. or in accordance with
the terms and conditions stipulated in the agreement/contract
under which the program(s) have been supplied.
*
*
*
*
*
$Id: Example.java ,v 1.1 2000/08/03 22:11:13 authorname Exp $
Date Author Changes
Aug 2 2000 author-name Created
Aug 2 2000author-name Added new classes
*/
package demo;
import java.util.NoSuchElementException;
class{
……………
}
[email protected]
/**
*
*
*
*
*
*
A class representing a window on the screen.
For example:
<pre>
Window win = new Window(parent);
win.show();
</pre>
*
*
*
*
*
*
@author <a href=”mailto:[email protected]”>Bozo the Clown</a>
@version $Revision: 1.4 $ $Date: 2000/02/29 22:11:13 $
@see awt.BaseWindow
@see awt.Button
*/
class Window extends BaseWindow {
...
}
[email protected]
Why Have Code Conventions
 80% of software = Maintenance.
 Hardly any software is maintained for its
whole life by the original author.
 Code conventions improve the readability of
the software, allowing engineers to
understand new code more quickly and
thoroughly.
 If you ship your source code as a product,
you need to make sure it is as well packaged
and clean as any other product you create.
[email protected]
Java Source Files
 Beginning comments
/*
* Classname
*
* Version info
*
* Copyright notice
*/
 Package and Import statements; for example:



import java.applet.Applet;
import java.awt.*;
import java.net.*;
[email protected]
Naming Conventions
ื่ packages ควรขึน
้ ต้นด้วยอ ักษรต ัวเล็ก
 การตงช
ั้ อ
 mypackage
 com.company.application.ui
ื่ class ควรขึน
้ ต้นด้วยอ ักษรต ัวใหญ่
 การตงช
ั้ อ
 Line, AudioSystem
ื่ ต ัวแปร(variable) ควรขึน
้ ต้นด้วยอ ักษรต ัวเล็ก
 การตงช
ั้ อ
 line, audioSystem
้ ักษรต ัวใหญ่และสามารถใช ้
 ค่าคงที่ (constants) ต้องใชอ
เครือ
่ งหมาย “_” เพือ
่ แบ่งคา
 MAX_ITERATIONS, COLOR_RED
ื่ methods จะต้องเป็นคากริยาและควรขึน
้ ต้นด้วยอ ักษรต ัว
 การตงช
ั้ อ
เล็ก
 getName();
 computeTotalWidth();
[email protected]
Naming Conventions
้ พท์
ั ภาษาอ ังกฤษ
ื่ ควรใชศ
 การตงช
ั้ อ
 The terms get/set must be used where an attribute is
accessed directly.
 employee.getName();
 employee.setName(name);
 ควรใช ้ “is” นาหน้าต ัวแปรและการทางาน(variables and
methods) ประเภท boolean.
 The term compute can be used in methods where
something is computed.
 The term find can be used in methods where something is
looked up.
้ อ
 “No” สามารถนามาใชต
่ ท้ายก ับต ัวแปรทีม
่ ล
ี ักษณะเป็นต ัวเลข
[email protected]
Wrapping Lines
 ต ัดบรรท ัดหล ังเครือ
่ งหมาย comma (,) .
 ต ัดบรรท ัดก่อนเครือ
่ งหมาย operator (+ - * /) .
 จ ัดระยะสาหร ับบรรท ัดใหม่ให้ตรงก ับระด ับเดิม
 If the above rules lead to confusing code or to
code that’s squished up against the right margin,
just indent 8 spaces instead.
[email protected]
Example
function(longExpression1, longExpression2,
longExpression3, longExpression4, longExpression5);
var = function1(longExpression1,
function2(longExpression2,
longExpression3));
longName1 = longName2 * (longName3 + longName4 - longName5)
+ 4 * longname6; // PREFER
longName1 = longName2 * (longName3 + longName4
- longName5) + 4 * longname6; //AVOID
[email protected]
Blank Lines เว้นบรรท ัด
 เว้นบรรท ัดระหว่าง Method
 เว้นบรรท ัดระหว่างต ัวแปร และ first statement
 เว้นบรรท ัดก่อน comment
 เว้นบรรท ัดระหว่าง logical sections inside a method
้
เพือ
่ ให้อา
่ นง่ายขึน
[email protected]
Blank Spaces
 A keyword followed by a parenthesis should be separated
by a space
while (true) {
...
}
 A blank space should appear after commas in argument
lists.
 All binary operators except . should be separated from their
operands by spaces.
 The expressions in a for statement should be separated by
blank spaces.
 for (expr1; expr2; expr3)
 a = (a + b) / (c * d);
 while (d++ = s++) {
n++;
}
[email protected]
 prints("size is " + foo + "\n");
Q&A
[email protected]