Title: SL-110 Fundamental of Java Programming Language ????
1SL-110Fundamental of Java Programming Language
????
- Sean Cheng
- sean.cheng_at_ever.com.tw
2Sun Java Course Path
3Module 1
- Explaining Java Technology
4????
- ??????
- Download Java SDK???
- ?? Java SDK???
- ??????
- Path ltJAVA_HOMEgt\bin
- ClassPath .
- ??????? (EditPlus2)
5???
- Java Platform
- ??????java??,?????????Windows?Mac OS?
Linux,Java??????? - ???????????
- Java Runtime Environment (JRE)
- Java Virtual Machine(ByteCode ???)
- Java Class Libraries (API)
- ?????
6Java Runtime Environment
- Loads Code
- Class Loader
- Verifies Code
- Bytecode Verifier
- Executes Code
- Runtime Interpreter
7Java Technology Product Groups
8JRE vs. SDK
9Module 2
- Analyzing a Problem Using
- Object-Oriented Analysis
10???????
- OOA ??????
- OOD ??????
- OOP ????????
11??????
- Identify Problem Domain ??????
- Identify Object ????
- Recognized Object ????
- Identify Objects Attribute Operation??????????
12Problem Statement
DirectClothing, Inc. sells shirts from their
catalog. Business is growing 30 percent per
year, and they need a new order entry system.
You have been contracted by DirectClothing to
design the new system. DirectClothing produces a
catalog of clothing every six months and mails it
to subscribers. Each shirt in the catalog has an
item identifier (ID), one or more colors (each
with a different color code), one or more sizes,
a description, and a price. DirectClothing
accepts checks and all major credit cards. To
place an order, customers can call DirectClothing
to order directly from a customer service
representative (CSR), or customers can mail or
fax an order form to DirectClothing.
13Problem Statement - continue
Orders that are mailed or faxed are entered by a
CSR. DirectClothing would like to give the
customer the option of entering an order online
through the Internet. The items available online
are priced according to the current catalog. As
the order is entered in the system, each items
availability (quantity-on-hand) is verified. If
one or more items are not currently available (in
DirectClothings warehouse), then the order is
placed on hold until the items arrive at the
warehouse. After all of the items are available,
payment is verified and the order is submitted to
the warehouse for assembly and for shipping to
the customers address. If the order is received
by phone, the CSR gives the customer an order ID,
which is used to track the order throughout the
process. The CSR also gives the CSRs telephone
extension to the customer.
14Identifying Objects
DirectClothing, Inc. sells shirts from their
catalog. Business is growing 30 percent per
year, and they need a new order entry system.
You have been contracted by DirectClothing to
design the new system. DirectClothing produces a
catalog of clothing every six months and mails it
to subscribers. Each shirt in the catalog has an
item identifier (ID), one or more colors (each
with a different color code), one or more sizes,
a description, and a price. DirectClothing
accepts checks and all major credit cards. To
place an order, customers can call DirectClothing
to order directly from a customer service
representative (CSR), or customers can mail or
fax an order from to DirectClothing.
15Identifying Objects - continue
Orders that are mailed or faxed are entered by a
CSR. DirectClothing would like to give the
customer the option of entering an order online
through the Internet. The items available online
are priced according to the current catalog. As
the order is entered in the system, each items
availability (quantity-on-hand) is verified. If
one or more items are not currently available (in
DirectClothings warehouse), then the order is
placed on hold until the items arrive at the
warehouse. After all of the items are available,
payment is verified and the order is submitted to
the warehouse for assembly and for shipping to
the customers address. If the order is received
by phone, the CSR gives the customer an order ID,
which is used to track the order throughout the
process. The CSR also gives the CSRs telephone
extension to the customer.
16???????
- ???????
- Account, Shirt
- ??????????
- color, size
- ???????????????????
- display, submit order
17??????
- Design Class
- ???????(??)
- Modeling Class
- ?????????
18?? vs. ??
- ??????????
- ?????????????
- Java programmer ??????????????.
- Java ???????,JVM ??????????(????????),????????????
?????(???????????)
19Module 3
- Developing and Testing a Java Technology Program
20????????
- ?????
- ????????
- ????????????
- Java??????
- ??main Object(??????????????)
- main???????????
- ??????????????
21Class Structure
- Class ????
- Class declaration
- Attributes
- Methods (operations)
- Comments
22Structuring Classes
public class Shirt public int shirtID 0
public String description "-description
required-" public char colorCode 'U'
public double price 0.0 public int
quantityInStock 0 public void
displayShirtInformation()
System.out.println("Shirt ID " shirtID)
System.out.println("Shirt description"
description) System.out.println("Color
Code " colorCode) System.out.println("Sh
irt price " price) System.out.println("Q
uantity in stock " quantityInStock)
Shirt
shirtID int description String colorCode char price double quantityInStock int
displayShirtInformation()
23Class Declaration
- modifiers class ltclass_namegt
-
- Modifier?public???,???class_name.java????
- ??Java??,????????????
- ?????????Java???,?????public???
24Attribute Variable Declaration
- modifiers lttypegt ltnamegt initial_values
- ????????????????
- ?????class body??????????
- ????????????????
25Method Declaration
- modifiers ltreturn_typegt ltnamegt (arguments)
-
- ????????,return type ? void
- Argument list
- ??0N?
- Type Name
- ??????,?,??
26Comments
- ????
- public int shirtID 0 //Default ID for the
shirt - // The color codes are R Red, B Blue
- ????
- /
- this is a multi-line comment
- /
- JavaDoc ??
- /
- The following comment will be show in JavaDoc
- /
27main Method
- public static void main (String args)
-
- ?????main(),???JRE????????
- Modifier
- ???final
- ?????
- ????????????
- String args
- ???????????args
28Compiling Executing a Java Program
public class HelloWorld public static void
main(String args) System.out.println("Hell
o World!")
javac
java
HelloWorld.java
HelloWorld.class
29Module 4
- Declaring, Initializing and Using Variables
30Attribute vs. Local Variable
- Attribute
- Variable???class???,method????
- ???member variable, instance variable
- ???????,????????????????,????
- ???????,??default? (int i ??i0)
- ??????Heap????
- Local variable
- Variable??method????
- ??????????????
- ??????modifier(final??)
- ???????
- ???Stack????
31Literals
- A value ???????????
- boolean
- ?? true ? false
- ??
- ???? ?, ?, ????
- ???? int (??) ? long (????L)
- ???
- ???? float (????F)?double (??, ????D)
- ?? ????? ()
- ?? ????? ()
322s complement
- 2??????
- ????X?2?????,??-X?2?????
- ???1?0, 0?1
- ?Step1??1
- 5 00000110 11010100 -x
- 11111001 00101011
- 1
1 - -5 11111010 00101100 x 44
33Escape Character
\b \u0008 Back Space
\t \u0009 Tab
\n \u000a New Line
\f \u000c Form feed
\ \u0022
\ \u0027
\\ \u005c \
\r \u000d Return
34Primitive Data Types
Type Length Range Literal examples Init. Value
boolean 1 true, false true, false false
byte 8 -2727-1 2, -114 0
short 16 -215215-1 2, -32699 0
int 32 -231231-1 2, -147344778 0
long 64 -263263-1 2, -2036854708L 0L
float 32 IEEE754 99F, -32699.01F 0.0F
double 64 IEEE754 -111, 21E12 0.0
char 16 065536 A, x, 3, ?, \n, \u0063 \u0000
35Identifier
- ????????
- Unicode letters A-Z, a-z, ??,
- Underscore _
- Dollar sign , ,
- Digits 0,1,9 (?????????)
- ???keywords / Reserved words
- ????? Case sensitive
36Key words Reserved Word
- Keywords
- abstract assert boolean break
byte - case catch char class
continue - default do double else
extends - final finally float for
if - implements import instanceof int
interface - long native new
package private - protected public return short
static - strictpf super switch
synchronized this - throw throws transient try
void - volatile while
- Reserved words
- false null true const
goto
37Promotion
byte
short
int
long
float
double
-
- Promotion????
- 1.????
- 2.????
- 3.????
-
char
boolean
38Module 5
- Creating and Using Objects
39Declaration
public class Shirt public int shirtID 0
public String description "-required-"
public char colorCode 'U' public double
price 0.0 public void
displayShirtInformation()
System.out.println("Shirt ID " shirtID)
System.out.println("Shirt description"
description) System.out.println("Color Code
" colorCode) System.out.println("Shirt
price " price)
- Shirt myShirt
- myShirt new Shirt()
myShirt
Stack memory
40Instantiating
public class Shirt public int shirtID 0
public String description "-required-"
public char colorCode 'U' public double
price 0.0 public void displayShirtInformatio
n() System.out.println("Shirt ID "
shirtID) System.out.println("Shirt
description" description)
System.out.println("Color Code " colorCode)
System.out.println("Shirt price " price)
Shirt myShirt myShirt new Shirt()
0
-required-
U
0.0
shirtID
description
colorCode
price
myShirt
Stack Memory
Heap Memory
41Initializing
public class Shirt public int shirtID 0
public String description "-required-"
public char colorCode 'U' public double
price 0.0 public void displayShirtInformatio
n() System.out.println("Shirt ID "
shirtID) System.out.println("Shirt
description" description)
System.out.println("Color Code " colorCode)
System.out.println("Shirt price " price)
Shirt myShirt myShirt new Shirt()
0
-required-
U
0.0
shirtID
description
colorCode
price
0x01234567
myShirt
Stack Memory
Heap Memory
42int x 7 Shirt myShirt new Shirt()
yourShirt new Shirt()
shirtID
description
colorCode
price
0
-required-
U
0.0
0
-required-
U
0.0
shirtID
description
colorCode
price
7
0x01234567
0x07654321
x
myShirt
yourShirt
Stack Memory
Heap Memory
43Manipulate Data
Shirt myShirt new Shirt() yourShirt new
Shirt() myShirt.colorCode R yourShirt.colorC
ode G
shirtID
description
colorCode
price
0
-required-
R
0.0
0
-required-
G
0.0
shirtID
description
colorCode
price
7
0x01234567
0x07654321
x
myShirt
yourShirt
Stack Memory
Heap Memory
44Accessing Data
System.out.println(myShirt.colorCode) myShirt.dis
playShirtInformation()
0
-required-
R
0.0
shirtID
description
colorCode
price
0x01234567
myShirt
Stack Memory
Heap Memory
45String Literal Pool
- JVM??????????
- ? String identifier string_literal
????????????string pool? - ??????? String pool ??????????????, ????????????,
????????
46String literal Pool
public class StringPoolTest public static
void main(String args) String s1
"Hello" String s2 "Hello" String s3 new
String("Hello") String s4 new
String("Hello") System.out.println("s1s2
is " (s1s2)) System.out.println("s3s4 is
" (s3s4))
Literal Pool
47Module 6
- Using Operators and Decision Constructs
48????
??? ??? Sample
??,??,??,?? true ! flase String s Hello s Hello
! ??,??,??,?? true ! flase String s Hello s Hello
gt ??,??
gt ??,??
lt ??,??
lt ??,??
instanceof ??,?? S instanceof String
49??????
AND true false
true true false
false false false
OR true false
true true true
false true false
XOR true false
true false true
false true false
NOT true
true false
false true
50????
- ???????
- , , ! ,
- ???????(shirt-circuit)
- ,
- ???????????????????,???????,????????????????,????
?? - ????????????,?????????
X
true true
false X
X
true X
false false
51Branching Statements if/else
if (boolean) statements
if (boolean) statements else
statements
52Branching Statements else if
if (boolean) statements else if (boolean)
statements else statements
53Branching Statements - switch
- switch (variable)
- case literal_value
- statements
- break
- case literal_value
- statements
- break
- default
- statements
-
54(No Transcript)
55Module 7
56while Loop
- while (boolean)
- statements
-
57do/while Loop
- do
- statements
- while (boolean)
58for Loop
1
2
3
- for (initialize,initialize
boolean_expression update,update) - statements
-
- for?????
- ????. ?????
- ?????. ????????
- ????. ?? code_blocks??, 2 ??
59(No Transcript)
60Module 8
- Developing and Using Methods
61Method Declaration
modifiers ltreturn_typegt ltnamegt (arguments)
Arguments list
Name of the method
Return data type
Accessibility
62Call Stack mechanism
63static attribute
public class Count public int no
public static int counter 0 public
void print() System.out.print(counte
r counter) System.out.print(no
no)
public class CountTest public static void
main(String args) Count c1 new
Count() Count c2 new Count()
c1.no 1 c2.no 2 c1.count
2 c1.print() c2 print()
64static attribute
- ????????(Global Variable)
- ????????
- ClassName.attributeName
- objectName.attributeName
65static method
- ??
- ??utility??
- ??static variable
- ??
- modifiers static return_type naem(argu_list)
66static method
- ??
- ClassName.methodName()
- objectName.methodName()
- ??
- static methods??????
- local variables (parameter)
- static variables
- ??static method
67Overloaded Method
public class Calculator public int sum(int
numberOne, int numberTwo)
System.out.println("Method One") return
numberOne numberTwo public float
sum(float numberOne, float numberTwo)
System.out.println("Method Two") return
numberOne numberTwo public float
sum(int numberOne, float numberTwo)
System.out.println("Method Three") return
numberOne numberTwo
public class CalculatorTest public static
void main(String args) Calculator
myCalculator new Calculator() int
totalOne myCalculator.sum(2,3)
System.out.println(totalOne) float totalTwo
myCalculator.sum(15.99F, 12.85F)
System.out.println(totalTwo) float
totalThree myCalculator.sum(2, 12.85F)
System.out.println(totalThree)
68Module 9
- Implementing Encapsulation and Constructors
69Encapsulation
- ?? encapsulation
- ????????,??????????
- ???????
- ?????
- ????????
- ?????????
- Getter Setter
- Setter?????????
- ?????????,??????????????
70Encapsulation - continue
public class People public String name
default public int age 0
public class People private String name
default private int age 0 public void
setName(String newName) name newName
public String getName() return name
public void setAge(int newAge) age
newAge public int getAge() return
age
71Encapsulation - continue
public class People private String name
default private int age 0 public void
setName(String newName) name newName
public String getName() return name
public void setAge(int newAge)
if(newAge gt 0) age newAge
public int getAge() return age
setter method setXXX( )
getter method getXXX( )
72Encapsulation - continue
public class People private String name
default private int age 0 public void
setName(String newName) name newName
public String getName() return name
public void setAge(int newAge)
if(newAge gt 0) age newAge
public int getAge() return age
public class PeopleTest public static void
main(String args) People p new
People() p.name John p.age 30
p.setName(John) p.setAge(30)
System.out.println(Name p.getName())
System.out.println(Age p.getAge())
compile error!
73Creating Constructors
- modifiers class ltclass_namegt
- modifiers constructor_name(arguments)
- code_blocks
-
Arguments list
Same as class_name
Accessibility
74Constructors
- ???????
- ??????
- ?????????????
- ????(Overloading)
75Default Constructor
public class People private String name
private int age public String show ()
System.out.println(name age)
javac People.java
public class People private String name
private int age public People() super()
public String show () System.out.println(nam
e age)
76Constructors overloading
public class People private String name
private int age public People(String newName)
name newName age 0
public People(String newName, int newAge)
name newName age newAge
77Module 10
- Creating and Using Arrays
78Declaration
- type array_identifier
- type array_identifier
- Ex
- int age
- Shirt shirts
x
age
shirts
Stack Memory
79Instantiation
- array_identifier new typelength
- Ex
- age new int 4
- shirts new Shirt 3
0
0
0
0
7
0x01234567
0x02345678
x
Age
Shirts
Stack Memory
Heap Memory
80Initialization
- array_identifier index value
- Ex
- age 2 3
- shirts 0 new Shirt()
0
1
3
0
0
-required-
U
0.0
shirtID
description
colorCode
price
7
0x01234567
0x02345678
x
age
shirts
0x03456789
Stack Memory
Heap Memory
81Declaration, Instantiation Initialization
82Two-Dimension Array
Quarter1 Quarter2 Quarter3 Quarter4
Year1
Year2
Year3
Year4
Year5
83Declaration
- type array_identifier
- type array_identifier
- Ex
- int yearlySales
x
yearlySales
Stack Memory
84Instantiation
- array_identifier new type number_of_arrayslen
gth - Ex
- yearlySales new int 34
0
0
0
0
0
0
0
0
0x12345678
0x23456789
0x34567890
7
0x01234567
x
yearlySalese
0
0
0
0
Stack Memory
Heap Memory
85Initialization
- array_identifier number_of_arrays index
value - Ex
- yearlySales 12 500
0
0
0
0
0
0
500
0
0x12345678
0x23456789
0x34567890
7
0x01234567
x
yearlySalese
0
0
0
0
Stack Memory
Heap Memory
86Module 11
87Inheritance
- ??
- ???????,?????(Generalize),????(specialize)???
- is A relationship
- ?????????????
- ???(sub-class)?????(super-class)?????
88SubClass declaration
- modifiers class ltclass_namegt extends
class_name
Name of the super class
Keyword
Name of the class
Keyword
Accessibility
89Abstraction
- ???
- ??????????????,?????????
- ?????????,???????
- ???????????????
- A Manager is an Employee
- A Engineer is an Employee
- ?????????
90Employee, Manager, Engineer
public class Employee private String name
private int salary public void show ()
System.out.println(name) System.out.println(s
alary)
public class Manager private String name
private int salary private String
department public void show ()
System.out.println(name) System.out.println(s
alary) System.out.println(department)
91public class Engineer private String name
private int salary private String skill
public void show () System.out.println(name
) System.out.println(salary) for(int
i0 i lt skill.length i)
System.out.println(skilli)
public class Employee private String name
private int salary public void show ()
System.out.println(name) System.out.println(s
alary)
92public class Manager extends Employee private
String department public void show ()
System.out.println(name) System.out.println(s
alary) System.out.println(department)
public class Employee private String name
private int salary public void show ()
System.out.println(name) System.out.println(s
alary)
public class Engineer extends Employee private
String skill public void show ()
System.out.println(name) System.out.println(s
alary) for(int i0 i lt skill.length i)
System.out.println(skilli)
93??(package)???
- ??
- ?????????????????? ??
- ?????????
- ??package(default)?????
94??Package??
- Package?????
- ???package?????,?????????
- ????package?????,??????????,???????
- ?????vs.??
- ?????????
- ??????????package??
95????Package??
- ????Package??
- ????????
- ??(import)package????
- ??(import)????package
96import????
- ????import
- java.lang.
- default package(??????)
- ??????
- ?????,?classapth??????????????,??????????