JAVA vs. C++
- 无直接指针操作
- 自动内存管理
- 数据类型长度固定
- 不用头文件
- 不包含结构和联合
- 不支持宏
- 不用多重继承
- 无类外全局变量
- 无GOTO
应用程序的类型:
Application: 应用程序,独立的程序,需要执行器运行
Applet: 小应用程序,由appleViewer或web浏览器运行
编译:使用JDK中的javac
Example: javac Hello.java
运行:java工具
Example: java Hello
文件名要与public class的名字一致
一个文件至多有一个public class
设定path和classpath
path: javac和java的路径
classpath: 所引用的class的路径
#check current path
set path
#set path
set path=.;c:\jdk\bin;...
#set classpath
set classpath
在java命令行上使用 classpath
javac -cp libxx.jar
java -cp libxx.jar
使用package时的编译
设置生成文件路径
java -d classes dir\*.java
java -cp classes edu.pku.tds.PackageTest
Applet的编辑,编译和运行
applet标签:<applet code="HelloWorldApplet.class" width=200 height=40 background=white>
运行
appleViewer HelloWorldApplet.html
or
download java(JRE) from java.com, and then enable java in browser
alternative to applet: flash, silverlight, javascript
jar打包
(1)编译 javac A.java
(2) 打包 jar cvfm A.jar A.man A.class
(3)运行 java -jar A.jar
javadoc生成文档
javadoc -d directory xxx.java
javap查看类信息
javap class_name
javap反汇编 (显示java虚拟机指令)
javap -c class_name
comments
/*
*
*/
输入/输出
Text Interface: 使用Scanner class
import java.util.Scanner;
class ScannerTest{
public static void main(String[] args ){
Scanner scanner = new Scanner(System.in);
System.out.print("Please input one number");
int a = scanner.nextInt();
System.out.printf("%d squared is %d \n",a,a*a);
}
}
nextInt()
nextDouble()
next() /*(for char)*/
In and Out
java.io class
System.in.read()
System.out.print(), println, printf
Input Error: try{}catch
try{
c=(char) System.in.read();
}catch(IOException e){}
Read one char
c=(char) System.in.read();
Read one line
BufferedReader in = new BufferedReader(new inputStreamReader(System.in));
s=in.readLine();
Convert to Int, or Double
n=Integer.parseInt(s);
d=Double.parseDouble(s);
图形界面的输入和输出
AppGraphInOut.java
add(xxx) 加入对象
btn.addActionListener 处理事件
actionPerformed() 具体处理事件
Java 8 简写 e->{...}
lambda 表达式
Applet输入和输出
在init()中
add(xxx) 加入对象
btn.addActionListerner 处理事件
actionPerformed() 具体处理事件
java编程工具
Eclipse code template
Alt+/
流程控制语句
注释:
// 单行注释
/*...*/ multiple line comments
/**...*/ doc comment

If-else
if(condition)
{statement;}
else
{statement;}
Switch
switch(exp){
case const1:
statement1;
break;
case const2:
statement2;
break;
default:
statment_default;
break;
}
****** LOOP ******
FOR
int result=0;
for(int i=1; i<=100; i++){
result+=i;
}
WHILE
int i=1
while(i<=100){
result+=i;
i++;
}
DO/WHILE (at least execute once)
do{
result+=i;
i++;
}while(i<=100);
BREAK/CONTINUE
break;
label1: for(...){
label2: while(...){
break label1;
}
}
continue; //get into next iteration, skip the remaining statement in current iteration
continue label1;
ARRAY
int[] a;
double []b;
Mydate []c;
int []a=new int[3]; //allocate memory
a[0]=3;
a[1]=3;
a[2]=5
int[] a={3,3,5};
int[] a=new int[]{3,3,5};
MyDate[] dates={
new MyDate(22,7,1987),
new MyDate(1,1,2000),
new MyDate(4,30,2015)
};
length of array:
a.length
for(int i=0;i<a.length;i++){}
Enhanced for loop, in:
for(int age : ages){
System.out.println(age);
}
Copy:
//copy source from 0 to source.Length to dest
Array.Copy(source,0,dest,0,source.Length)
Multi-dimension array (array of array):
int [][] a={{1,2},{3,4,0,9},{5,6,7}}
int [][] t = new int[3][];
t[0]=new int[2];
t[1]=new int[4];
t[2]=new int[3];
class contains field (variable) and method (function)
Constructor
constructor is a special method
constructor has the same name as class, no return type
one class has one or more constructors
if no constructor is defined, system will provide a default constructor
Use object
Example:
Person p = new Person();
System.out.println(p.name);
p.sayHello();
Overloading
same name for multiple methods,with different signature
overloading can be used to implement polymorphism
'this' (same as python self)
Within method, field is the same as this.field
used to distinguish local variable from field
e.g.
//age, name local variable, this.age and this.name are fields
Person(int age, String name){
this.age=age;
this.name=name;
}
In constructor, can be used to construct another constructor
e.g.
Person()
{
this(0,"") // must be put as the first statement
...
}
Inheritance
child class: subclass
parent class: superclass
class Student extends Person{
...
}
method override
define a method with the same method name as parent method
method overload
define a method with the same method with different argument compared with parent method
super
use fields and methods in parent class (that was override by the same name in current class)
compared with 'this' for current class
Example
void sayHello(){
super.sayHello();
System.out.println("My school is" + school);
}
use super to inherit constructor
Student(String name, int age, String school){
super(name,age); // must put at the first place
this.school=school;
}
Superclass <-> Subclass
Person p1 = new Student();
Student s = (Student) p1; //casting, OK
Person p2 = new Person();
Student s2 = (Student) p2; // casting, OK with complier, not OK with run
Package
package pkg1.pkg2....
classes in the same package can visit each other
import
import pkg1.pkg2.classname;
import pkg1.pkg2.*;
simplify the reference classname (otherwise need pkg1.pkg2.classname)
Class Path
put a class file into a directory
javac -d [directory]
To run a program, run the class file with main()
java pk.TestPkg
CLASSPATH
(1) java -classpath [directory]; . pk.TestPkg
(2) set environment variable
set classpath=[directory];
Modifiers
Modifier for class
public or null
public: everywhere
default: within package
fields are usually private
setter and getter for methods to modify and check values of private fields
private int age;
public void setAge(){}
public void getAge(){}
Other Keywords
static
final
abstract
static:
(1) belongs to the class, not to specific instance
(2) global variable
eg. System.in and System.out
class Person{
static long totalNum; //same for all persons
int age;
String Name;
}
import static
import static java.lang.System.*;
out.println(); //equivalent to System.out.println();
final:
final class: not inheritable - no subclass allowed
final method: cannot be override by subclass
final fields or local variable:
- the value can be set at most once, cannot change the value later ;
- set value at definition place, or in constructor
static final: constant, read-only, default value 0
abstract:
abstract class: cannot define instance of the class
abstract method: has the method name, but no body; only declare, no realization; placeholder for realization in subclass (by override)
Interface
define interface: all methods are automatically public abstract
implement interface (keyword 'implements'): can be inherited by multiple subclasses, and don't consider the inheritance relations
used to indicate common behavior or methods of multiple classes
eg.
interface Collection{
void add(Object obj);
void delete(Object obj);
Object find(Object obj);
int size();
}
//implements
class FIFOQueue implements Collection{
public void add(Object obj){
...
}
public void delete(Object obj){
...
}
public int currentCount{
...
}
}
constant in interface
type NAME = value; //automatically to be public,static,final
method in interface in Java 8
-static method
-default method (default implements, no need to implements in subclass)
Syntax Summary
Class Definition
new Object(){
public String toString(){...}
}
Lambda expression
(parameters) -> result
e.g.
(String s) -> s.length()
x->x*x
()->{System.out.println("aaa");}
It's a function
It's an instance of anonymous class
It's an abbreviation of interface or interface function
E.g.
//a function needs a interface function f input
interface Fun{double fun(double x);}
static double Integral(Fun f, double a, double b, double eps){...}
//lambda as f input
d=Integral(x->Math.sin(x),0,1,EPS);
d=Integral(x->x*x,0,1,EPS);
primitive type -> Object (reference type)
int->Integer
Integer I = new Integer(10);
Boxing / Unboxing
Boxing -> reference type
Integer I=10;
Unboxing -> primitive type
int i=I;
Enum
It's a special class.
e.g.
enum Light{Red,Yellow,Green};
Light light = Light.Red
Annotation
not comments
e.g.
@Override
@Deprecated
@SuppressWarnings
User defined annotation
public @interface Author{
String name();
}
Reference and Pointer
reference is actually a pointer
function pointer: lambda expression
linked list:
class Node{
Object data;
Node next; //reference, pointer
}
Equal or Not Equal
Primitive: value equality
Reference: reference equality
Boxing reference: value equality for -128~+127, otherwise reference equality
Comparing contents in reference type: equals, hashCode()
Strings:
-- don't use ==, use equals
-- String literal (constant) can be compared with ==
String hello="Hello", lo="lo";
System.out.println(hello=="Hello"); // true
System.out.println(hello==Other.hello); // true
System.out.println(hello=="Hel"+"lo"); // true
System.out.println(hello=="Hel"+lo); // false
System.out.println(hello==("Hel"+lo).intern()); // true
System.out.println(hello==new String ("Hello")); // false
L6 Exception
Basic format:
try{
statement;
}catch(){
statement for exception;
}
or
try{
statement;
}catch(err1){
statement for exception;
}catch(err2){
statement for exception;
}finally{
statement for exception;//run without condition; even return before finally
}
throw: throw error
Throwable superclass -> error subclass; exception subclass
Exception class
Constructor
public Exception();
public Exception(String message);
Exception(String message, Throwable cause);
Method
-getMessage()
-getCause()
-printStackTrace()
Exception Types
-Runtime Exception: not necessary to handle
-Checked Exception: need handle (catch/throws)
throws: put after method signature, claiming this method may throw exception
Example of throws:
public static void readFile()throws IOException{
...
}
Another format of try
try(type variable=new type()){
...
}
-automatically add finally{variable.close();}
User Defined Exception
...
Assert
assert statement: "output message"; //output if statement is False
Testing (JUnit)
Eclipse
Project->New->Junit Test Case
Project->Run as -> Junit Test
@Test marks testing method
fail(message); // program error
assertEqauls(param1, param2); //param1==param2
assertNull(param);//param should be null
Errors
-Syntax error
-Runtime error
try,catch
-Logic error
debug, unit test
Debug
-breakpoint
right click line left to add break point
Ctrl+shift+B
-trace
F5: Stepwise run
F6: Stepover run (skip getting inside method)
F7: Jump out of method
Ctrl+R: Run to the point
-watch
realtime watch: point to the variable to get value
fast watch: right click, Inspector
add watch: right click, watch
L7 Basic Library, Data Structure, and Algorithm
Basic Class Library
java.lang: language related
java.util: utility
java.io
java.awt, javax.swing: GUI
java.net: web
java.sql
JAVA API Doc
JDK source code
Object class
The direct or indirect superclass of all classes
Methods in Object class
(1) equals(): whether the contents is the same
Example:
Integer one = new Integer (1);
Integer anotherOne = new Integer (1);
if(one==anotherOne) //false
if(one.equals(anotherOne)) //true
(2)getClass(): a final method, return the class type in run time
(3)toString(): return the string type of the object
usually used to output: System.out.println(person);
or used to + string: "current person is" + person
adjust output info by overriding
(4)finalize()
clear object
(5) notify(), notifyAll(), wait()
multi-thread
Wrappers of basic type (basic type -> reference type)
Character, Byte, Short, Integer, Long, Float, Double, Boolean
Methods:
valueOf(String), toString()
xxxxValue(): get the value, eg. intValue()
Cannot modify the value inside
Boxing and unboxing
Integer I = 5; // Boxing, equivalent to I=Integer.valueOf(5)
int i=I; //Unboxing, equivalent to i=I.intValue()
Math Class
public final static double PI;
public static double log(double a);
public static double random(); // random number in (0,1)
public static double sqrt();
System Class
System.getProperty(String name): get the system property value
System.getProperties(): get a object containing all system property value
String
Two types of String
- String: immutable, cannot change the value; inefficient in += : new string created
- String Buffer, StringBuilder: able to modify after construction
String s=""; //s+=x;
StringBuffer sb=new StringBuffer(); //sb.append(x);
Methods in String
-concat,replace,substring,toLowerCase,toUpperCase
-endsWith, startsWith, indexOf
-equals,equalsIgnoreCase
StringBuffer
Constructor
-StringBuffer()
-StringBuffer(int capacity)
-StringBuffer(String initialString)
Methods
-append, insert, reverse, setCharAt, setLength
StringToken: split string
-StringTokenizer(string, separater)
Date
Calender
-getTime() -> Date
Date
-getTime() -> long
Calender
-getInstance()
-get(DAY_OF_MONTH)
-set()
-setTime(date), getTime()
Date
-new Date(), new Date(System.currentTimeMillis())
-setTime(long), getTime()
SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
-format, parse
Java8: time api: java.time.*, java.time.format.*
Collection API and List
Interface
-subinterface List
-subinterface Set
Map
-key-value pair
Interface
-add
-remove
-set
-get
...
List interface
- Realized class: ArrayList, LinkedList, Vector (old,similar to array list)
- Iterator
eg. Iterator iterator = iterable.iterator();
while(iterator.hasNext()) doSomething(iterator.next());
Enhanced for
- for (Element e : list) doSomething(e);
Stack and Queue
Stack
-Last In First Out, LIFO
Methods
-push(Object item)
-pop()
-empty()
Eg.
Stack<String> stk = new Stack<>();
Queue
-First In First Out, FIFO
-LinkedList
Methods
Old Classes
Old -> Now
-Vector -> ArrayList
-Stack -> LinkedList
-Hashtable -> HashMap
-Enumeration -> Iterator
Set
-Realized classes: HashSet, TreeSet
-unique element: hashCode() not equal, if equal, then evaluate equals or == false
Map
-Realized classes: HashMap, TreeMap
-entrySet(): key-value set, keySet(), values()
-map.put("key","value");
-map.get("key");
Sort and Search
Arrays
-Arrays.asList(10,7,6,5,9) get a list
-sort(), binarySearch()
-eg. Arrays.<String>sort(s);
-eg. Arrays.<String>binarySearch(s,s[2])
Compare
-Comparable: java.lang.Comparable
-define a java.lang.Comparator
eg. public int compare(T o1, T o2)
- more general, use Lambda
eg. Collections.sort(school,(p1,p2)->p1.age-p2.age);
Collections
-sort,binarySearch,reverse
Generic
Same processing method for different classes
eg.
Vector<String> v = new Vector<String> ();
v.addElement("one");
String s = v.elementAt(0);
Self-Defined Generic
- Class
class TNode<T>{
private T value
private ArrayList<TNode<T>> children = new ArrayList<>();
}
- Method
class BeanUtil{
public static <T> T getInstance(String clzName){
}
Type control
-? any type
reverse(List<?> list)
-extends: all subclass of E
Set.addAll(Collection<? extends E> col)
-super: all superclass os T
Collections.fill(List<? super T> list, T obj)
Algorithms
-Exhaust Algorithm 遍历
for(; ;){if();}
-Iterative Algorithm 迭代
while(){x=f(x);}
-Recursive Algorithm 递归
f(n){f(n-1);}
-Back-track Algorithm 回溯
x++; if(...) x--;
eg. Queen 8
L8 Threads
One process can contain multiple threads
-These threads share CPU - parallel or sequential (time slots)
-Share memory - eg. multiple threads visit the same object
Java supports multi-threading
java.lang -> Thread
Thread class
-run() method
eg.
-inheriting Thread
class MyThread extends Thread{
public void run() {
...
}
}
-or Use Thread constructor to pass Runnable object
class MyTask implements Runnable{
public void run() {
...
}
}
Thread thread = new Thread(mytask);
//Run the thread
thread.start();
-or anonymous class
- or Lambda expression
Hold thread for a while
Thread.sleep(1000); // 1000 milisecond
Set priority
setPriority(int priority)
Regular thread - not Daemon
-if regular thread is still running, program will not end
Daemon thread
-backend thread
-thread.setDeamon(true)
Multi-threading
Lock
monitor, lock, mutex, synchronized
-lock on some code blocks
synchronized(object){...}
-lock on methods
eg. public synchronized void push(char c){...}
Wait
wait()
-release object lock
notify(), or notifyAll()
-notify other waiting threads it is ready
java.util.concurrent library
-automatically lock and unlock
AtomicInteger
aint.getAndIncrement();
CopyOnWriteArrayList
CopyOnWriteArraySet
ConcurrentHashMap
ArrayBlockingQueue
字符流(char):Reader, Writer
InputStream:
- read() method, byte-wise
OutStream:
-write() method
Reader: read()
Writer: write()
Node Stream
- Node example: file
Common node streams:
Processing Stream
- Further processing of node stream or other processing stream
- Buffer, construct object, etc
Common Processing streams:
Standard Input/Output
System.in: InputStream type
System.out: PrintStream type
System.err: PrintStream type
Common Contents
-binary
-text
-object
Encoding format
-UTF-8
-ASCII
-GB2312
-default (depending on os)
New package java.nio.file.Files
-readAllLines() method
Object I/O
ObjectInputStream
ObjectOutputStream
DataInputStream
DataOutputStream
Serialize: output
Deserialize: input
Network Stream
URLGetContent.java
URL Class
-input stream: url.openStream()
Files and Directories
File Class
-treat directory as file
eg.
File f;
f= new File("Test.java");
Methods in File Class
-getName()
-getPath()
-exists()
-length()
-mkdir()
-list()
...
RandomAccessFile Class
-seed() method
Regular Expressions
-Match strings
-[charactor]{times}position: [0-9]{2,4}\b
-[0-9]+ any number from 0 to 9, '+': 1 or more digits in one word
Pattern Class for applying Regular Expressions
-split
-matches
Matcher class
-find and replace: find(), appendReplacement()
URLCrawler
AWT and swing
java.awt
javax.swing
AWT component
-Container, non-container
3 Steps to create GUI
(1) Create Component
(2) Set Layout
(3) Add Event handler
Use JFrame
set as closable
setDefaultCloseOperation(EXIT_ON_CLOSE);
Common Layouts:
-FlowLayout
-BorderLayout
-GridLayout
Event
-Action
-getsource()
Event Listener
-Program responding to action
Register event listener
addxxxxListenser
Implement event listener
implements xxxListener
extends xxxAdapter
Socket Class for client side
client to server
ServerSocket Class for server side
Media programming
Graphics and its subclass Graphics2D class
object: getGraphics(), Convas, JComponent object
Media Audio and Video
Java Advanced Imaging (JAI); Java 3D; Java Media Framework (JMF)
Database Programming
DBMS (relational database management system):
-Mysql
-Oracle
-Microsoft SQL Server
-DB2
Tool: Navicat
Eclipse tool: DB Viewer, Quantum DB
Connection class to connect to DB
Class clz;
clz.getPackage();
clz.getDeclaredFields();
clz.getName();
clz.getDeclaredMethods();
Applet: 小应用程序,由appleViewer或web浏览器运行
编译:使用JDK中的javac
Example: javac Hello.java
运行:java工具
Example: java Hello
文件名要与public class的名字一致
一个文件至多有一个public class
设定path和classpath
path: javac和java的路径
classpath: 所引用的class的路径
#check current path
set path
#set path
set path=.;c:\jdk\bin;...
#set classpath
set classpath
在java命令行上使用 classpath
javac -cp libxx.jar
java -cp libxx.jar
使用package时的编译
设置生成文件路径
java -d classes dir\*.java
java -cp classes edu.pku.tds.PackageTest
Applet的编辑,编译和运行
applet标签:<applet code="HelloWorldApplet.class" width=200 height=40 background=white>
运行
appleViewer HelloWorldApplet.html
or
download java(JRE) from java.com, and then enable java in browser
alternative to applet: flash, silverlight, javascript
jar打包
(1)编译 javac A.java
(2) 打包 jar cvfm A.jar A.man A.class
(3)运行 java -jar A.jar
javadoc生成文档
javadoc -d directory xxx.java
javap查看类信息
javap class_name
javap反汇编 (显示java虚拟机指令)
javap -c class_name
comments
/*
*
*/
输入/输出
Text Interface: 使用Scanner class
import java.util.Scanner;
class ScannerTest{
public static void main(String[] args ){
Scanner scanner = new Scanner(System.in);
System.out.print("Please input one number");
int a = scanner.nextInt();
System.out.printf("%d squared is %d \n",a,a*a);
}
}
nextInt()
nextDouble()
next() /*(for char)*/
In and Out
java.io class
System.in.read()
System.out.print(), println, printf
Input Error: try{}catch
try{
c=(char) System.in.read();
}catch(IOException e){}
Read one char
c=(char) System.in.read();
Read one line
BufferedReader in = new BufferedReader(new inputStreamReader(System.in));
s=in.readLine();
Convert to Int, or Double
n=Integer.parseInt(s);
d=Double.parseDouble(s);
图形界面的输入和输出
AppGraphInOut.java
add(xxx) 加入对象
btn.addActionListener 处理事件
actionPerformed() 具体处理事件
Java 8 简写 e->{...}
lambda 表达式
Applet输入和输出
在init()中
add(xxx) 加入对象
btn.addActionListerner 处理事件
actionPerformed() 具体处理事件
java编程工具
Eclipse code template
Alt+/
L3
数据类型
2类:
primitive types:
double, char, int
reference types:
class, interface, 数组
存储:
primitive types: stack
reference types:
heap
reference type is similar to pointer, contain
address
赋值
Person p2=p; copy reference (pointing to the same
class)
基本数据类型 4类/8种
整数型 byte,short,int,long
浮点数型 float,double
逻辑型 Boolean
字符型 char
Boolean: true/false,不可以用0/1代替
Char: char c=’A’ unicode编码,每个字符占两个字节
Int:
long b=3.14L
Java没有无符号数
Float: 3.14,3.14e2 (*10^2),3.14E2
Float a=3.14f
大小写敏感
class 首字母大写,其他首字母小写
运算符
% 取余, 个位 a%10
^ 异或
&& 短路short-circuit与: 第一个操作为false就不判断第二个操作
|| short-circuit
or: if the first statement is true, then don’t evaluate the second statement
<< 左移,a<<b 移b位,<<1=*2
>>带符号右移,int对32位取模,long对64位取模
>>>无符号右移
Example:
数据类型转换 (cast)
int i =(int)l;
注释:
// 单行注释
/*...*/ multiple line comments
/**...*/ doc comment

If-else
if(condition)
{statement;}
else
{statement;}
Switch
switch(exp){
case const1:
statement1;
break;
case const2:
statement2;
break;
default:
statment_default;
break;
}
****** LOOP ******
FOR
int result=0;
for(int i=1; i<=100; i++){
result+=i;
}
WHILE
int i=1
while(i<=100){
result+=i;
i++;
}
DO/WHILE (at least execute once)
do{
result+=i;
i++;
}while(i<=100);
BREAK/CONTINUE
break;
label1: for(...){
label2: while(...){
break label1;
}
}
continue; //get into next iteration, skip the remaining statement in current iteration
continue label1;
ARRAY
int[] a;
double []b;
Mydate []c;
int []a=new int[3]; //allocate memory
a[0]=3;
a[1]=3;
a[2]=5
int[] a={3,3,5};
int[] a=new int[]{3,3,5};
MyDate[] dates={
new MyDate(22,7,1987),
new MyDate(1,1,2000),
new MyDate(4,30,2015)
};
length of array:
a.length
for(int i=0;i<a.length;i++){}
Enhanced for loop, in:
for(int age : ages){
System.out.println(age);
}
Copy:
//copy source from 0 to source.Length to dest
Array.Copy(source,0,dest,0,source.Length)
Multi-dimension array (array of array):
int [][] a={{1,2},{3,4,0,9},{5,6,7}}
int [][] t = new int[3][];
t[0]=new int[2];
t[1]=new int[4];
t[2]=new int[3];
L4
CLASSclass contains field (variable) and method (function)
Constructor
constructor is a special method
constructor has the same name as class, no return type
one class has one or more constructors
if no constructor is defined, system will provide a default constructor
Use object
Example:
Person p = new Person();
System.out.println(p.name);
p.sayHello();
Overloading
same name for multiple methods,with different signature
overloading can be used to implement polymorphism
'this' (same as python self)
Within method, field is the same as this.field
used to distinguish local variable from field
e.g.
//age, name local variable, this.age and this.name are fields
Person(int age, String name){
this.age=age;
this.name=name;
}
In constructor, can be used to construct another constructor
e.g.
Person()
{
this(0,"") // must be put as the first statement
...
}
Inheritance
child class: subclass
parent class: superclass
class Student extends Person{
...
}
method override
define a method with the same method name as parent method
method overload
define a method with the same method with different argument compared with parent method
super
use fields and methods in parent class (that was override by the same name in current class)
compared with 'this' for current class
Example
void sayHello(){
super.sayHello();
System.out.println("My school is" + school);
}
use super to inherit constructor
Student(String name, int age, String school){
super(name,age); // must put at the first place
this.school=school;
}
Superclass <-> Subclass
Person p1 = new Student();
Student s = (Student) p1; //casting, OK
Person p2 = new Person();
Student s2 = (Student) p2; // casting, OK with complier, not OK with run
Package
package pkg1.pkg2....
classes in the same package can visit each other
import
import pkg1.pkg2.classname;
import pkg1.pkg2.*;
simplify the reference classname (otherwise need pkg1.pkg2.classname)
Class Path
put a class file into a directory
javac -d [directory]
To run a program, run the class file with main()
java pk.TestPkg
CLASSPATH
(1) java -classpath [directory]; . pk.TestPkg
(2) set environment variable
set classpath=[directory];
Modifiers
Modifier for class
public or null
public: everywhere
default: within package
fields are usually private
setter and getter for methods to modify and check values of private fields
private int age;
public void setAge(){}
public void getAge(){}
Other Keywords
static
final
abstract
static:
(1) belongs to the class, not to specific instance
(2) global variable
eg. System.in and System.out
class Person{
static long totalNum; //same for all persons
int age;
String Name;
}
import static
import static java.lang.System.*;
out.println(); //equivalent to System.out.println();
final:
final class: not inheritable - no subclass allowed
final method: cannot be override by subclass
final fields or local variable:
- the value can be set at most once, cannot change the value later ;
- set value at definition place, or in constructor
static final: constant, read-only, default value 0
abstract:
abstract class: cannot define instance of the class
abstract method: has the method name, but no body; only declare, no realization; placeholder for realization in subclass (by override)
Interface
define interface: all methods are automatically public abstract
implement interface (keyword 'implements'): can be inherited by multiple subclasses, and don't consider the inheritance relations
used to indicate common behavior or methods of multiple classes
eg.
interface Collection{
void add(Object obj);
void delete(Object obj);
Object find(Object obj);
int size();
}
//implements
class FIFOQueue implements Collection{
public void add(Object obj){
...
}
public void delete(Object obj){
...
}
public int currentCount{
...
}
}
constant in interface
type NAME = value; //automatically to be public,static,final
method in interface in Java 8
-static method
-default method (default implements, no need to implements in subclass)
Syntax Summary
Class Definition
L5
Variables
- primitive type
- reference type
1.field
- in class
- has initial default value
- can be set as public, private, static, final
2.local variable
- in method
- does not have initial default value
- can be set as final; cannot be set as public, private, static
Variable pass
- primitive type: value pass
- reference type: address pass
Polymorphism
1. Polymorphism during compiling
- overload
2. Polymorphism during running
- override
- dynamic binding --- virtual method invoking
upcasting
use subclass as a superclass
Person p = new Student();
virtural method invoking
--system chose the actual type of instance to choose method
--all methods (except final) can do automatically dynamic binding
instanceof: check dynamic variable, boolean output
eg. if(x instanceof Integer){...}
common methods are virtual methods
static, private, final methods are not virtual methods
-static: only belongs to class, not related with instances; static method depends on the type that declared, not depend on the instance type
-private: not applicable to subclass, thus not virtual
-final: cannot be override, virtual does not exist
Object Construction and Initialization
Constructor
-abstract class also has constructor
-this: current class' constructor; super: super class' constructor; must come at the 1st statement, this/super only one can apply
-If no this or super in constructor, then default super() is used (superclass' no argument constructor), in this case, either there is no constructor is defined in superclass, such that there is default constructor a(); or there is a constructor without argument defined a(){...}
-multiple constructors can be defined
Initialization
p = new Person(){{age=18; name="Li Ming";}};
Inside class:
1. Instance Initializer
{statement;}
2. Static Initializer
-related to class, not related to instances; used at the first time the class is used, before instance initializer
static{statement;}
Initialization order:
step 1 super() -> step 2 field initialization (outside class constructor) -> step 3 other statements in class constructor
*final method in superclass constructor to avoid incorrect order of using virtual method
Garbage collection
finalize()
try(Scanner scanner = new Scanner(...) ){...} automatically call close() after the statement, more controllable
Internal Class / Inner Class
A class defined in another class
Used inside: same as common class
Used outside:
OutClass a = new OutClass(...);
OutClass.InterClass b = a.new InterClass(...)
In InterClass, to use fields or methods with the same name in OutClass and InterClass:
OutClass field: OutClass.this.field
InterClass field: this.field
Modifier:
public, protected, (default), private
final, abstract
static InterClass (called nested class): it's an common class actually, not related to OuterClass instance
When define instance, no need to add OutClassInstance before new
Cannot call non-static fields or methods in OutClass
Local Class - a special Internal Class
defined in a method
Like local variables, no public, private, protected, static
can come with final or abstract
can call OutClass components
cannot call the local variables (temporary) in method, unless final local variables
Anonymous Class - a special Internal Class
- no class name, use superclass or interface name, no class, extends or implements
- an instance is created when class definition, 'new' before class definition
- used only once
- use superclass constructor
eg.new Object(){
public String toString(){...}
}
Lambda expression
(parameters) -> result
e.g.
(String s) -> s.length()
x->x*x
()->{System.out.println("aaa");}
It's a function
It's an instance of anonymous class
It's an abbreviation of interface or interface function
E.g.
//a function needs a interface function f input
interface Fun{double fun(double x);}
static double Integral(Fun f, double a, double b, double eps){...}
//lambda as f input
d=Integral(x->Math.sin(x),0,1,EPS);
d=Integral(x->x*x,0,1,EPS);
primitive type -> Object (reference type)
int->Integer
Integer I = new Integer(10);
Boxing / Unboxing
Boxing -> reference type
Integer I=10;
Unboxing -> primitive type
int i=I;
Enum
It's a special class.
e.g.
enum Light{Red,Yellow,Green};
Light light = Light.Red
Annotation
not comments
e.g.
@Override
@Deprecated
@SuppressWarnings
User defined annotation
public @interface Author{
String name();
}
Reference and Pointer
reference is actually a pointer
function pointer: lambda expression
linked list:
class Node{
Object data;
Node next; //reference, pointer
}
Equal or Not Equal
Primitive: value equality
Reference: reference equality
Boxing reference: value equality for -128~+127, otherwise reference equality
Comparing contents in reference type: equals, hashCode()
Strings:
-- don't use ==, use equals
-- String literal (constant) can be compared with ==
String hello="Hello", lo="lo";
System.out.println(hello=="Hello"); // true
System.out.println(hello==Other.hello); // true
System.out.println(hello=="Hel"+"lo"); // true
System.out.println(hello=="Hel"+lo); // false
System.out.println(hello==("Hel"+lo).intern()); // true
System.out.println(hello==new String ("Hello")); // false
L6 Exception
Basic format:
try{
statement;
}catch(){
statement for exception;
}
or
try{
statement;
}catch(err1){
statement for exception;
}catch(err2){
statement for exception;
}finally{
statement for exception;//run without condition; even return before finally
}
throw: throw error
Throwable superclass -> error subclass; exception subclass
Exception class
Constructor
public Exception();
public Exception(String message);
Exception(String message, Throwable cause);
Method
-getMessage()
-getCause()
-printStackTrace()
Exception Types
-Runtime Exception: not necessary to handle
-Checked Exception: need handle (catch/throws)
throws: put after method signature, claiming this method may throw exception
Example of throws:
public static void readFile()throws IOException{
...
}
Another format of try
try(type variable=new type()){
...
}
-automatically add finally{variable.close();}
User Defined Exception
...
Assert
assert statement: "output message"; //output if statement is False
Testing (JUnit)
Eclipse
Project->New->Junit Test Case
Project->Run as -> Junit Test
@Test marks testing method
fail(message); // program error
assertEqauls(param1, param2); //param1==param2
assertNull(param);//param should be null
Errors
-Syntax error
-Runtime error
try,catch
-Logic error
debug, unit test
Debug
-breakpoint
right click line left to add break point
Ctrl+shift+B
-trace
F5: Stepwise run
F6: Stepover run (skip getting inside method)
F7: Jump out of method
Ctrl+R: Run to the point
-watch
realtime watch: point to the variable to get value
fast watch: right click, Inspector
add watch: right click, watch
L7 Basic Library, Data Structure, and Algorithm
Basic Class Library
java.lang: language related
java.util: utility
java.io
java.awt, javax.swing: GUI
java.net: web
java.sql
JAVA API Doc
JDK source code
Object class
The direct or indirect superclass of all classes
Methods in Object class
(1) equals(): whether the contents is the same
Example:
Integer one = new Integer (1);
Integer anotherOne = new Integer (1);
if(one==anotherOne) //false
if(one.equals(anotherOne)) //true
(2)getClass(): a final method, return the class type in run time
(3)toString(): return the string type of the object
usually used to output: System.out.println(person);
or used to + string: "current person is" + person
adjust output info by overriding
(4)finalize()
clear object
(5) notify(), notifyAll(), wait()
multi-thread
Wrappers of basic type (basic type -> reference type)
Character, Byte, Short, Integer, Long, Float, Double, Boolean
Methods:
valueOf(String), toString()
xxxxValue(): get the value, eg. intValue()
Cannot modify the value inside
Boxing and unboxing
Integer I = 5; // Boxing, equivalent to I=Integer.valueOf(5)
int i=I; //Unboxing, equivalent to i=I.intValue()
Math Class
public final static double PI;
public static double log(double a);
public static double random(); // random number in (0,1)
public static double sqrt();
System Class
System.getProperty(String name): get the system property value
System.getProperties(): get a object containing all system property value
String
Two types of String
- String: immutable, cannot change the value; inefficient in += : new string created
- String Buffer, StringBuilder: able to modify after construction
String s=""; //s+=x;
StringBuffer sb=new StringBuffer(); //sb.append(x);
Methods in String
-concat,replace,substring,toLowerCase,toUpperCase
-endsWith, startsWith, indexOf
-equals,equalsIgnoreCase
StringBuffer
Constructor
-StringBuffer()
-StringBuffer(int capacity)
-StringBuffer(String initialString)
Methods
-append, insert, reverse, setCharAt, setLength
StringToken: split string
-StringTokenizer(string, separater)
Date
Calender
-getTime() -> Date
Date
-getTime() -> long
Calender
-getInstance()
-get(DAY_OF_MONTH)
-set()
-setTime(date), getTime()
Date
-new Date(), new Date(System.currentTimeMillis())
-setTime(long), getTime()
SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
-format, parse
Java8: time api: java.time.*, java.time.format.*
Collection API and List
Interface
-subinterface List
-subinterface Set
Map
-key-value pair
Interface
-add
-remove
-set
-get
...
List interface
- Realized class: ArrayList, LinkedList, Vector (old,similar to array list)
- Iterator
eg. Iterator iterator = iterable.iterator();
while(iterator.hasNext()) doSomething(iterator.next());
Enhanced for
- for (Element e : list) doSomething(e);
Stack and Queue
Stack
-Last In First Out, LIFO
Methods
-push(Object item)
-pop()
-empty()
Eg.
Stack<String> stk = new Stack<>();
Queue
-First In First Out, FIFO
-LinkedList
Methods
Old Classes
Old -> Now
-Vector -> ArrayList
-Stack -> LinkedList
-Hashtable -> HashMap
-Enumeration -> Iterator
Set
-Realized classes: HashSet, TreeSet
-unique element: hashCode() not equal, if equal, then evaluate equals or == false
Map
-Realized classes: HashMap, TreeMap
-entrySet(): key-value set, keySet(), values()
-map.put("key","value");
-map.get("key");
Sort and Search
Arrays
-Arrays.asList(10,7,6,5,9) get a list
-sort(), binarySearch()
-eg. Arrays.<String>sort(s);
-eg. Arrays.<String>binarySearch(s,s[2])
Compare
-Comparable: java.lang.Comparable
-define a java.lang.Comparator
eg. public int compare(T o1, T o2)
- more general, use Lambda
eg. Collections.sort(school,(p1,p2)->p1.age-p2.age);
Collections
-sort,binarySearch,reverse
Generic
Same processing method for different classes
eg.
Vector<String> v = new Vector<String> ();
v.addElement("one");
String s = v.elementAt(0);
Self-Defined Generic
- Class
class TNode<T>{
private T value
private ArrayList<TNode<T>> children = new ArrayList<>();
}
- Method
class BeanUtil{
public static <T> T getInstance(String clzName){
}
Type control
-? any type
reverse(List<?> list)
-extends: all subclass of E
Set.addAll(Collection<? extends E> col)
-super: all superclass os T
Collections.fill(List<? super T> list, T obj)
Algorithms
-Exhaust Algorithm 遍历
for(; ;){if();}
-Iterative Algorithm 迭代
while(){x=f(x);}
-Recursive Algorithm 递归
f(n){f(n-1);}
-Back-track Algorithm 回溯
x++; if(...) x--;
eg. Queen 8
L8 Threads
One process can contain multiple threads
-These threads share CPU - parallel or sequential (time slots)
-Share memory - eg. multiple threads visit the same object
Java supports multi-threading
java.lang -> Thread
Thread class
-run() method
eg.
-inheriting Thread
class MyThread extends Thread{
public void run() {
...
}
}
-or Use Thread constructor to pass Runnable object
class MyTask implements Runnable{
public void run() {
...
}
}
Thread thread = new Thread(mytask);
//Run the thread
thread.start();
-or anonymous class
- or Lambda expression
Hold thread for a while
Thread.sleep(1000); // 1000 milisecond
Set priority
setPriority(int priority)
Regular thread - not Daemon
-if regular thread is still running, program will not end
Daemon thread
-backend thread
-thread.setDeamon(true)
Multi-threading
Lock
monitor, lock, mutex, synchronized
-lock on some code blocks
synchronized(object){...}
-lock on methods
eg. public synchronized void push(char c){...}
Wait
wait()
-release object lock
notify(), or notifyAll()
-notify other waiting threads it is ready
java.util.concurrent library
-automatically lock and unlock
AtomicInteger
aint.getAndIncrement();
CopyOnWriteArrayList
CopyOnWriteArraySet
ConcurrentHashMap
ArrayBlockingQueue
L9 I/O and Stream
字节流(byte):InputStream, OutputStream字符流(char):Reader, Writer
InputStream:
- read() method, byte-wise
OutStream:
-write() method
Reader: read()
Writer: write()
Node Stream
- Node example: file
Common node streams:
Processing Stream
- Further processing of node stream or other processing stream
- Buffer, construct object, etc
Common Processing streams:
Standard Input/Output
System.in: InputStream type
System.out: PrintStream type
System.err: PrintStream type
Common Contents
-binary
-text
-object
Encoding format
-UTF-8
-ASCII
-GB2312
-default (depending on os)
New package java.nio.file.Files
-readAllLines() method
Object I/O
ObjectInputStream
ObjectOutputStream
DataInputStream
DataOutputStream
Serialize: output
Deserialize: input
Network Stream
URLGetContent.java
URL Class
-input stream: url.openStream()
Files and Directories
File Class
-treat directory as file
eg.
File f;
f= new File("Test.java");
Methods in File Class
-getName()
-getPath()
-exists()
-length()
-mkdir()
-list()
...
RandomAccessFile Class
-seed() method
Regular Expressions
-Match strings
-[charactor]{times}position: [0-9]{2,4}\b
-[0-9]+ any number from 0 to 9, '+': 1 or more digits in one word
Pattern Class for applying Regular Expressions
-split
-matches
Matcher class
-find and replace: find(), appendReplacement()
URLCrawler
L10
GUI (Graphical User Interface)AWT and swing
java.awt
javax.swing
AWT component
-Container, non-container
3 Steps to create GUI
(1) Create Component
(2) Set Layout
(3) Add Event handler
Use JFrame
set as closable
setDefaultCloseOperation(EXIT_ON_CLOSE);
Common Layouts:
-FlowLayout
-BorderLayout
-GridLayout
Event
-Action
-getsource()
Event Listener
-Program responding to action
Register event listener
addxxxxListenser
Implement event listener
implements xxxListener
extends xxxAdapter
L11
Network programmingSocket Class for client side
client to server
ServerSocket Class for server side
Media programming
Graphics and its subclass Graphics2D class
object: getGraphics(), Convas, JComponent object
Media Audio and Video
Java Advanced Imaging (JAI); Java 3D; Java Media Framework (JMF)
Database Programming
DBMS (relational database management system):
-Mysql
-Oracle
-Microsoft SQL Server
-DB2
Tool: Navicat
Eclipse tool: DB Viewer, Quantum DB
Connection class to connect to DB
L12
Get class infoClass clz;
clz.getPackage();
clz.getDeclaredFields();
clz.getName();
clz.getDeclaredMethods();









No comments:
Post a Comment