我已经安装了JDK 8并尝试运行Eclipse.我收到以下警告信息:
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512m;
support was removed in 8.0
Run Code Online (Sandbox Code Playgroud)
忽视这个论点的原因是什么?
我知道有很多关于如何获得的问题,但我想要和使用新的Java 8 Date api的例子.我也知道JodaTime库,但我想要一种没有外部库的工作方式.
功能需要抱怨这些限制:
Java 8引入了默认方法,以提供扩展接口的能力,而无需修改现有实现.
我想知道,当该方法被覆盖或由于不同接口中的冲突默认实现不可用时,是否可以显式调用方法的默认实现.
interface A {
default void foo() {
System.out.println("A.foo");
}
}
class B implements A {
@Override
public void foo() {
System.out.println("B.foo");
}
public void afoo() {
// how to invoke A.foo() here?
}
}
Run Code Online (Sandbox Code Playgroud)
考虑到上面的代码,你会如何A.foo()从B类方法调用?
我需要在我的项目中使用Web服务.我使用NetBeans,所以我右键单击我的项目并尝试添加一个新的"Web服务客户端".上次我检查时,这是创建Web服务客户端的方法.但它导致了一个AssertionError,说:
java.lang.AssertionError:org.xml.sax.SAXParseException; systemId:jar:file:/path/to/glassfish/modules/jaxb-osgi.jar!/com/sun/tools/xjc/reader/xmlschema/bindinfo/binding.xsd; lineNumber:52; columnNumber:88; schema_reference:无法读取模式文档" xjc.xsd ",因为由于accessExternalSchema属性设置的限制,不允许"文件"访问.
NetBeans的默认Java平台是JDK8(Oracle的正式版),因此当我更改我的netbeans.conf文件并将JDK7(也来自Oracle)作为我的默认版本时,一切正常.所以我认为问题出在JDK8上.这是我的java -version输出:
java版"1.8.0"
Java(TM)SE运行时环境(版本1.8.0-b132)
Java HotSpot(TM)64位服务器VM(版本25.0-b70,混合模式)
现在,我将JDK7作为我的默认Java平台.如果有办法使JDK8工作,请分享.
注意:这个问题源于一个死链接,这是以前的SO问题,但是这里......
看到这个代码(注意:我不知道,这个代码将不能"工作",而Integer::compare应使用-我只是提取它从链接的问题):
final ArrayList <Integer> list
= IntStream.rangeClosed(1, 20).boxed().collect(Collectors.toList());
System.out.println(list.stream().max(Integer::max).get());
System.out.println(list.stream().min(Integer::min).get());
Run Code Online (Sandbox Code Playgroud)
据javadoc的.min()和.max(),两者的参数应该是一个Comparator.然而,这里的方法引用是Integer类的静态方法.
那么,为什么要编译呢?
我对该Function.identity()方法的用法有疑问.
想象一下以下代码:
Arrays.asList("a", "b", "c")
.stream()
.map(Function.identity()) // <- This,
.map(str -> str) // <- is the same as this.
.collect(Collectors.toMap(
Function.identity(), // <-- And this,
str -> str)); // <-- is the same as this.
Run Code Online (Sandbox Code Playgroud)
是否有任何理由你应该使用Function.identity()而不是str->str(反之亦然).我认为第二种选择更具可读性(当然是品味问题).但是,有没有"真正的"理由为什么应该首选?
在Java 8中,我可以轻松地写:
interface Interface1 {
default void method1() {
synchronized (this) {
// Something
}
}
static void method2() {
synchronized (Interface1.class) {
// Something
}
}
}
Run Code Online (Sandbox Code Playgroud)
我将获得完全同步语义,我也可以在类中使用.但是,我不能synchronized在方法声明上使用修饰符:
interface Interface2 {
default synchronized void method1() {
// ^^^^^^^^^^^^ Modifier 'synchronized' not allowed here
}
static synchronized void method2() {
// ^^^^^^^^^^^^ Modifier 'synchronized' not allowed here
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我们可以认为,这两个接口的行为方式相同,只是Interface2建立了一个合同上的method1()和method2(),这是比强一点Interface1呢.当然,我们也可能会争辩说default实现不应该对具体实现状态做出任何假设,或者这样的关键字根本不会减轻它的重量.
JSR-335专家组决定不支持synchronized接口方法的原因是什么?
有时您想要Stream使用多个条件过滤a :
myList.stream().filter(x -> x.size() > 10).filter(x -> x.isCool()) ...
Run Code Online (Sandbox Code Playgroud)
或者你可以用复杂的条件和单一的方式 做同样的事情filter:
myList.stream().filter(x -> x.size() > 10 && x -> x.isCool()) ...
Run Code Online (Sandbox Code Playgroud)
我的猜测是第二种方法具有更好的性能特征,但我不知道.
第一种方法在可读性方面取胜,但性能更好?
使用Java 8 Optional类时,有两种方法可以将值包装在可选项中.
String foobar = <value or null>;
Optional.of(foobar); // May throw NullPointerException
Optional.ofNullable(foobar); // Safe from NullPointerException
Run Code Online (Sandbox Code Playgroud)
我理解Optional.ofNullable是唯一安全的使用方式Optional,但为什么Optional.of存在呢?为什么不Optional.ofNullable 随时使用并保持安全?
我知道:
仍然最终IMO都可以作为大多数应用程序用例的类型.例如:目前我正在运行一个批处理作业,我需要根据日期计算下一次运行,我很难找到这两种类型之间的优缺点(除了Instant的纳秒级精度优势和时区部分) of LocalDateTime).
您能说出一些应用示例,其中只应使用Instant或LocalDateTime吗?
编辑:注意有关精度和时区的LocalDateTime的误读文档
java-8 ×10
java ×9
java-stream ×2
lambda ×2
datetime ×1
days ×1
filter ×1
inheritance ×1
interface ×1
java-time ×1
jsr335 ×1
jvm ×1
netbeans-8 ×1
null ×1
optional ×1
permgen ×1
synchronized ×1