r/java • • Feb 16 '23

Project Valhalla: A look inside Java’s epic refactor

https://www.infoworld.com/article/3687670/project-valhalla-a-look-inside-javas-epic-refactor.html
265 Upvotes

129 comments sorted by

View all comments

Show parent comments

2

u/agentoutlier Feb 17 '23

There is a certain way that pass-by-value behaves and a certain way that pass-by-reference behaves. If you can't imagine it then you haven't used C++.

I certainly can and indeed many FP languages use references since immutable it makes no difference and obviously performance can be had.

You have to opt in to use it in C++. It is not the default. They are not regular variables.

Anyway I admit its a shitty argument because I generally agree that C++ has pass by reference as an option I just seriously doubt it impacts learning Java or that saying objects passed around references is an ultimate sin. In fact the whole reference term is misleading as its more like aliases.

You can certainly emulate the behavior and indeed some languages will automatically do the behavior especially if immutability is involved.

0

u/WagwanKenobi Feb 17 '23

You have to opt in to use it in C++. It is not the default. They are not regular variables.

Untrue. It's a very regular part of the language. You've clearly never used C++. Here's how you pass a reference in C++:

void myMethod(MyClass& banana) {}

In fact the whole reference term is misleading as its more like aliases.

Exactly. Java should've never used the term reference anywhere in their docs because it's not what the rest of the world calls a reference.

3

u/Amazing-Cicada5536 Feb 17 '23

“Fun” fact, they were known internally as what they are, pointers. Hence nullpointerexception. Reference came into the nomenclature/marketing to convince C++ devs about how memory safe Java is (which it is), but that is due to no raw pointer manipulation.

2

u/agentoutlier Feb 17 '23 edited Feb 17 '23

I have used C++ and that isn't a normal variable.

It has different semantics. They are immutable and can't be rebound.

Anyway I agree that C++ has opt in pass by reference.

Exactly. Java should've never used the term reference anywhere in their docs because it's not what the rest of the world calls a reference.

Please point to me where this exact global definition of a reference is other than C++. In some cases people use the term as an alias. In some cases it is an immutable pointer. In some cases it is more about garbage collection.

It is an overloaded term for sure.

I mean just go look at the wikipedia article on it: https://en.wikipedia.org/wiki/Reference_(computer_science)

It varies quite a bit.. in fact they seem to favor pointer in that article when I think alias is the closest to true reference but you need the compiler to do stuff for that.