myeclipse 异常try catch 快捷键:alt + shift + z
public class aaa {
public static void main(String[] args) {
aaa t = new aaa();
int a = t.test();
System.out.println(a);
}
public int test(){
String a = "yangliwei";
System.out.println(a.length());
System.out.println(a.substring(2, 4));
System.out.println(a.indexOf("a"));
System.out.println(a.charAt(3));
try{
System.out.println(1/0);
return 1;
}catch(Exception e){
e.printStackTrace();
return 2;
}finally{
System.out.println("aa");
}
}
}
字符串比较
String s1="asd";
String s2=new String("asd");
String s3="asd";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1==s3);
Integer id=1;
int id2=1;
Double d=new Double(12.1);
System.out.println(id==id2);
Integer 是int 的类,可以用来作为对象比较
例如:Integer a = new Integer(1); 或者 Integer a=1; 和String 一样
直接赋值是直接把值放入堆内存,再次赋值不会申请内存,
new 关键字会申请内存,所以 s1和s2不相等,
“==” 是用来比较字符串的内存地址,
用.equals来比较字符串的内容
堆内存是:先进先出
栈内存:先进后出