Object-oriented Programming Language (JAVA) : 李建欣 (Jianxin Li)
Object-oriented Programming Language (JAVA) : 李建欣 (Jianxin Li)
Language (JAVA)
(面向对象程序设计)
李建欣(Jianxin Li)
[email protected]
School of Computer,
Beihang University
© Beihang University
Chapter 2 Review
Object
Class
Inheritance
Interface
2
© Beihang University
Exercise
extends
implements
Fields:
Shape public float area;
extends
3
© Beihang University
Chapter-3
4
© Beihang University
programming language
5
© Beihang University
Agenda
6
© Beihang University
Identifier
Identifier Convention
The name of variable, class, method (function)
A variable's name can be any legal identifier — an
unlimited-length sequence of Unicode letters and
digits, beginning with a letter, the dollar sign "$", or the
underscore character "_".
Variable names are case-sensitive
E.g.,
Value
VAlue
userName
userPassword
7
© Beihang University
Try to write down some variables!
Some variables declaration
8
© Beihang University
Some Conventions
The rules and conventions for naming your variables can be
summarized as follows:
The convention, however, is to always begin your
variable names with a letter, not "$" or "_". Additionally,
the dollar sign character, by convention, is never used at
all.
When choosing a name for your variables, use full words
instead of cryptic abbreviations. Doing so will make
your code easier to read and understand. In many cases it
will also make your code self-documenting; fields named
cadence, speed, and gear, for example, are much more
intuitive than abbreviated versions, such as s, c, and g.
Also keep in mind that the name you choose must not
be a keyword or reserved word
9
© Beihang University
Some Conventions
If the name you choose consists of only one word, spell
that word in all lowercase letters. If it consists of more than
one word, capitalize the first letter of each subsequent
word. The names print, gearRatio and currentGear are
prime examples of this convention.
If your variable stores a constant value,
such as static final int NUM_GEARS = 6,
the convention changes slightly, capitalizing every letter and
separating subsequent words with the underscore character.
10
© Beihang University
Keywords
http://en.wikipedia.org/wiki/List_of_Java_keywords
11
© Beihang University
Primitive Data Types
The Java programming language is strongly-typed,
which means that all variables must first be declared
before they can be used. This involves stating the
variable's type and name.
Four types
Logical type
boolean
Character type
char
Integer type
byte, short, int, long,
Floating type
float, double
12
© Beihang University
Logical type
Boolean
The boolean data type has only two possible values:
true and false.
Use this data type for simple flags that track true/false
conditions. This data type represents one bit of
information.
13
© Beihang University
Character Type
Char
The char data type is a single 16-bit Unicode
character.
It has a minimum value of '\u0000' (or 0) and a
maximum value of '\uffff' (or 65,535 inclusive).
One useful class: String
Java programming language also provides special support for
character strings via the java.lang.String class.
Enclosing your character string within double quotes will
automatically create a new String object;
for example, String s = "this is a string"; String objects are
immutable, which means that once created, their values cannot be
changed.
Notice: The String class is not technically a primitive data type
14
© Beihang University
Integer Type
15
© Beihang University
Floating Type
Float
The float data type is a single-precision 32-bit floating
point
float x=1.2;
Double
The double data type is a double-precision 64-bit
floating point
double x=1.212321;
16
© Beihang University
Default Value
17
© Beihang University
Summary
18
© Beihang University
Declarations
19
© Beihang University
Assignment
20
© Beihang University
A class example
public class Date{
private int day;
private int month;
private int year;
void setDate( int a, int b, int c){... }
}
21
© Beihang University
Declaration and Reference for Variables
22
© Beihang University
Declaration and Reference for Variables
today 0xabcd
0xabcd
day 0
month 0
year 0
23
© Beihang University
Reference Assignment
1 Date a, b ;
2 a=new Date( );
3 b=a;
0xabcd
a 0xabcd
0 day
0 month
b 0xabcd
0 year
24
© Beihang University
Agenda
25
© Beihang University
Operators
Now that we can learn how to declare and initialize
variables, you probably want to know how to do
something with them.
Learning the operators of the Java programming
language is a good place to start.
26
© Beihang University
Operators Table
27
© Beihang University
Operators Table
28
© Beihang University
Summary of Operators
Arithmetic Operators
+,-,*,/, %, ++, - -
Equality and Relational Operators
>, >=, < <=, ==, !=
Bitwise and Bit Shift Operators
>>, <<, >>>, & , |, ^,~
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
29
© Beihang University
Summary of Operators (cont.)
Conditional Operators
&& Conditional-AND
|| Conditional-OR
Assignment Operator
=, +=, -=, *=, /=, %=,&=, |=, ^=, <<=,
>>=, >>>=
Type Comparison Operator
instanceof Compares an object to a specified type
Other Operators
?: , [], . , ( ),(type), new,
D
30
© Beihang University
Arithmetic Operator
31
© Beihang University
Bitwise Operator Example
32
© Beihang University
What is the Java equivalent of unsigned
C/C++ Java
unsigned byte b = ...; int b = ...;
b += 100; b = (b + 100) & 0xff;
33
© Beihang University
Agenda
34
© Beihang University
Statements & Blocks
35
© Beihang University
Flow of Control
Java executes one statement after the other in the
order they are written
Many Java statements are flow control statements:
Alternation: if, if else, switch
Looping: for, while, do while
jump: break, continue, return
36
© Beihang University
IF-THEN-ELSE
if (<condition expression>)
<statement 1>;
else
<statement2>;
37
© Beihang University
IF…ELSE Example
int temperature = 35;
if (temperature>35){
System.out.println(“It’s too hot!”);
System.out.println(“I want to go home!”);
}
else
{
System.out.println(“Have a nice day.”);
System.out.println(“Let’s stay here.”);
}
38
© Beihang University
Nested If statements
If (<condition expression1>)
{…
if (<condition expression 2>)
<statement 2>;
…
}
39
© Beihang University
While
Format:
40
© Beihang University
while Statement
public class WhileDemo {
public static void main(String[] args) {
int i = 0;
char c = copyFromMe.charAt(i);
Results:
while (c != 'g') {
copyToMe.append(c); Copy this strin
c = copyFromMe.charAt(++i);
}
System.out.println(copyToMe);
}
}
41
© Beihang University
do-while Statement
public class DoWhileDemo {
public static void main(String[] args) {
int i = 0;
char c = copyFromMe.charAt(i);
Results:
do {
copyToMe.append(c); Copy this strin
c = copyFromMe.charAt(++i);
} while (c != 'g');
System.out.println(copyToMe);
}
}
42
© Beihang University
Jump statement
break
Jump from switch, loop statements
Continue
Jump from the following parts of the loop
statement
43
© Beihang University
Examples
45
© Beihang University
Array
46
© Beihang University
Array
An array is a container object that holds a fixed
number of values of a single type.
The length of an array is established when the
array is created. After creation, its length is fixed.
You've seen an example of arrays already, in the
main method of the "Hello World!" application.
47
© Beihang University
Array Example
An array of ten elements
Each item in an array is called an element, and
each element is accessed by its numerical index. As
shown in the above illustration, numbering begins
with 0.
The 9th element, for example, would therefore be
accessed at index 8.
48
© Beihang University
Declaring a Variable to Refer to an Array
49
© Beihang University
Creating, Initializing an Array
Int[] anArray;
anArray = new int[10]; // create an array of integers If
this statement were missing,
The next few lines assign values to each element of
the array:
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
Alternatively, you can use the shortcut syntax to
create and initialize an array:
String names[ ] = { “Jack”, “Wang”, “Lee”};
int a[ ] = {1, 2, 3};
Date d[] = { new Date( ), new Date( ), new Date( )} 50
© Beihang University
Object Array Demo
public class ArrayOfStringsDemo {
public static void main(String[] args) {
String[] anArray = { "String One", "String Two",
"String Three" };
for (int i = 0; i < anArray.length; i++) {
System.out.println(anArray[i].toLowerCase());
}
}
}
51
© Beihang University
Multidimensional Array
Declaration
int a[ ][ ]; 或int [ ][ ] a;
Instance
a = new int[4][4]; //allocate a square array
a = new int[4][ ]; // the latter dimension is not given
a[0] = new int[10] ;
a[1] = new int[5];
…
Get the length of array
a = new int [10][12];
a.length = 10 ;
a[0].length = 12 ;
52
© Beihang University
Example of Multidimensional Array
54
© Beihang University
Copying Array Function
Array copying function
System.arrayCopy(Object source, int srcIndex,
Object dest, int destIndex,
int length)
55
© Beihang University
Copying Array Demo
56
© Beihang University
Results
Results:
57
© Beihang University
Exercise (for everyone)
58
© Beihang University
Following Lessons
Chapter4
Java OOP Features
Chapter 5
59
© Beihang University