在尝试多捕获功能时,我在我的m1()方法中发现一切都按预期正常工作。
但是,在m2()相同的代码中不能编译。我刚刚更改了语法以减少代码行数。
public class Main {
public int m1(boolean bool) {
try {
if (bool) {
throw new Excep1();
}
throw new Excep2();
//This m1() is compiling abs fine.
} catch (Excep1 | Excep2 e) {
return 0;
}
}
public int m2(boolean b) {
try {
throw b ? new Excep1() : new Excep2();
//This one is not compiling.
} catch (Excep1 | Excep2 e) {
return 0;
}
}
private static interface …Run Code Online (Sandbox Code Playgroud) 从我的理解下面我写了不应该编译的语句代码"我不可达"是后return.
但是,编译绝对正常.
同样来自JLS:无法访问的语句,它不应该编译.
来自规范14.21无法到达的声明:
如果满足以下两个条件,则try语句可以正常完成:
try块可以正常完成,或者任何catch块都可以正常完成.
如果try语句有finally块,则finally块可以正常完成.
这里的try块无法正常完成,但catch块可以和finally块一样,所以我在这里很困惑
public class Test1 {
public static void main(String[] args) {
try {
return;
} catch (Exception e) {
System.out.println("catch");
} finally {
System.out.println("finally");
}
System.out.println("I am unreachable??!!!");
}
}
Run Code Online (Sandbox Code Playgroud)
有人能帮我理解这种行为吗?
在执行以下代码时,我得到一个NullPointerException在线:
value = condition ? getDouble() : 1.0;
Run Code Online (Sandbox Code Playgroud)
在早期的行中,当我使用null而不是getDouble()一切都有效时,这很奇怪.
public class Test {
static Double getDouble() {
return null;
}
public static void main(String[] args) {
boolean condition = true;
Double value;
value = condition ? null : 1.0; //works fine
System.out.println(value); //prints null
value = condition ? getDouble() : 1.0; //throws NPE
System.out.println(value);
}
}
Run Code Online (Sandbox Code Playgroud)
有人能帮我理解这种行为吗?
据我所知,应该打印以下代码False,但是当我运行此代码时,它正在打印True.
来自Java文档:
如果整数参数包含abstract修饰符,则返回true,否则返回false.
public class Test{
public static void main(String[] args) {
System.out.println(Modifier.isAbstract(byte[].class.getModifiers()));
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我理解这种行为吗?
据我了解,下面的代码应该打印出来true.
但是,当我运行此代码时,它正在打印false.
匿名类总是隐式最终的
public class Test {
public static void main(String args[]) {
Object o = new Object() {
};
System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers()));
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我理解这种行为.
在使用树集时,我发现了非常奇特的行为.根据我的理解,这个程序应该打印两个相同的行:
public class TestSet {
static void test(String... args) {
Set<String> s = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
s.addAll(Arrays.asList("a", "b"));
s.removeAll(Arrays.asList(args));
System.out.println(s);
}
public static void main(String[] args) {
test("A");
test("A", "C");
}
}
Run Code Online (Sandbox Code Playgroud)
但奇怪的是它打印:
[b]
[a, b]
Run Code Online (Sandbox Code Playgroud)
为什么树集会表现得像这样?
据我所知,lambda表达式捕获值而不是变量.例如,以下是编译时错误:
for (int k = 0; k < 10; k++) {
new Thread(() -> System.out.println(k)).start();
// Error—cannot capture k
// Local variable k defined in an enclosing scope must be final or effectively final
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用增强版运行相同的逻辑时,for-loop一切正常:
List<Integer> listOfInt = new Arrays.asList(1, 2, 3);
for (Integer arg : listOfInt) {
new Thread(() -> System.out.println(arg)).start();
// OK to capture 'arg'
}
Run Code Online (Sandbox Code Playgroud)
为什么它适用于增强for循环而不适用于正常循环,尽管增强for循环也在内部通过正常循环来增加变量.**
我们可以发现hashcode的list是本身含有的element?
我知道这是一个不好的做法,但这是面试官问的。
当我运行以下代码时,它会抛出一个StackOverflowError:
public class Main {
public static void main(String args[]) {
ArrayList<ArrayList> a = new ArrayList();
a.add(a);
a.hashCode();
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有两个问题:
StackOverflowError?根据我的理解,下面的代码应该false在进行identity基础比较时进行打印.
但是,当我运行以下代码时,它正在打印true:
public class Test1 {
public static void main(String[] args) {
IdentityHashMap m = new IdentityHashMap();
m.put("A", new String("B"));
System.out.println(m.remove("A", new String("B")));
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我理解这种行为吗?
在学习Java 9时,我遇到了一种新的Thread类方法,叫做onSpinWait?.根据javadocs,这个方法用于此:
表示调用者暂时无法进展,直到其他活动发生一个或多个操作为止.
有人能帮助我理解这种方法给出一个真实的例子吗?