top | item 17639902

Java's Magic Sauce

108 points| wheresvic1 | 7 years ago |azul.com | reply

71 comments

order
[+] sreque|7 years ago|reply
Oracle went the wrong direction with unsafe. Unsafe memory access is a feature of .NET and the CLR, not a hidden API where you get reprimanded by the community for using it. Not surprisingly, c# has succeeded where Java has failed in being cross platform, used for instance in game development with the unity engine or multi platform mobile development via xamarin. On the other hand, no one in the .NET community is complaining that their code is segfaulting all the time.

The jdk API developers should not be granted a special API because they are more trustworthy than the rest of us "average" developers. Unsafe should be made public, not removed and replaced.

[+] peterashford|7 years ago|reply
Unity "proving" C#'s success in games is about as reasonable as saying that Android games in Java proves Java's suitability for games. Its an accident of history that those languages were tied to particular projects that were very successful. Unity could have used Java for scripting (in fact, it does now for Android) and Android could have been delivered in c#. Claiming that these products could have only been delivered in their target languages is just ridiculous
[+] pjmlp|7 years ago|reply
Actually one might say Sun was the one that went the wrong way.

> "[C# is] sort of Java with reliability, productivity and security deleted."

-- Gosling on https://www.cnet.com/news/why-microsofts-c-isnt/

Oracle is improving Java native interop with projects Panama,

http://openjdk.java.net/projects/panama/

Java has surely not failed in being cross platform, not if we are comparing it with .NET.

.NET Core still doesn't run in many environments, where I can gladly put Java on. The computing world is much bigger than just macOS, Windows and GNU/Linux.

And then there is the issue that even Swing/JavaFX/SWT are better supported that whatever .NET Core UI framework one can think of.

Xamarin is nice and way better than React Native, Cordova or Qt, and yet you aren't going to find much love for it on online forums.

On game development C# got lucky with Unity, because finally there was a company that was firm to hold on to managed languages in game development no matter what, and they managed to turn out one of the best engines out there for indies.

Had Unity decided to go the way of Unreal with UnrealScript and the story would be quite different.

I still remember the JavaGamming initiative at Java ONE, Java3D or the JOGL project, but like many other Sun initiatives, they let it die after a couple of years. Maybe here the story would have been different if they were actually committed to it.

Thing is UNIX companies never understood desktop and gaming cultures, so here .NET has notoriously an advantage by having been part of a desktop OS SDK since its inception, from a company that actually understands desktop and gaming cultures.

[+] lawlessone|7 years ago|reply
>surprisingly, c# has succeeded where Java has failed in being cross platform,

lol what are you talking about, i can use Java on everything.

[+] bitmapbrother|7 years ago|reply
>Not surprisingly, c# has succeeded where Java has failed in being cross platform, used for instance in game development with the unity engine

C# succeeded in game development? That's its claim to fame? Being an optional scripting language used by Unity? The real work is done by the graphics engine that's written in C++.

>or multi platform mobile development via xamarin

Last time I checked the percentage of apps on the App Store and Google Play that were written with Xamarin tools were so low that they barely even registered.

[+] didibus|7 years ago|reply
Doesn't unity just steal the C# syntax, but actually compiles it on its own? Effectively, it's unrelated to the CLR?

Same for Xamarin?

[+] skocznymroczny|7 years ago|reply
Check out Project Panama. It's trying to bridge the gap between Java and C# when it comes to native interop, which is the main usecase for 'unsafe' features.
[+] kodablah|7 years ago|reply
> I’ll leave you with a question to ponder, would Java have been as successful as it has been had sun.misc.Unsafe not been hidden in the library code but still accessible?

Yes, and it would have been as successful if Unsafe was never added. The language choice rarely hinged on this, it was just used to improve performance and could have been done natively if it had to.

[+] vvanders|7 years ago|reply
Hardly, I spent a lot of time in spaces where Java and others languages would be considered. If there was ever any hint if performance being a bottleneck we chose C++ over it. Even moreso because unsafe doesnt exist on some variants(Android).

What unsafe gets you is good cache usage. JNI and other approches negate that benefit(also disables inlining and other things).

[+] _ea1k|7 years ago|reply
I tend to agree with you. Unsafe was great and useful, but Java was rarely used for the kind of things that would have been impossible without it. I think the real impact on its share was minimal, tbh.

I'm glad that it is moving into a fully supported state, though.

[+] chrisseaton|7 years ago|reply
> could have been done natively if it had to

I don't think so. Do you mean by something like JNI? The use-cases of Unsafe that I see most often would not have been tractable using JNI. They rely on intrinsification.

> it was just used to improve performance

But to the point where Java is not suitable at all without the performance, so it's not a nice-to-have for some people, it's essential.

[+] ram_rar|7 years ago|reply
I see many of non-trivial java projects using UnSafe. At this point, why not make UnSafe a feature and let programmers use memory outside the purview of Garbage Collector ?
[+] yawaramin|7 years ago|reply
Can I just ask–why not do manual memory management with just arrays? Why Unsafe?
[+] chrisseaton|7 years ago|reply
- You cannot get the native address of arrays in order to pass them to native code.

- Arrays are subject to being moved by the GC, so even if you could get the address they could move.

- Arrays are limited to around 16 GB, and that's if you're packing data into longs.

- Accessing an array involves a bounds check, which may float out of a loop, but may well not.

- Arrays cannot be allocated with memory that is anything except mapped private.

I use Unsafe to put memory into a location where I can get a stable native address in order to make native calls using it. Using an array would not allow that.

[+] guipsp|7 years ago|reply
Arrays are stored on the gc while unsafe is off the gc
[+] johannes1234321|7 years ago|reply
> There are many ways you can use these methods that will return a result that is meaningless (if you’re lucky) or causes the JVM to stop abruptly (if you’re not).

In my world I prefer a hard crash than me wingless results, which I then have a hard time reading down ...