文章目录
- Question 1 - ++i & finally
- Question 2 - 值传递和引用传递
- Question 3 - 类的加载顺序
- Question 4 - SpringMvc中获取Request
Question 1 - ++i & finally
public static void main(String[] args) {
System.out.println("result: " + calculate());
}
public static int calculate(){
int i = 0;
try {
++i;
} finally {
++i;
System.out.println("i: " + i);
}
return ++i;
}
i: 2
result: 3
public static void main(String[] args) {
System.out.println("result: " + calculate());
}
public static int calculate(){
int i = 0;
try {
++i;
return ++i;
} finally {
++i;
System.out.println("i: " + i);
}
}
i: 3
result: 2
Question 2 - 值传递和引用传递
public static void main(String[] args) {
StringBuffer x = new StringBuffer("a");
StringBuffer y = new StringBuffer("b");
change(x, y);
System.out.println(x);
System.out.println(y);
String m = "m";
String n = "n";
change(m, n);
System.out.println(m);
System.out.println(n);
}
public static void change(StringBuffer x, StringBuffer y) {
x.append(y);
y = x;
System.out.println("StringBuffer: " + y);
}
public static void change(String x, String y) {
x = x + y;
y = x;
System.out.println("String: " + y);
}
StringBuffer: ab
ab
b
String: mn
m
n
Question 3 - 类的加载顺序
public class B extends A {
private String address = address();
{
System.out.println("B none static code block.");
}
private static int sex = sex();
static {
System.out.println("B static code block.");
}
public B() {
System.out.println("B Constructor init.");
}
public B(String address) {
this.address = address;
System.out.println("B Constructor(address) init.");
}
private String address() {
System.out.println("B property address");
return "address";
}
private static int sex() {
System.out.println("B static property sex");
return 1;
}
public static void main(String[] args) {
System.out.println("main start...");
new B();
}
}
class A {
private String name = name();
private static int count = count();
{
System.out.println("A none static code block.");
}
static {
System.out.println("A static code block.");
}
public A() {
System.out.println("A Constructor init.");
}
private String name() {
System.out.println("A property name");
return "name";
}
private static int count() {
System.out.println("A static property count");
return 1;
}
}
A static property count
A static code block.
B static property sex
B static code block.
main start…
A property name
A none static code block.
A Constructor init.
B property address
B none static code block.
B Constructor init.
类的加载顺序
- 静态: 父类优先, 静态变量和代码块按照顺序加载
- 非静态: 父类优先, 成员变量和代码块按照顺序加载
- 构造函数: 父类优先
Question 4 - SpringMvc中获取Request
- Controller 中加参数
- 自动注入
- 手动调用
- @ModelAttribute 方法
// Controller 中加参数
@Controller
public class AALController {
@RequestMapping("/answer")
public void answer(HttpServletRequest request) {
......
}
}
// 自动注入
@Controller
public class AALController {
// 自动注入 request
@Autowired
private HttpServletRequest request;
@RequestMapping("/answer")
public void answer() throws InterruptedException{
......
}
}
// 手动调用
@Controller
public class AALController {
@RequestMapping("/answer")
public void answer() throws InterruptedException {
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
.......
}
}
// @ModelAttribute 方法
@Controller
public class AALController {
// 此处 request 非线程安全
private HttpServletRequest request;
@ModelAttribute
public void bindRequest(HttpServletRequest request) {
// 此处 request 线程安全
this.request = request;
}
@RequestMapping("/answer")
public void answer() throws InterruptedException {
......
}
}