Java Intro
Write Once Run Anywhere
- write once:
Java src code(.java file) ==> Javac (complier) () ==>bytecode(.class file) - run anywhere:
byte code==> JVM (interpret) ==>machine code(binary file) ==> device (execute) - compile once, copy bytecode to any platform having JVM to execute
3 versions [p2]
| Edition | Note |
|---|---|
| Java SE (Standard) | main for desktop app, inc Java lang fundation, JDBC, nework, thread |
| Java EE (Enterprise) | main for distributed network, core as EJB |
| Java ME (Embeded) | embeded system, e.g. pad,phone |
Configuration
搭建Java Env: 安装JDK & JRE,set env_vars
| Term | Note |
|---|---|
| JDK | Java SE Development Kit, inc compiler (javac), debuger, app launcher, + JDK src code [p7] |
| JRE | run Java program [p7], inc JVM & GC |
| JVM | interpret bytecode to machine code bf each exe (call main()) |
8 primitive types
byte,short,int(default),long(L);float(F/f) &double;char(Unicode char & escape seq),boolean
Array
2-dim array int[][] tdarr = new int[a][b]
- a: tdarr.length (行); b: tdarr[i].length (列), can be spec later by
tdarr[0] = new int[2]
String
Impl by char[], strVar -> obj (in heap) -> literal (in stack)
| Declaration Syntax | Note |
|---|---|
".." (char-str) |
ref share if literal exist in stack |
new String("..") || expr |
ref not share (always new a obj in heap) |
Check str empty: check null by ==, check "" by equals() (Object.equals(): check by ==) |
|
String methods: length(), toCharArray(), format(..) |
| Type | Note |
|---|---|
String |
literal immutable |
StringBuffer |
thread safe (method synchronized so 同时只有一个线程运行该方法) |
StringBuilder |
more efficient (no thread lock) |
Object oriented
Class basics
- Access ctrl modifier:
private(本类),_(本包),protected(子类),public(所有类) [static] {..}: initializer blockstatic: run when obj declare (init asnull)- non-static: run with
new, bfcon()(Java compiler copy init blocks into con(…))
- const:
static+final - defalut constructor (from compiler):
ClassName() {} - method: 6 components, method signature: name+param-list, method overload
return this;in method [p142]main(): class main entrance [p164]
static method(): access static member directly, access instance member by obj ref ..
Inheritance & interface
interface: contract btw class & outside-world (promises to provide the behavior of the interface)- member auto
public static final[p174], cannot define inside a block .. - why only method no field: field usually private, method op on internal-state & method usually public — serve as o2o interface with the outside world (data encapsulation)
- member auto
- up-cast — same as
Parent obj = new Child(); obj.xx(xx must exist in Parent) [p164] instanceofcheck real type (constructor);null instanceof xx==> false;instanceofbf down-cast- Top class/default parent class:
Object- get class name:
Object.getClass().getName()[p160]
- get class name:
abstract: abstract-class vs interface [p174]
- static nested class: access instance member by obj ref
- inner class: no own static members (e.g.
static {..}& interface) except const- local class (block): access enclosing fn param / effectively
finallocal var- in static method, can only refer to outer.staticMembers
- anonymous class (expr of class declare + instanciate, e.g. return, = ~, arg)
- same member-restriction & accessibity as local classes; no own con();
- suitable for interface contains >=2 abstract methods, otherwise lambda expr
- field shadowing (
OuterClass.this=> obj ref of OuterClass)
- local class (block): access enclosing fn param / effectively
Others
package— namespace for related class & interfaceimport java.util.*- Java SE API: set of pkgs (class lib)
- GC只能回收
newobj —Object.finalize()&System.gc()[p141]
Concurency & Multi-thread
- 进程(process):每个独立运行的程序; CUP时间片:每个不同进程轮流执行
- 当执行一个程序时,JVM自动产生一个线程来运行
main()运行; - 在java中实现线程的2种方式:
- extends
java.lang.Thread& overriderun()run(): 定义线程任务 (无限循环 + bool break 使线程一直运行下去)start(): 启动线程 (callrun()internal), 等待系统分配资源 & 运行
- impl
java.lang.Runable(含run()) — 继承非Thread类并在当前类中实现多线程Thread t = new Thread(new Runnable())
- extends
- 线程的加入
t2.join()int1.run(): 暂停当前进程,待t执行完毕后继续 - 线程的暂停
wait()与恢复nodify() - 线程的中断
interrupt():- call
sleep() / wait()进入就绪状态, - call
interrupt()跳出run()& throwsInterruptedException
- call
- 线程同步
synchronized:防止多个线程访问资源的冲突,如dirty read — 共享资源上锁,某个时间内只允许一个线程访问该资源synchronized (obj) {..}&synchronized void method(): 放置对共享资源的操作
- 线程优先级
Thread.MAX/MIN_PRIORITY(1510): 增加/减少线程优先执行的概率,不保证顺序
Internet Communication
- server: provide init info & info processing; client: fetch info;
- LAN: Local Area Network; WAN(Wide ~): link LANs, e.g. Internet, the worldwide WAN
- IP 协议 & IP 地址; IPv4: 4字节/32位 binary; IPv6: 128 bits
- TCP/IP 协议: based on IP 协议 in Internet (4层)
- TCP协议:一对一连接,,传输可靠(保证接收且顺序相同), 速度慢(需额外认证),e.g. HTTP, FTP, Telnet
- UDP协议:一对多,无连接,不保证可靠;数据包之间相对独立(接收顺序不重要),如即时聊天;
- 端口 port:数据交流的出口(0~65535),e.g. HTTP 80;FTP 21; 套接字socket:连接app & port
- 相关java类
java.net.*InetAddress: 封装IP地址;SocketAddress: IP + portServerSocket&Socket: TCP 程序DatagramSocket&DatagramPacket: UDP 程序
TCP程序设计:
- server
new ServerSocket(port)等待client连接 - client
new Socket(host, port)请求与server连接; - server
accept()连接client并returnSocket(如一直无client请求连接则阻塞);- server创建新的Socket与client连接,
server.accept()return 该(与客户端Socket相连的)Socket;
- server继续等待新的client连接
note:
- server 一次与一个client socket连接;默认最大连接容量为50
server.accept().getOutputStream()=clientSocket.getInputStream()(server向outputStream写入信息时,client可通过InputStream 来读取,反之亦然)
UDP程序设计:
JDBC API & DB Access
Overview
JDBC API: access tabular data (e.g. Realational DB, flat file or spreadsheet ..), included in JavaSE & JavaEE ..
- JDBC 4.0 API into 2 pkgs:
java.sql& Extension pkgjavax.sql; - supports both 2-tier & 3-tier processing models for database access
JDBC driver: Drivers that impl JDBC API, dev by DB vendor .., used to communicate with data source ..
- e.g. MySQL Connector/J (it impl network protocol to MySQL, client connects directly to MySQL) ..
- contains >= 1 JDBC driver class (class impl interface
java.sql.Driver..) - init/load by
Class.forName(java.sql.Driver)(whenDriverManagerfirst attempts to connect ..)
JDBC Architecture
- 2-tier model: java app with JDBC driver (client) -> DBMS-proprietary protocol -> DBMS (server);
3-tire model: Java applet/HTML browser -> HTTP.. -> app server with JDBC.. -> DBMS (DB server)- extra middle tier: control access & data updates; simplify the deployment of app
DB concepts ..
- table (aka relation): a collection of objects of the same type (rows)
- data in a table related by common keys or concepts
Programming process
- Install DBMS & JDBC driver from DB vendor (e.g. MySQL & Connector/J)
- spec the JDBC driver class path (path of the JAR file that contains the [JDBC Driver] class specified in driver .., e.g.
"com.mysql.cj.jdbc.Driver")
- init JDBC driver by
Class.forName(java.sql.Driver)(auto-load by DriverManager class for JDBC 4.0+) - get connection (to a JDBC driver ..) by
DriverManager.getConnection(DB_URL, connProps)- DB_URL for MySQL Connector/J:
"jdbc:mysql://${DB.HOST}:${DB.PORT}/"
- DB_URL for MySQL Connector/J:
- close conn in
finally
- 3 statements:
Statement,PreparedStatement: precompiled, reusable stmt obj (with diff params byps.set*(..)) ..,CallableStatement& stored procedure .. execute(),executeQuery(query),executeUpdate()- batch updates:
int [] updateCounts = stmt.executeBatch()(update count in int-arr returned by DBMS, batch list reset to empty).. ResultSetinterface ..RowSetinterface, JavaBeans Component, scrollable & updatable ..- 1 connected
JdbcRowSet& 4 disconn (serializable & lightweight, ideal for sending data over a network) CatchedRowSet: super-interface for all disconn (extracrs.acceptChanges(con)to write data to DB)- caches data in memory, read/write by a
RowSetReader/Writerobj (provided by the assignedSyncProviderinstance when crs created) - r/w work in bg: get conn, read/write data, close conn
- ref impl of the default
SyncProviderobj: instance of theRIOptimisticProviderimpl: (not) write to db if any/no conflict (with no db locks) SyncProviderException&SyncResolverwhen conflict: compare & choose one
- caches data in memory, read/write by a
JoinRowSet: join mtp disconn rs byjrs.addRowSet(coffees, "SUP_ID");WebRowSet: enable Web Services to send/receive data from db in XML doc (standard for Web Services comm)- sets
SyncProviderobj toRIXMLProviderimpl write-/readXml(..): convert wrs to/from xml- contain data + col metadata
- sets
FilteredRowSet:frs.setFilter(Predicate)(CRUD on current visible rows)
- 1 connected
- transaction & DBMS locks ..
- default transaction isolation level depends on the DBMS &
DatabaseMetaData conn.rollback(SavePoint)..
- default transaction isolation level depends on the DBMS &
conn/stmt/rs.close()infinally: close conn also close stmt (good practice to close stmt explicitly) .., close stmt also close rsSQLException: throw when error during interaction with data source ..- errorCode: (DB vendor) imp-spec
- 3 subclass of SQLException JDBC driver-spec might throw ..
- JDBC 4.1 (Java SE 7+): allow
try-resrcstmt .. to auto close conn/stmt/resultSet even exception occur when the try block terminates - Adv DB datatype: BLOB, CLOB, XML, Locator (ogical pointer) ..
- BLOB, CLOB, NCLOB: materialize data as a stream
- release resrc by
obj.free() - MySQL not support XML (can store in LONGTEXT), ARRAY (max length) & UDT (DISTINCT, struc) .., RowID (JDBC interface: address to a row in a database table, not a standard SQL type)
- custom type mapping (from SQL data type to java data type), Datalink & URL ..
1 | CallableStatement cablStmt = conn.prepareCall("{call pro_GetInfo(?, ?)}"); |
Refs
Oracle Help Center / Java / Java SE doc / Java Tut / Learning the Java Lang
Javase 8 API
《Java 从入门到精通》(项目案例版) 2017.9