首页 > 编程 > Java > 正文

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

2019-11-08 02:08:33
字体:
来源:转载
供稿:网友

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表