Java8那些忘却的事

我学习Java已经是十年以前的事情了。距离最后一次使用Java也是五年前的事了,以至于我以为这辈子不会再使用Java了,开发网页可以用PHP,数据处理可以用Python,就连从前不得不使用Java开发的Android应用竟然也有了替代品Kotlin。这些工具都让开发更加简洁,特别是个人开发的时候,PHP给人的感觉是项目可以做完,而Java总是能把小型项目开发成中型项目,把中型项目开发成大型项目。

最近参加培训,竟然又把Java系统性地学了一遍,然后用最后半个月的时间和一个五人团队完整的跑了一遍瀑布模型。说实话对Java的印象有所改观。Java虽然语法严格,但其接口、抽象类、强数据类型等检查机制,通过IDE加持,在团队开发中起到了不坏的效果。另外值得欣慰的是,我十年前学过的知识到现在竟然还没有过时,我稍微感到一些安心。

当然还是有很多忘掉的内容,本文用来整理这些内容。

问题背景

Java8那些忘却的事

CMD Mode

Compile and Run Java in CMD

1
2
3
4
# compile
$ javac Hello.java
# run
$ java Hello

Parameters

1
2
3
4
# export as java package
$ java --jar Hello
# run
$ java Hello

Basic Grammar

Naming rule

1
2
3
4
5
6
7
8
9
10
11
Class TheClassNameUsingPascalCase{

public static final int TheConstant;

int theVariateUsingCamelCase;

void theFunctionNameUsingCamelCase(int theVariateUsingCamelCase){

}

}

Variate Default Value

1
2
3
4
5
6

short, byte, int, long a = 0;
float, double a = 0.0;
char a = '\u0000';
boolean a = false
Other a = null;

Metric Variable

1
2
3

0.1F; //A float variable
100000000L; //A long variable

Array

1
2
3
4
5
6
7
8

int arr[] = {10, 20, 30, 40};

for (int i : arr){
sout(i);
}

sout(arr.length);

Comment

1
2
3
4
5
6
7
8
9
10
11
// comment

/*
comment1
comment2
*/

/**
document1
document2
*/

Class and Object

StringBuffer : for String modification

1
2
3
4
5
6
7
public static void main(String args[]){
StringBuffer sBuffer = new StringBuffer("google:");
sBuffer.append("www");
sBuffer.append(".google");
sBuffer.append(".com");
System.out.println(sBuffer);
}

JVM

  • CLASSPATH: a path of class files (Library)

Type

  • Autoboxing: Boxing a basic type into reference type

    1
    2
    3
    Integer i = 100;
    ==
    Integer i = new Integer(100);
  • Unboxing: Unbox a reference type to basic type

    1
    2
    3
    4
    Integer i = 100;
    rs = i + 200;
    ==
    rs = i.value() + 200;

Object.hashCode() : Hash code of the class

1
2


Inheritance

instanceof :

1
2
3
4
5
ParentClass p1 = new ParentClass();
Object o = (Object)p1;
if (o instanceof ParentClass){
ParentClass p2 = (ParentClass)o;
}

IsA, HasA

Human is a Animal :
Human has a Head :

1
2
3
class Human extends Animal{
private Head head;
}

Interface

An Interface can extends another interface or other interfaces.

Exception

Exception Class Structure

1
2
3
4
5
6
7
8
{
"Throwable": {
"Exception": {
"RunTimeException": {}
},
"Error":{}
}
}

RunTimeException: Do not checked by compiler.

Collection

Generics:

1
2
3
4
5
6
7
public class SomeClass<T>{
private T t;
private List<T> list = new List<T>();
public T someMethod(T arg){
return new T(arg);
}
}

enumeration: implements iterator

HashMap Iterate:

1
2
3
for(Map.Entry<String, String> entry : weekdays.entrySet()) {
System.out.println(entry.getKey() + " => " + entry.getValue());
}

Documentation

format

1
2
3
4
5
6
7
8
9
10
11
/**
* This is a sample
*/
public class Sample(){
/**
* Say "Hello"
*/
public void hello() {
System.out.println("Hello");
}
}

tags

1
2
3
4
5
6
7
8
9
10
/**
* @author 作者
* @version 版本号
* 当前版本号
* @since 版本号
* 前置版本号
* @see 参考标签
* @deprecated 说明
* 说明过期的情况
*/

Sample

1
2
3
4
5
6
7
/** This class shows a Sample
* @version 1.0
* @author Chenyi Liao
*/
public class Sample(){

}

generator

1
$ javadoc -d <output dir> <source.java>

Access

Same Class Same Package Sub-class Different Class
public
protected ×
(default) × ×
private × × ×

Overload and Override

  • Override: between super class and sub-class. If a class inherits a method from its superclass, then there is a chance to override the method provided that it is not marked final.
  • Overload (Overloading): in the same class.

#JUnit

setUP() and tearDown()

  • setUP(): run before all test methods.
  • tearDown(): run after all test methods.

Serialize

1
2
3
4
5
import java.io.Serializable;

public class Employee implements Serializable{

}

Java Web Application

Folder Structure

  • DocumentRoot: the root directory for web server
    • ContextRoot: JSP files, HTML…
    • WEB-INF: web.xml Classes…
      • lib: JAR files
      • classes: not opening to the Internet. Java Classes
      • web.xml

Forced close tomcat in CMD (Windows)

1
2
$ netstat -nao
$ taskkill /F /pid 3144

Tomcat Reload Automatically when source-code modified

In server.xml, there is a:

1
<Context docBase="labhttp" path="/labhttp" reloadable="false" source="org.eclipse.jst.jee.server:labhttp"/></Host>

Modify it as:

1
<Context docBase="labhttp" path="/labhttp" reloadable="true" source="org.eclipse.jst.jee.server:labhttp"/></Host>

which can be reload automatically.

1
2
3
4
5
6
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies){
String name = cookie.getName();
String value = cookie.getValue();
String utfValue = URLDecoder.decode(cookie.getValue(), "UTF-8");
}

JSP & Servlet

JAVA code

1
2
3
<% 
String msg = "helloworld";
%>

Ouput1:

1
<%=msg%>

Ouput2:

1
2
3
<%
out.println("HelloWorld");
%>

Definition:

1
2
3
4
5
6
7
8
<%!
int b = 1;
%>
<%!
private int add(int a, int b){
return a + b;
}
%>

Page Directive:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import="{package.class | package.*}, ..." ]
[ session="true|false" ]
[ buffer="none|8kb|sizekb" ]
[ autoFlush="true|false" ]
[ isThreadSafe="true|false" ]
[ info="text" ]
[ errorPage="relativeURL" ]
[ contentType="mimeType [ ; charset=characterSet ]" |
"text/html ; charset=ISO-8859-1" ]
[ isErrorPage="true|false" ]
[ pageEncoding="characterSet | ISO-8859-1" ]
[ isELIgnored="true|false"]
%>

taglib:

1
2
3
4
<%@ taglib prefix="c" url="http://java.sun.com/jstl/core"%>
<c:forEach begin="1" end="9" var="i">
<c:out value="${i}" />
</c:forEach>

Dispatcher

Servlet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class UsersServlet extends HttpServlet {
private static final long serialVersionUID = 1L ;

protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
/*****************一般实际开发这些用户数据都是从数据库查出来的*********/
List <User > users = new ArrayList <> ();
User u1 = new User () ;
u1 .setAge ( 20) ;
u1 .setName ( "Bob") ;
User u2 = new User () ;
u2 .setAge ( 21) ;
u2 .setName ( "Tony") ;
users .add ( u1) ;
users .add ( u2) ;
/*********************************************/
request .setAttribute ( "users", users) ; //对request 进制预处理准备工作
request .getRequestDispatcher ( "users.jsp").forward( request , response );//转发到users.jsp,让他去具体响应
}
}

JSP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page   contentType= "text/html; charset=UTF-8" pageEncoding ="UTF-8" trimDirectiveWhitespaces= "true"
session ="true" %>
<%@ taglib prefix= "c" uri = "http://java.sun.com/jstl/core_rt" %>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv = "Content-Type" content ="text/html; charset=UTF-8">
<title> Users List</title>
</head>
<body>

< c:forEach var ="user" items= " ${users}" >
UserName:${user.name} UserAge:${user.age} <br />
</ c:forEach>
</body>
</html>

JDBC

PreparedStatement

1
2
3
4
5
6
7
8
PreparedStatement pstmt = con.prepareStatement("INSERT INTO table VALUES(?,?)");
pstmt.setInt(1, 11);
pstmt.setString(2, "abc");
ResultSet res = pstmt.executeQuery();

//or

Integer count = pstmt.executeUpdate();

Transaction

1
2
3
4
5
6
7
8
9
10
conn.setAutoCommit(false);
try{
stmt.executeUpdate("sql1");
stmt.executeUpdate("sql2");
conn.commit();
} catch(Exception ex){
conn.rollback();
} finally{
conn.setAutoCommit(true);
}

Datatype: Java VS RDBMS

Java RDBMS
String CHAR VARCHAR
java.math.BigDecimal NUMERIC
boolean BIT
byte TINYINT
short SMALLINT
int INTEGER
long BIGINT
float REAL
double DOUBLE
byte[] BINARY VARBINARY
java.sql.Date DATE
java.sql.Time TIME
java.sql.Timestamp TIMESTAMP

MVC

Create Database and Tables

1
create table customer(id serial, name varchar(100));

Bean:

Create Bean class according to

: CustomerBean.java
1
2
3
4
5
public class CustomerBean implements Serializable(){
private int id;
private String name;
// setter, getter
}

DAO: Data Access Objects

: IDao.java
1
2
3
4
5
6
7
8
interface ICustomerDao{
void insert(Customer customer);
void update(Customer customer);
void delete(Customer customer);

List<Customer> findAll(Customer customer);
Customer findById(int customerId);
}
: CustomerDao.java
1
2
3
4
5
6
7
8
9
public class CustomerDao implements ICustomerDao{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet res = null;

CustomerDao{
con = DBManager.getConnection("xxxxx");
}
}

Servlet :

1
2
3
4
5
6
String name = request.getParameter("name");

Customer customer = new Customer(name);

request.setAttribute("customer", customer);
forward("customer", "xx.jsp");

JSP

1
2
3
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

The name is : ${customer.name}

Foreach

1
2
3
4
5
6
7
8
9
10
11
12
13
<%
String[] strs = {
"最初の行",
"真ん中の行",
"最後の行",
};
request.setAttribute("list", strs);
%>
<table>
<c:forEach var="v" items="${list}" varStatus="st">
<tr><td>${st.count}番目</td><td>${v}</td></tr>
</c:forEach>
</table>