5 java Exception相关 | abstract final | == equals

public class ExceptionTest{
	public void doSomething() throws ArithmeticException{
		System.out.println("do something");
	 }
	public static void main(String[] arge){
		ExceptionTest test = new ExceptionTest();
		test.doSomething();
	 }
 }

是否可以编译通过?

可以编译通过,因为ArithmeticExcetpion运行时异常,所以不用显示的try catch。

java 中异常分为两类:

1,checked exception

(1)继续抛出,消极做法,直到抛到JVM

(2)try...catch

2,unchecked exception (runtime exception)

------------------------------------------------------------------

一个类的声明可否既是abstract的有是final的?为什么?

不能。

abstract要求必须被继承,才可以实例化,而final则要求不能被继承,互相矛盾,所以一个类的声明不能同时用abstract和final两个修饰符。

一个抽象类里不一定包含抽象方法。

但是一个类里有抽象方法,则必须是抽象类。


为什么对于一个public类型的终态的成员变量,一般都要声明为static的?

public static final String s = "abc";

节省内存。

-------------------------------------------------------------------

String str = new String("abc");
String str2 = new String("abc");

System.out.println(str == str2);
System.out.println(str.equals(str2));
输出:

false

true

----

Object object = new Object();
Object object2 = new Object();

System.out.println(object == object2);
System.out.println(object.equals(object2));
输出:

false

false

由于Object类里 equals 方法为 this == ojb 所以返回false

---------------

class Person{
	String name;
	public Person(String name){
		this.name = name;
	 }
 }

Person p1 = new Person("zhangsan");
person p2 = new Person("zhangsan");
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));

输出:

false

false




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值