第一题:
public class StringConstant {
public static void main(String[] args) {
String a = "abc";judge(a,"abc");String b = new String("abc");judge(a,b);
String c = "a" +"bc";judge(a,c);String d = new String("a"+"bc");judge(a,d);
String d = "a";
String f = "bc";
String g = d +f ;
judge(a,g);
String h = "c";
String i = "ab"+h;
judge(a,i);
final String k ="c";
String l = "ab" + k;
judge(a,l);
final String m = get();
String n = "a"+m+"c";
judge(a,n);
judge(n.intern(),a)
}
static String get(){
return "b";
}
static void judge(String a ,String b){
}System.out.println((a==b));
}
第二题:
public class StringConstant {
static {
System.out.println("StringConstant static region");
}
StringConstant(){
System.out.println("StringConstant construct");
}
public static void main(String[] args) {
new DesignMode();
}
}
class DesignMode extends StringConstant{
static {
System.out.println("DesignMode static region");
}
DesignMode(){
System.out.println("DesignMode construct");
}
}
第三题:
public class ReturnFinally
{
public static void main(String[] args)
{
//用例 1:
try
{
throwException();
} catch (Exception e) {
System.out.println(" catch 1 ");
return;
} finally {
System.out.println(" finally 1");
}
// 用例 2:
int i = 0;
i = subFunc();
System.out.println(i);
}
private static void throwException() throws Exception {
try {
double d = 1 / 0;
} catch (Exception e) {
System.out.println(" catch 2 :" + e.getMessage());
return;
} finally {
System.out.println("finally 2");
throw new Exception();
}
}
private static int subFunc() {
try{
System.out.println(" try subFunc ... ...");
}catch(Exception e){throw new Exception();
System.out.println(" catch subFunc ... ...");
//注释 和 不注释 下面语句 区别
System.exit(0);
return 0;
}finally{
System.out.println(" finally subFunc ... ...");
return 1;
}
}
}