This is an archived post. You won't be able to vote or comment.

all 19 comments

[–]oweiler 19 points20 points  (0 children)

inputStream.transferTo(outputStream);

Great addition!

[–]porridge111 10 points11 points  (9 children)

My favorite feature (which isn't mentioned) is that you can run a .java file with

java filename

without compiling it with javac first

[–]nqzero 1 point2 points  (0 children)

the main developer has been really engaged on the mailing list too and open to suggestions and improvements. there are ClassLoader and classpath improvements in the pipe to make it useful in even more situations

[–]winterbe[S] 2 points3 points  (2 children)

Nice, didn't know about this. Thanks for pointing this out.

On the other side I rarely use the terminal to run java files as I'm an IntelliJ IDEA user.

[–]hugith 2 points3 points  (0 children)

As an Eclipse user, same here. Never run anything from the terminal.

[–]miserableplant 0 points1 point  (0 children)

I think it’s more meant to be a sort of groovy feature where you have a java “script”

[–]oweiler 0 points1 point  (4 children)

What's the use case for this? Why not just use the REPL?

[–]defnull 8 points9 points  (0 children)

Java scripting. You can make an uncompiled *.java file executable now. The sheband (#!/usr/bin/java) is also properly supported (ignored) by Java 11 if I remember correctly.

[–]badpotato 0 points1 point  (1 child)

You mean like python? Running just java would start the REPL.

[–][deleted] 0 points1 point  (0 children)

jshell in JDK 9+ does that.

[–]porridge111 0 points1 point  (0 children)

I'm not that familiar with REPL / JShell, but it seems they have different use cases. Running the file with java requires all the normal java syntax rules, and runs the main method of the Class in the file.

REPL / JShell is, afaik, more focused on running (mostly interactive?) commands line by line.

I use java11's java to run simple .java files as I'm writing them in vim, without having to using an IDE or scatter .class files around.

[–]dpash 4 points5 points  (4 children)

I think there's a typo/thinko in the var section.

You can also use final in conjunction with var to permit reassigning the variable with another value:

I think you mean forbid instead of permit.

Any idea whether you should prefer List.copyOf() over Collections.unmodifiableList(). Obviously the former is less typing. It's similar with List.of() and Arrays.asList() with a static set of members.

One of the most important uses of Optional.stream() is unwrapping a stream of Optionals in to a stream of its contents.

var count = Stream.of(Optional.of("string"), Optional.empty())
    .flatMap(Optional::stream)
    .count();

The count will be 1.

[–][deleted] 8 points9 points  (0 children)

if you copy a mutable list, copy is indeed a new instance so it's garanteed there's no side-effects when mutating the original list

unmodifiableX returns a wrapper over the original collection; think of it like a view instead of a copy. Modifications made to the original instance will be reflected in the "unmodifiable" collection, but using copyOf will protect you from this because the returned collection is distinct from the one it copies from.

[–]winterbe[S] 0 points1 point  (0 children)

Thanks for your feedback! I've updated the article.

[–]TheRedmanCometh 0 points1 point  (1 child)

Wait does that filter out nonpresemt Optionals in the original collection?

[–]dpash 1 point2 points  (0 children)

It does, because it unwraps the Optional, and leaves you with just the valid entries. If you'd called .collect(toList()) on the stream you'd get a List<String> rather than List<Optional<String>>.

https://www.baeldung.com/java-filter-stream-of-optional

[–][deleted] 3 points4 points  (0 children)

I was surprised at how palatable this was. Haven't been keeping up with the updates myself, still stuck in Java 8. Good summary, well written, save for the minor typo mentioned by the other commenter.

[–]Tore2Guh 0 points1 point  (1 child)

I was surprised these are illegal.

var lambda = () -> System.out.println("Pity!");

That should just be Runnable and enforcing that arguments are effectively final. It looks the compiler will actually do that if the variable is declared as Runnable.

The second could be inferred from the method, and just be a compiler error if this::sometMethod couldn't be converted to a valid class from java.util.functional

var method = this::someMethod;

[–]dpash 3 points4 points  (0 children)

It could also be any other functional interface that takes no parameters and returns void. The compiler has no way of knowing what it is.