是否可以忽略使用try-with-resources语句关闭资源时抛出的异常?
例:
class MyResource implements AutoCloseable{
@Override
public void close() throws Exception {
throw new Exception("Could not close");
}
public void read() throws Exception{
}
}
//this method prints an exception "Could not close"
//I want to ignore it
public static void test(){
try(MyResource r = new MyResource()){
r.read();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
或者我应该继续关闭finally?
public static void test2(){
MyResource r = null;
try {
r.read();
}
finally{
if(r!=null){
try {
r.close(); …Run Code Online (Sandbox Code Playgroud) 在Java 7多个分块中,如下所示:
try {
// code that throws exception
} catch (CharacterCodingException | UnknownServiceException ex) {
// handle exception
}
Run Code Online (Sandbox Code Playgroud)
什么是编译时类型ex?它是两个异常类型共同的派生类最多的类吗?在这个例子中,这将是一个IOException.
此代码在Java 8中编译,但无法在Java 7中编译:
class Map<K,V> {
static <K,V> Map<K,V> empty() {return null;}
Map<K,V> put(K k, V v) {return null;}
V get(K k) {return null;}
}
class A {
static void f(Map<Integer,String> m){}
public static void main(String[] args) {
f(Map.empty());
}
}
Run Code Online (Sandbox Code Playgroud)
它不会推断出Map返回的具体类型Map.empty():
$ javac7 A.java
A.java:10: error: method f in class A cannot be applied to given types;
f(Map.empty());
^
required: Map<Integer,String>
found: Map<Object,Object>
reason: actual argument Map<Object,Object> cannot be converted to Map<Integer,String> by method invocation …Run Code Online (Sandbox Code Playgroud) 这是我关于SO的第一个问题,我很困惑,还没有类似的问题!
所以问题是:
为什么不尝试使用资源来处理字段变量?
换句话说:为什么我总是需要一个局部变量呢?
这里有一些示例代码:
public class FileWriteTest {
public FileWriter file;
public void workingDemo() {
try(FileWriter file = new FileWriter(new File("someFilePath")) {
// do something
} catch (IOException e) {
e.printStackTrace();
}
}
public void notWorkingDemo() {
file = null;
try(file = new FileWriter(new File("someFilePath")) {
// do something
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
愿任何人解释我为什么有这个约定?
我正在看下面的Stack Overflow答案: 如何在运行时更改Spring的@Scheduled fixedDelay
在代码中有以下行:
schedulerFuture = taskScheduler.schedule(() -> { }, this);
Run Code Online (Sandbox Code Playgroud)
我想知道lambda () -> {}在该代码中的含义.我需要在不使用lambdas的情况下编写它.
开始用C#学习LINQ.
特别是LINQ to Objects和LINQ to XML.
我非常喜欢LINQ的强大功能.
我了解到有一种叫做JLINQ的Jscript实现.
另外(正如Catbert发布的那样)Scala将拥有LINQ
你知道LINQ或类似的东西是否会成为Java 7的一部分吗?
更新:2008年有趣的帖子 - LINQ for Java工具
我想拥有EnumSet的所有效率并传递它,而不必担心有人会修改它.
在Java 6之前,我们有一个恒定的时间子串String.在Java 7中,他们为什么决定使用复制char数组 - 并降低线性时间复杂度 - 当这样的东西StringBuilder完全意味着什么?
免责声明:我知道签署APK的基本知识,我只对我的一个项目有问题,并且只有在使用Microsoft Windows作为操作系统进行签名时才会出现问题.
我正在使用Maven构建我的APK 并使用maven-jarsigner-plugin进行签名:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<executions>
<execution>
<id>signing</id>
<goals>
<goal>sign</goal>
</goals>
<phase>package</phase>
<inherited>true</inherited>
<configuration>
<archive>target/${project.build.finalName}.apk</archive>
<sigfile>CERT</sigfile>
<keystore>${env.HOME}/.keystore</keystore>
<storepass>${env.KEY_STOREPASS}</storepass>
<keypass>${env.KEY_KEYPASS}</keypass>
<alias>${env.KEY_ALIAS}</alias>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
现在有了我的一个项目,在尝试安装和启动应用程序时出现以下错误:
1360 KB/s (2057872 bytes in 1.477s)
pkg: /data/local/tmp/FX-602P-Droid-5.0.0.apk
Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]
Run Code Online (Sandbox Code Playgroud)
正如我在第一句中所说:在使用Mac OS X进行签名时,它工作正常.只有在使用Windows 7进行签名时才会失败.
--debug在调用Maven时使用我看到jarsigner被称为ok并且没有报告错误:
[INFO]
[INFO] --- maven-jarsigner-plugin:1.2:sign (signing) @ FX-602P-Droid ---
[DEBUG] org.apache.maven.plugins:maven-jarsigner-plugin:jar:1.2:
[DEBUG] org.apache.maven:maven-plugin-api:jar:2.0.6:compile
[DEBUG] org.apache.maven:maven-project:jar:2.0.6:compile
[DEBUG] org.apache.maven:maven-settings:jar:2.0.6:compile
[DEBUG] org.apache.maven:maven-profile:jar:2.0.6:compile
[DEBUG] org.apache.maven:maven-model:jar:2.0.6:compile
[DEBUG] org.apache.maven:maven-artifact-manager:jar:2.0.6:compile
[DEBUG] org.apache.maven:maven-repository-metadata:jar:2.0.6:compile
[DEBUG] org.apache.maven:maven-plugin-registry:jar:2.0.6:compile
[DEBUG] org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile
[DEBUG] junit:junit:jar:3.8.1:compile
[DEBUG] classworlds:classworlds:jar:1.1-alpha-2:compile
[DEBUG] org.apache.maven:maven-artifact:jar:2.0.6:compile …Run Code Online (Sandbox Code Playgroud) 情况1
static void call(Integer i) {
System.out.println("hi" + i);
}
static void call(int i) {
System.out.println("hello" + i);
}
public static void main(String... args) {
call(10);
}
Run Code Online (Sandbox Code Playgroud)
案例1的输出:hello10
案例2
static void call(Integer... i) {
System.out.println("hi" + i);
}
static void call(int... i) {
System.out.println("hello" + i);
}
public static void main(String... args) {
call(10);
}
Run Code Online (Sandbox Code Playgroud)
显示编译错误reference to call ambiguous.但是,我无法理解.为什么?但是,当我评论出任何call()方法时Case 2,它的工作正常.任何人都可以帮助我理解,这里发生了什么?