请检查以下程序.
我怀疑编译器何时会在编译器级别发出转换异常,何时它会在runtime?
喜欢下面的程序,表达
我认为(Redwood) new Tree()应该在编译时失败,因为Tree不是Redwood.但它没有失败compile time,正如预期的那样失败runtime!!!
public class Redwood extends Tree {
public static void main(String[] args) {
new Redwood().go();
}
void go() {
go2(new Tree(), new Redwood());
go2((Redwood) new Tree(), new Redwood());
}
void go2(Tree t1, Redwood r1) {
Redwood r2 = (Redwood)t1;
Tree t2 = (Tree)r1;
}
}
class Tree { }
Run Code Online (Sandbox Code Playgroud) 任何人都知道为什么编译器不能在'short'中输出值'7'?显式转换正在工作,但在传递参数时它不起作用!
class Alien {
String invade(short ships) { return "a few"; }
String invade(short... ships) { return "many"; }
}
public class Wind {
public static void main(String [] args) {
short temp = 7;
System.out.println(new Alien().invade(7));
}
}
Run Code Online (Sandbox Code Playgroud) 任何人都可以解释为什么这个程序给出输出9?我猜测"不可预测的行为"作为答案,因为主线程和线程0可以按任何顺序执行.join()会对主线程本身做什么?
public class Starter extends Thread{
private int x = 2;
public static void main(String[] args) throws Exception{
new Starter().makeItSo();
}
public Starter(){
//Thread main
x=5;
start();
}
public void makeItSo() throws Exception{
//Thread main
join();
x = x-1;
System.out.println(x);
}
public void run(){
//Thread-0
x*= 2;
}
}
Run Code Online (Sandbox Code Playgroud)
主线程启动线程0并调用run方法.但如果你把SOP放在makeItSo()中,它会说主线程在调用join()之后在那里等待!为什么?我认为makeItSo()或run()之间没有序列,所以X的值将是不可预测的.