Java 10的发布带来了新的静态工厂方法,特别是:
static <E> List<E> copyOf?(Collection<? extends E> coll)static <E> Set<E> copyOf?(Collection<? extends E> coll)static <K,V> Map<K,V> copyOf?(Map<? extends K,? extends V> map)看到这些方法允许我们将Collections 复制到不同的Collection实现中,它们如何与现有方法进行比较和对比?
我记得在某处读过带有推断类型的局部变量可以用相同类型的值重新分配,这是有意义的.
var x = 5;
x = 1; // Should compile, no?
Run Code Online (Sandbox Code Playgroud)
但是,我很好奇如果要重新分配x给不同类型的对象会发生什么.这样的东西还会编译吗?
var x = 5;
x = new Scanner(System.in); // What happens?
Run Code Online (Sandbox Code Playgroud)
我目前无法安装JDK 10的早期版本,并且不想等到明天才能找到答案.
如果我理解正确,Java 10的二进制形式与Java 9完全相同,那么它不会产生任何问题.
我的假设是否正确?
我正在研究一个问题,以便在彼此之间存储两个类的引用
例如:
class A {
B b;
A(B b){
this.b = b;}
}
class B {
A a;
B(A a){
this.a = a;}
}
public static void main(String...s){
A a = new A(new B(null));
a.b.a = a;
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我使用以下语句而不是上面的初始化:
A a = new A(new B(a));
Run Code Online (Sandbox Code Playgroud)
我得到了以下错误,这很明显:
Main.java:19: error: variable a might not have been initialised
A a = new A(new B(a));
Run Code Online (Sandbox Code Playgroud)
但是如果我在JShell上尝试相同,它就可以正常工作(只是为了确保variable a从未初始化过,我variable a在执行语句之前检查过,确认它之前没有初始化:
可能是我在这里遗漏了一些东西,但有些人可以帮助我理解为什么在JAVA中执行同一语句有两种不同的行为.
理解这个问题的一个简单方法是允许使用以下语句,Jshell但不允许在正常程序中:
var somevar = somevar;
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用XMLReaderFactory,但这已被弃用.现在,我如何创建XMLReader的实例?
升级到Java 10(从8开始)后,我遇到了这些错误:
InaccessibleObjectException: Unable to make field private javafx.scene.control.Button tech.flexpoint.dashman.controllers.configurator.RegistrationController.registerButton accessible: module tech.flexpoint.dashman does not "opens tech.flexpoint.dashman.controllers.configurator" to module javafx.fxml
Run Code Online (Sandbox Code Playgroud)
这是否意味着我应该将它们公之于众?这是否使得@FXML注释在Java 9和10中基本无用?
升级到Java 10(从8开始)之后,我设法让一切正常,但是在添加之后module-info.java,我遇到的一个问题是,当从命令行运行测试时,Flyway无法访问SQL文件.
启动应用程序或从IntelliJ工作运行测试(但我发现在IntelliJ上运行时,许多与模块相关的问题并不明显).使用mvn spring-boot:run它启动应用程序时工作正常,但mvn package在测试任务中构建应用程序失败并出现此错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Unable to obtain input
stream for resource: db/migration/V0002__enable_uuid.sql
at spring.beans@5.0.7.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1708) ~[spring-beans-5.0.7.RELEASE.jar:na]
at spring.beans@5.0.7.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:581) ~[spring-beans-5.0.7.RELEASE.jar:na]
at spring.beans@5.0.7.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:na]
at spring.beans@5.0.7.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:na]
at spring.beans@5.0.7.RELEASE/org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:na]
at spring.beans@5.0.7.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:na]
at spring.beans@5.0.7.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:na]
at spring.beans@5.0.7.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304) ~[spring-beans-5.0.7.RELEASE.jar:na]
at spring.beans@5.0.7.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:na]
at spring.context@5.0.7.RELEASE/org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.7.RELEASE.jar:na]
at spring.context@5.0.7.RELEASE/org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.7.RELEASE.jar:na]
at spring.context@5.0.7.RELEASE/org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:na]
at spring.boot@2.0.3.RELEASE/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.0.3.RELEASE.jar:na] …Run Code Online (Sandbox Code Playgroud) 使用 JDK 10 构建我的 OSGi 包时,maven-bundle-plugin (3.5.1) 生成以下清单属性:
Require-Capability: osgi.ee;filter:="(osgi.ee=UNKNOWN)"
Run Code Online (Sandbox Code Playgroud)
这在运行时失败:
Unresolved requirements: [[com.mycompany.mybundle [1](R 1.0)] osgi.ee; (osgi.ee=UNKNOWN)]
Run Code Online (Sandbox Code Playgroud)
使用 JDK 9 构建时,我得到了预期:
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=9.0))"
Run Code Online (Sandbox Code Playgroud)
我将 maven-compiler-plugin 的源和目标版本分别设置为“1.9”和“10”。
目前,我正在使用一种解决方法来阻止插件添加osgi.ee到清单:
<configuration>
<instructions>
<_noee>true</_noee>
</instructions>
</configuration>
Run Code Online (Sandbox Code Playgroud)
.. 但我宁愿有正确的 Java 10 要求。
继续我之前在链接中的查询:Java 10上的Swing问题,我发现了一些问题(仅突出显示错误),这次我发现在转移到Java 10之后问题主要出现在Collection API中.
以下是错误.想知道Java 10从Java 8迁移时是否有任何重大变化(从Collection/Generics的角度来看).
[javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\tree\WMTreeNode.java:159: error: breadthFirstEnumeration() in WMTreeNode cannot override breadthFirstEnumeration() in DefaultMutableTreeNode
[javac] public Enumeration<?extends WMTreeNode> breadthFirstEnumeration() {
[javac] ^
[javac] return type Enumeration<? extends WMTreeNode> is not compatible with Enumeration<TreeNode>
[javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\tree\WMTreeNode.java:158: error: method does not override or implement a method from a supertype
[javac] @Override
[javac] ^
[javac] C:\WorkSpace\JAVA10\oswm\rel20.10_Patches\WorkManager\src\com\osm\ui\tree\WMTreeNode.java:164: error: depthFirstEnumeration() in WMTreeNode cannot override depthFirstEnumeration() in DefaultMutableTreeNode
[javac] public Enumeration<?extends WMTreeNode> depthFirstEnumeration() {
[javac] ^
[javac] return type Enumeration<? …Run Code Online (Sandbox Code Playgroud) 在我们的java语言中引入模块系统后,我无法理解.java9及以上版本是否仍然是平台独立的?我问这个问题,因为我已经读过,现在每个应用程序都会有自己的jre.那么,这个单一的jre将如何在所有操作系统上运行,如Windows,Linux或Mac OS.