Java SE基础四

一、异常:

所有的异常类都是java.lang.Exception类继承的子类。
Throwable类是顶级父类。
1、Exception还能细分:
RuntimeException:ClassCastException(强制类型转换)、IndexOutOfBoundException(数组越界)、NullPointException(空指针)
如上不需要我们手动处理,JVM运行时都会主动抛出这个错误。
Exception in thread “main” java.lang.NullPointerException

2、非RuntimeException
文件不存在、连接错误、需要我们自己写代码来抛出这个错误;
try catch处理然后发邮件、钉钉。

1.2:如何处理异常 try catch finally

try{执行代码}
	catch(Exception e) {
		//异常处理
} finally{
		//善后操作
}
//声明一个数组,有四个元素,使用a[4]获取第五个元素,就会抛出一个异常。
package day04;

public class ExcepTest {

    public static void main(String args[]) {
        try {
            int a[] = new int[]{1,2,3,4};
            System.out.println("Access element " + a[4]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Exception thrown: " + e);
        } finally {
            System.out.println("john is very cool");
        }
    }
}

public static void main(String[] args){
	FileWrite fw = null;

	try{
		fw = new FileWrite("a.txt");
		fw.write("今天天气好晴朗");
}catch (IOException e){
		System.out.println(e.getMessage());
}catch (Exception e){
		System.out.println(e.getMessage());
}	finally{
	System.out.println("11111");
	}
}

自定义异常信息:

IDEA新建UdException.java
package day04;

public class UdException extends Exception {
    public UdException() {

    }

    public UdException(String var1) {
        super(var1);
    }
}

新建UdTest.java
package day04;

public class UdTest {
    public static void test(int i) {
        if (i < 0) {
            try {
                throw new UdException("请输入正数");
            } catch (UdException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        test(-1);			//测试-1作为参数输入
    }
}
第一次的输出结果:
java.lang.Exception: 请输入正数
	at day04.UdTest.test(UdTest.java:7)
	at day04.UdTest.main(UdTest.java:15)

改写:UdTest.java这个类:
package day04;

public class UdTest {
    public static void test(int i) throws UdException{
        if (i < 0) {
                throw new UdException("请输入正数");
            }
        }


    public static void main(String[] args) {
        try {
            test(-1);
        } catch (UdException e) {
            System.out.println(e.getMessage());
        }
    }
}
第二次的输出结果:
请输入正数

二、String

String还是java.lang包下的一个。

面试题:为什么Java中的String是不可变的。
String是用数组存数据的,而这个数组又用final修饰,所以它是不可变的。

public static void main(String[] args) {
        String s = "john";
        new String("sail");
        s = s + "sail";			//此时已经变成了一个新的对象了
        System.out.println(s);
    }
    输出sail,那是不是数组发生了变化呢???

其实s + “sail” 这一步时已经生成了一个新的。

2.2String中的concat方法

concat会返回一个新的对象,需要拿s1去接。

public class StringTest {
    public static void main(String[] args) {
        String s = "john";
        new String("sail");
        s = "sail";
        String s1 = s.concat("+333");
        System.out.println(s1);
    }
}
运算结果:sail+333

2.3StringBuilder和StringBuffer

StringBuffer ruoze = new StringBuffer("ruoze"); ruoze.append("&jepson"); System.out.println(ruoze);
输出:ruoze&jepson

经典面试题,安能辨我是雌雄?

public static void main(String[] args){
	Object o1 = new Object();
	Object o2 = new Object();
	System.out.println(o1.equals(o2));
	System.out.println(o1 == o2);

	String s1 = new String("1");
	String s2 = new  String("2");
	System.out.println(s1.equals(s2));			//true
	System.out.println(s1 == s2);			//false,判断内存地址,因为都是通过new,所以两个对象内存地址不同???

	 String s3 = "1";
        String s4 = "1";
        System.out.println(s3.equals(s4));			//true
        System.out.println(s3 == s4);					//s3被赋值为1,给分配了一个内存空间,s4也是1,两个的分配地址是一样的。
}

三、集合

1、概念:一个容器中装有相同属性的东西,和数组比较像

2、集合和数组的区别:

  • 集合的长度不固定
  • API超丰富
    我们去改变数组的长度要使用index操作

3、Collection
Set:无序,不重复排列
HashSet
List:有序的,可重复
ArrayList
LinkedList
Quene:有序的,可重复
ArrayBlockingQuene
LinkedList
ConcurrentLinkedQuene
1、底层数据结构
2、同步问题(锁)、阻塞
4、Map
HashMap
ConcurrentHashMap

自己去测试ArrayList.class中的增删改差操作;
Quene.class中的方法。

Quene的应用场景:
启动一个server,用于接收HTTP的请求,将请求到的数据把它落地到磁盘,要求异步?
使用ArrayBlockingQuene
线程1:接收请求,把请求丢到队列中
线程2:从队列中去拉去数据。

Collection.class中也提供了很多的API

四、值传递和引用传递

Java中只有值传递

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值