java 异常,字符串比较,内存堆栈

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来比较字符串的内容

堆内存是:先进先出

栈内存:先进后出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值