前言
第一题
看一下equals的源码:
public boolean equals(Object anObject)
{
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n ==
anotherString.value.length) {
char v1[] =
value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
很明显。char[]不是String的对象所以返回肯定是false了。
作者:ProJay
链接:https://www.nowcoder.com/exam/test/82773902/submission?pid=58406314&pageSource=testHistory
来源:牛客网
在使用equals方法时,会判断参数类型是否是String类型,如果是,执行后续逻辑,如果不是,直接返回false
第二题
摘自牛客用户:计算机小霸王
运行时异常: 都是RuntimeException类及其子类异常,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。这些异常一般是由程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生。
常见的运行异常:
NullPointerException(空指针异常)
IndexOutOfBoundsException(下标越界异常)
ClassNotFoundException (指定的类不存在)
NoSuchMethodError (方法不存在错误)
NumberFormatException (数字格式异常)
ArrithmeticException(算数异常)
运行时异常的特点是Java编译器不会检查它,也就是说,当程序中可能出现这类异常,即使没有用try-catch语句捕获它,也没有用throws子句声明抛出它,也会编译通过。
编译异常: 是RuntimeException以外的异常,类型上都属于Exception类及其子类。从程序语法角度讲是必须进行处理的异常,如果不处理,程序就不能编译通过。
常见的编译异常: IOException(包括FileNotFoundException)、SQLException等以及用户自定义的Exception异常, 一般情况下不自定义检查异常。
原文链接:https://blog.csdn.net/sdau_20171819/article/details/122794299
作者:代码裁缝
链接:https://www.nowcoder.com/exam/test/82775938/submission?examPageSource=Intelligent&pid=58407156&testCallback=https%3A%2F%2Fwww.nowcoder.com%2Fexam%2Fintelligent%3FquestionJobId%3D10
来源:牛客网