r/programming • • Sep 15 '20

Java 15 released!

https://jdk.java.net/15/
214 Upvotes

130 comments sorted by

View all comments

215

u/Ravoos Sep 15 '20 edited Sep 15 '20

Java devs: Cool.....I'm still using Java 8 tough.

Edit: Okay, I got a lot of interesting comments here when I just made a joke. Still, it is fun to read it all and to see just bow dedicated the Java guys are to their language

42

u/Bartolomev Sep 15 '20

I'm happy that in our company we moved (still moving actually) to Java 11, newer projects at least. My colleagues are still afraid of using var though, so nothing changed really.

38

u/pron98 Sep 15 '20

so nothing changed really

Except lower footprint, higher performance and better profiling with JFR, all of which can save you real money (to make even more money, use JDK 15).

7

u/[deleted] Sep 16 '20

> Except lower footprint

This was not my experience when my company migrated java 8 -> 11. We saw a number of our services increase RSS size by around 1gb. There was oddly no accounting for the increase in memory size in the heap dumps or native memory tracking, we eventually just accepted that some services would use more memory.

9

u/pron98 Sep 16 '20 edited Sep 16 '20

Do you mean that there's more heap consumed by data or that the Java process takes up more RAM from the OS? If it's the former, it is indeed strange and does require investigation (e.g. the footprint of most strings has been nearly halved in JDK 9, and this is the general trend of overall footprint we see). If the latter, it's probably a simple matter of different defaults. Remember that a Java process takes as much RAM as you tell it; if you don't, it will take up some heuristically-chosen default value that can change significantly from one release to another (I think that the default for G1, which is the default GC since 9, is to now take up 1/4 of total RAM unless you tell it otherwise).

→ More replies (2)

5

u/CoffeeTableEspresso Sep 15 '20

They'll get there, at least you're on the newer version

12

u/Certain_Abroad Sep 15 '20

My colleagues are still afraid of using var

Look on the bright side. Better than that having colleagues who just sprinkle var everywhere.

17

u/Programmdude Sep 15 '20

Var is good when you already know the type.

var x = 0; // Good
var y = new FooBar(); // Good
var z = ProcessRequest(); // Bad

Personally I use resharper (or intellij, webstorm) and so it just inserts the type anyway.

8

u/Ameisen Sep 16 '20

Already know the type, or don't care about the specific type. getObjects() obviously returns a container of Objects, you probably don't care what kind of container.

4

u/Programmdude Sep 16 '20

To an extent, assuming names are sensible. Personally I'd use var everywhere if it was just myself, but when working on a team I need to assure everybody can figure it out quickly.

8

u/Ameisen Sep 16 '20

My rules for using var/auto:

  • Really long or hard to read generic/template types

  • Where it is required (C++ lambdas for instance)

  • Where the type is obvious due to either the assigned value or the function name

  • Where the explicit type doesn't matter due to usage, but the "kind" of type is obvious (a container, for instance).

  • Where the type really, really doesn't matter, like a guaranteed intermediate between two closely-tied routines.

I get really annoyed seeing things like List<FooBar> fooBars = getFooBars();.

I tend to be fond of type inference as it also makes the code more resilient during refactoring and propogates exact types.

→ More replies (2)

7

u/eeperson Sep 16 '20

Why is the last one bad? The type should just be a mouse over away.

9

u/Atulin Sep 16 '20

"I burned my mouse years ago on a stake and exclusively use heavily modified VI running on a Raspberry clone I built with silicone dug up in my backyard"

→ More replies (2)

2

u/irishsultan Sep 16 '20

You can't copy/paste text in a mouse over, a mouse over hides other parts of your screen, it takes more time to get your mouse in position than it would take to read it, I could go on.

2

u/fuckin_ziggurats Sep 16 '20

The problem with this is code is difficult to read/skim through. When you're working with someone else's code it's very important to be able to skim through code quickly to get to the part that you need. Honestly skimming is half of the work in maintenance projects and bad use of var can make it very difficult.

2

u/metaltyphoon Sep 16 '20

You can't mouse over on a merge / pull request on GitLab / GitHub

→ More replies (3)

1

u/flatfinger Sep 16 '20

If the variable will get written to (receive a reference to a different object) later in the code, its type may need to be a supertype of the function's return type. For example, if a variable will sometimes need to hold references to different kinds of List (Java)/IList (.NET) implementations, but is initialized to hold a reference to e.g. an ArrayList(Java)/List(.NET), declaring the variable as the class type rather than the interface type may cause problems later on.

→ More replies (2)

4

u/vytah Sep 16 '20

My colleagues are still afraid of using var though, so nothing changed really.

I remember when C# got var, some programmers were against it as they didn't want any dynamic typing in their language.

17

u/AndreasTPC Sep 16 '20

They are idiots if that's their reasoning, because type inference is not dynamic typing by any means.

With dynamic typing the type is determined at runtime and can change during execution. In a statically typed language with type inference the type is still determined at compile time and can't change during execution, all the guarantees from the type system are still there. The only thing that changes is how readable the code is.

0

u/[deleted] Sep 15 '20

[deleted]

17

u/aoeudhtns Sep 15 '20

There were some fairly fundamental compat changes introduced in 9. Once you're on Java 9 it's more guaranteed that a jump will work. That being said, for 8 → 9, you're likely only going to encounter problems on a large project with a sprawling dependency tree.

4

u/j633 Sep 16 '20

I'm not a fan of var in java. Only if you have some super verbose type with generics. Otherwise it doesn't hurt to have a few characters more.

11

u/Ameisen Sep 16 '20

Same justification as auto in C++. It makes your code more resilient to change and also helps reduce needless redundancy. getPlayerObjects is pretty self-explanatory, there is no reason to explicitly assign a call result to a List<PlayerObject> or a PlayerObject[]. You know it's a container of PlayerObjects, specifying the result variable type just adds redundant noise. var also resolves to the exact return type rather than a more abstract type that people would often use otherwise.

2

u/bezik7124 Sep 16 '20 edited Sep 18 '20

I personally use var quite often, but with few rules.

Use, when variable name is good enough to tell the full story - which should always be the case, but we arent perfect.

Don't use when ą type can't be determined by scanning the block it is in, which practically means that i avoid using it to assign the result of a method, unless it is obvious what the return type is.

Don't use when dealing with generics, eg Map, Collection, etc.

1

u/eeperson Sep 16 '20

Don't use when dealing with generics, eg Map, Collection, etc

I actually feel that this is one of the better places to use var. I find that people tend to shy away from more complex types to fully express constraints. With var you can propagate more complex types without having to write it over and over.

→ More replies (3)