java-10

Java 9 migration issue - package com.mymodule is declared in unnamed module , module 'newmodule' does not read it

限于喜欢 提交于 2019-12-05 15:17:06
I have created a multimodule project with the following structure myproject |- mymodule |- src |- main |- java |- com |- mymodule |- Util.java |-newmodule |-src |-main |-java |-com |-newmodule |- Main.java |-module-info.java Now i want to use Util.java which is a non modularized code in a modularized module newmodule. i have declared following in newmodule module newmodule { requires mymodule; } Project is compiling fine, but Intellij is showing module not found and package com.mymodule is declared in unnamed module , module 'newmodule' does not read it. How to resolve this issue? And one more

How to filter the age while grouping in map with list

别等时光非礼了梦想. 提交于 2019-12-05 08:01:24
Similar to my previous question here , the User objects I have are these new User("ayush","admin",23) new User("ashish","guest",19) new User("ashish","admin",20) new User("garima","guest",29) new User("garima","super",45) new User("garima","guest",19) Now I am trying to get the name to varying ages trend for these users. But I need to filter them above a threshold age. I could get the trend using Map<String, List<Integer>> userNameAndAgeTrend = users.stream().collect(Collectors.groupingBy(user-> user.getName(), Collectors.mapping(u-> u.getAge(), toList()))); this gives me {ashish=[19, 20],

Local Type Inference vs Instance

穿精又带淫゛_ 提交于 2019-12-05 05:06:21
I've tried to scan JEP-286 about local type inference. I see that this works only for local variables - understood. So this does work indeed: public class TestClass { public static void main(String [] args){ var list = new ArrayList<>(); list.add("1"); System.out.println(list.get(0)); // 1 } } I do see that this on the other hand does not compile: public class TestClass { public var list = new ArrayList<>(); public static void main(String [] args){ } } It's obvious that it does not, since the JEP says so. Now my question: It makes perfect sense for a public/protected member declared as var to

Why am I getting an AssertionError when assigning Arrays.asList() to var directly?

帅比萌擦擦* 提交于 2019-12-04 15:13:15
问题 I'm trying to understand local variable type inference in Java 10. The code below works perfectly during compilation and runtime: List list1 = Arrays.asList(1L, 2.0F, "3"); var list2 = list1; However, this line throws a compilation error: var list3 = Arrays.asList(1L, 2.0F, "3"); Error:java: java.lang.AssertionError: Unexpected intersection type: java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>> I don't really

Java 10 'var' and inheritance

你离开我真会死。 提交于 2019-12-04 05:50:00
Following a review of the var feature as seen here : I have encountered difficulties with setting up my Eclipse/IntelliJ IDEA IDEs with JDK 10 and am therefore asking help from Stack Overflow users who have a working Java 10 environment. Consider the following: public class A { public void someMethod() { ... } } public class B extends A{ @Override public void someMethod() { ... } } ... ... ... var myA = new A(); // Works as expected myA = new B(); // Expected to fail in compilation due to var being // syntactic sugar for declaring an A type myA = (A) (new B()); // Should work myA.someMethod();

File Last Modified Not Updating when Java Writes to Windows Server 2016

旧巷老猫 提交于 2019-12-04 04:39:13
I have a Java 10 application on Windows Server 2016 which is continually writing to a file using java.util.logging. In Windows File Explorer, the "Last modified" and "Size" columns do not update. Pressing [F5] does not update the details. DOS DIR gives the same incorrect answer. Right Click > Properties > Details gives an even different (and older) answer. Only running DOS TYPE or opening/closing (without save) in Notepad on the file, seems to cause File Explorer and DOS DIR to update. I assume the Java code is correct with respect to flush() as the same classes on Java 8 on Windows Server

How to avoid IntelliJ to reset language level?

て烟熏妆下的殇ゞ 提交于 2019-12-04 03:31:47
问题 I am having troubles with this which have been answered dusin of times on this site. My take on the problem is a bit different. I have a project which builds fine using maven 3.5.x and java release 10 (configured in the maven-compiler-plugin) When looking in IntelliJ's project structure, all modules have language level 10 (but the projects settings is language level 6 and the chosen sdk in the project structure is 1.8... However, even if I change the sdk to java 10 and the projects language

Different behaviour of same statement while executing on JShell

浪尽此生 提交于 2019-12-03 21:06:17
问题 I was working on a problem to store reference of two classes within each other For Example: 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; } Now if instead of above initialisation, if I use below statement: A a = new A(new B(a)); I got below error which is quite obvious: Main.java:19: error: variable a might not have been initialised A a = new A(new B(a)); But if I try the same on JShell, it

Can a local variable with an inferred type be reassigned to a different type?

好久不见. 提交于 2019-12-03 21:05:50
问题 I remember reading somewhere that local variables with inferred types can be reassigned with values of the same type, which would make sense. var x = 5; x = 1; // Should compile, no? However, I'm curious what would happen if you were to reassign x to an object of a different type. Would something like this still compile? var x = 5; x = new Scanner(System.in); // What happens? I'm currently not able to install an early release of JDK 10, and did not want to wait until tomorrow to find out. 回答1

Does Java 10 provide a val keyword? If not, why not?

*爱你&永不变心* 提交于 2019-12-03 09:32:50
Java 10 brings a C#-like var keyword for local type-inference . But does Java 10 also provide a val keyword, as is found in Scala ? val would work like var but the binding would be final . var x = "Hello, world. "; x = "abc"; // allowed val y = "Hello, world. "; y = "abc"; // forbidden If it does not, is there a reason that this is the case? Eran There is no val in Java 10, as stated in JEP 286: Local-Variable Type Inference : Syntax Choices There was a diversity of opinions on syntax. The two main degrees of freedom here are what keywords to use (var, auto, etc), and whether to have a