# 下面程序段执行后 b 的结果是什么?

1
2
Integer integ =new Integer(9);
boolean b = integ instanceof Object;

A instanceof B: 表示 A 是否是 B 的实例,返回一个 boolean 值


这里的 integ 是 Integer 对象,然后 Integer 是 Object 的实例,所以返回 True。

# 若有下列定义,下列哪个表达式返回 false?

1
2
3
String s = "hello";
String t = "hello";
char c[] = {'h', 'e', 'l', 'l', 'o'} ;

答案在程序中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Demo03 {
public static void main(String []args) {
String s = "hello";
String t = "hello";
char c[] = {'h','e','l','l','o'} ;
System.out.println(s.equals(t));//true
//字符串数组不等于字符串
System.out.println(t.equals(c));//false
System.out.println(s==t);//true
System.out.println(t.equals(new String("hello")));//true
//这个不相等,因为语句中new的字符串不在常量池,是在堆
System.out.println(t==new String("hello"));//false
//这样可以判断字符数组与字符串是否包含同样的字符序列
System.out.println(t.equals(new String(c)));//true
}
}

# 结果是?

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Test {
public static void main(String[] args) {
System.out.println("return value of getValue(): " +
getValue());
}
public static int getValue() {
try {
return 0;
} finally {
return 1;
}
}
}

如果 try 语句中有 return,返回的是 try 语句中变量值


如果 try,finally 语句中均有 return,忽略 try 中的 return,而使用 finally 中的 return

故结果为 return value of getValue (): 1

# 在一个基于分布式的游戏服务器系统中,不同的服务器之间,哪种通信方式是不可行的?

A. 管道

B. 消息队列

C. 高速缓存数据库

D. 套接字

答案是管道:管道是一种半双工的通信方式,数据只能单向流动,主要是在父子进程中使用。不同的服务器进行通信没有相互之间的父子进程。

# finally 语句的使用

  1. 不管有没有异常发生,finally 块中的代码都会执行
  2. 当 try 和 catch 中有 return 时,finally 依然会执行。
  3. finally 中最好不要有 return,否则程序会提前退出,返回值不是 try 和 catch 中的返回值。

# switch 语句中的条件可以是哪些类型的数据。

以 java8 为准,switch 支持 10 种类型

基本类型:byte char short int

对于包装类 :Byte,Short,Character,Integer String enum Java 实际只能支持 int 类型的 switch 语句,那其他的类型时如何支持的

a、基本类型 byte char short 原因:这些基本数字类型可自动向上转为 int, 实际还是用的 int。


b、基本类型包装类 Byte,Short,Character,Integer 原因:java 的自动拆箱机制 可看这些对象自动转为基本类型 c、String 类型 原因:实际 switch 比较的 string.hashCode 值,它是一个 int 类型


c、enum 类型 原因 :实际比较的是 enum 的 ordinal 值(表示枚举值的顺序),它也是一个 int 类型 所以也可以说 switch 语句只支持 int 类型