1

Topic: Java10, var AND Impossible types

I saw many arguings of it var and it to me, , seemed pleasant , but not mandatory. It appears, behind it there is one cool feature - Impossible types Particulars here http://benjiweber.co.uk/blog/2018/03/03 … dk-10-var/ I  an example therefrom - works! $ cat Main.java public class Main {public static void main (String... args) {var person = new Object () {String name = "bob"; int age = 5;}; System.out.println (person.name + "aged" + person.age);}} $./jdk-10/bin/javac Main.java $./jdk-10/bin/java Main bob aged 5

2

Re: Java10, var AND Impossible types

There is one more interesting type: catch (Exception1 | Exception2 e). If to show imagination, this feature for certain it is possible  for something cool

3

Re: Java10, var AND Impossible types

Hello, vsb, you wrote: vsb> There is one more interesting type: catch (Exception1 | Exception2 e). If to show imagination, this feature for certain it is possible  for something cool As far as I understand, no interesting type here is present - "e" receives type of the nearest general ancestor. Accordingly, to unique fields/methods Exception1/2 you have no access (without explicit a caste)

4

Re: Java10, var AND Impossible types

Hello, bzig, you wrote: other feature From this follows, reassignment of such variables is impossible. var person = new Object () {}; person = new Object () {};//Compile ERROR the variable person cannot appropriate another Object.

5

Re: Java10, var AND Impossible types

Hello, serb, you wrote: S> Hello, bzig, you wrote: S> other feature From this follows, reassignment of such variables is impossible. S> var person = new Object () {}; S> person = new Object () {};//Compile ERROR S> the variable person cannot appropriate another Object. Well and it is correct - you implicitly created two anonymous classes and they are not appropriated each other.

6

Re: Java10, var AND Impossible types

Hello, bzig, you wrote: B> Hello, serb, you wrote: S>> Hello, bzig, you wrote: S>> other feature From this follows, reassignment of such variables is impossible. S>> var person = new Object () {}; S>> person = new Object () {};//Compile ERROR S>> the variable person cannot appropriate another Object. B> Well and it is correct - you implicitly created two anonymous classes and they are not appropriated each other. Not simply anonymous classes, and the classes expanding Object. If at the left was Object, all ok. Whether Another matter has it sense. For example, to appropriate different implementations of the same interface of any variable in a method of a class-factory for the purpose of its initialization and resetting upward.

7

Re: Java10, var AND Impossible types

B>> Well and it is correct - you implicitly created two anonymous classes and they are not appropriated each other. UDI> it is not simple anonymous classes, and the classes expanding Object. If at the left was Object, all ok. I will open to you a secret - anonymous classes on determination expand any class. Well and that two times not to rise - all classes in Java expand Object

8

Re: Java10, var AND Impossible types

Hello, bzig, you wrote: B>>> Well and it is correct - you implicitly created two anonymous classes and they are not appropriated each other. UDI>> it is not simple anonymous classes, and the classes expanding Object. If at the left was Object, all ok. B> I Will open to you a secret - anonymous classes on determination expand any class. Well and that two times not to rise - all classes in Java expand Object Ok, so. All expand, but not all copies it is copies of the classes necessary to you, i.e. that about what in my opinion serb to you said that now "" like var leads opposite to absolutely explicit definition of type, though and  for the developer. And that "reassignment" in such record is impossible.

9

Re: Java10, var AND Impossible types

UDI> And that "reassignment" in such record is impossible. Yes, therefore high-grade  still is not present. On the other hand some assignment all the same is possible. For example, if in a flow  objects of a certain "impossible" type also collected in a collection then it is possible to manipulate these objects: to interchange the position in a collection, to appropriate elements of a collection to variables and then to manipulate these variables, including . The main thing that anonymous class determination was in one place only (at the Serb was in two).

10

Re: Java10, var AND Impossible types

B> On the other hand some assignment all the same is possible. $ cat Main.java public class Main {public static void main (String... args) {var list = java.util.stream. Stream.of ("Bob", "Alice").map (s-> new Object () {String name = s; int age = 5;}).collect (java.util.stream. Collectors.toList ()); var first = list.get (0); var second = list.get (1); System.out.println ("first is" +first.name + ", second is" + second.name); var swap = first; first = second; second = swap; System.out.println ("first is" +first.name + ", second is" + second.name);}} $./jdk-10/bin/javac Main.java $./jdk-10/bin/java Main first is Bob, second is Alice first is Alice, second is Bob

11

Re: Java10, var AND Impossible types

Hello, bzig, you wrote: B>> On the other hand some assignment all the same is possible. B> B> var list = java.util.stream. Stream.of ("Bob", "Alice") B>.map (s-> new Object () {B> String name = s; B> int age = 5; B>}) B>.collect (java.util.stream. Collectors.toList ()); B> Yes so it turns out. But here as reflection (map) there is only one anonymous class AC1 (there is it at compilation more precisely) and then so much copies AC1 are used, how many it is necessary for data stream conversion. The list of copies AC1 turns out. And everywhere for var disappears AC1. In general as on me so it is one more "", interesting, and to apply it it is necessary to a place. Unfortunately, most likely will use it everywhere-everywhere and it will lead to incidents and .

12

Re: Java10, var AND Impossible types

UDI> so it is one more "" Just not . Instead of var here nothing can be delivered, that worked. Only to create the present class. I.e. this var transformed impossible type in possible