我想知道为什么Java编译器会接受以下代码:
public class Main {
public static void main(String ... args){
System.out.println("a() = " + a());
}
public static String a (){
try {
return "a";
}catch(Throwable t){
}finally{
return "b";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这可以也不应该奏效.java规范声明finally将始终执行块,但同时已指定返回值.所以要么你不能执行该return "b"语句,因为你已经退出return "a",这是不正确的.
但是,另一个选择是执行该return "b"语句,从而完全忽略该return "a"语句......
我会说两者都是错的,我希望这不会编译.然而,它编译并运行良好.我将把答案作为一个很好的练习留给读者;).
基本上我的问题是:除了不好的做法,这会被认为是一个Java错误,还是除了混淆之外还有其他奇妙的用途?
问题不是如果它是一个bug,已经得到了回答,但它有不错的用例吗?
鉴于:
我想要的是:
我的方法:
可能的问题:

我的问题:
到目前为止我的修复:
我对这个修复不太满意,因为我觉得我错过了一个更优雅的解决方案,我的修复感觉相当"hacky".效率是关键,因为它用于实时嵌入式系统.
现有的代码库是用C++编写的,所以如果你想用特定的语言编写,C++就有我的偏好.谢谢!
[编辑] 我改变了我的修复,从垂直点到平行点,因为我认为跟随线段比计算向外法线更容易.
我正在开发一个android应用程序,其主要用途是在设定的时间显示通知(某些特定的日历应用程序).其中一个主要的抱怨是,用户不会(总是)接收通知,而我最终会陷入困境.
我们在仿真器上对Android 4.4,6,7.0,7.1,8.0,8.1进行了内部测试,并使用了大约10个真实设备(6到8.1),所有设备都按时收到了通知.即使在重新启动时,通知也都按时收到.
我们遇到的一件事是Samsung设备上的SecurityException(> 500个已注册的警报),我们之前因为无法取消而触发了这些警报.它看起来不再是一个问题.
那么,这些缺失通知的原因是什么?它是特定于设备的设置,它是一个简单的错误吗?或者还有其他因素在这里发挥作用?
这是我们使用的代码:
private void cancelAlarm(String notificationId, Class<? extends AbstractReceiver> receiverClass)
throws BroadcastException {
/*
* Create an intent that looks similar, to the one that was registered using add. Making sure the notification id in the action is the same. Now we can search for
* such an intent using the 'getService' method and cancel it.
*/
final Intent intent = new Intent(this.context, receiverClass);
intent.setAction(notificationId);
final PendingIntent pi = PendingIntent.getBroadcast(this.context, 0, …Run Code Online (Sandbox Code Playgroud) 让我们说我有一个数组如下:
String[] letter = {a, b, c, e, f, }
Run Code Online (Sandbox Code Playgroud)
我怎样才能修剪这个数组以摆脱空元素?