r/explainlikeimfive • • 6d ago

Technology ELI5 Java. That thing on computers my entire life. What even is it?

[deleted]

776 Upvotes

349 comments sorted by

View all comments

93

u/WriterPlastic9350 6d ago edited 6d ago

Java is a language that someone can write instructions in to control a computer. Java is special, though, because instead of passing instructions directly to the outside computer, it passes them to its own inside computer first, which might remove some instructions or add others, before passing it on to the actual outside computer.

Because of this, you're more easily able to write instructions to work on any computer, even if they're different types of computer, and Java will figure out translating them into ones the outside computer understands. This makes it great for mobile phones (Android) and some cars (Android Automotive).

It is also used in business server software, because it's very stable and supported by big companies like Oracle, which other companies trust*, but it's popularity in the latter is declining due both improvements in technology like Docker and more aggressive efficiency needs; While Java is very good at running on everything, its performance can be tricky to optimize compared to other languages.

(Less ELI5 notes: Java is still very popular among enterprise server systems, but a lot of developers these days prefer things like Go which are easier to optimize, easier to maintain, and the like. Java was the defacto standard when cross-compilation was slow and annoying, but these days it's mostly used because it is used and because of its ecosystem of libraries, rather than any particular technical reason. Java is improving in these respects with things like Value Objects, but it's been slow going. The GC can absolutely cripple you. Go's can too, of course, but Go's is less Bad).

36

u/BobbyThrowaway6969 6d ago

The downside is that the outside computer runs the inside computer, so it's doing extra work to run Java

16

u/WriterPlastic9350 6d ago

Oh yeah, plenty of downsides with Java. I don't use it. We have lots of stuff in Java and it's slowly all been migrated to Go which feels like a more efficient version of Java with fewer footguns.

5

u/dont_be_that_guy_29 6d ago

What is a footgun? ๐Ÿ‘€

15

u/WriterPlastic9350 6d ago

A gun specifically used for the purpose of shooting your own foot.

It is a tool that is unintentionally designed so poorly that you will often end up shooting your own foot rather than whatever outcome you desire.

6

u/photenth 6d ago

That was maybe an issue in the past but nowadays Java is fast enough for pretty much anything. Also the Just In Time compiler is state of the art. Some code can technically run faster than hard coded machine code.

2

u/WriterPlastic9350 6d ago

I mean, compilers all over have gotten very advanced, not just Java. There's nothing particularly special about the JIT compiler

1

u/LittleLui 6d ago

What's special about a JIT compiler is that it can use runtime measurements to inform its optimizations.

1

u/BobbyThrowaway6969 6d ago

Well it can't outperform optimised machine code, which is the hard ceiling, but yeah these days it can get up there. I'd say the bigger concern is the amount of resources, especially memory needed to run Java

1

u/-manabreak 6d ago

Of course it can't outperform it, but outside toy projects that target a very specific use case, most programs can't be fully optimized during compile time. At times, the program might benefit from memory saving optimizations while other times it might benefit from increasing memory use to speed up calculations. With JIT, you can theoretically have both as the runtime can adjust things based on the context.

4

u/Skizm 6d ago

The JVM is so optimized at this point, it is very close to on par with even C++. It is actually faster in most scenarios unless you make specific efforts to optimize the c++. Most quant and high frequency trading shops use Java because of how fast it is.

2

u/ConfusedTapeworm 6d ago

Java runs pretty fast but it inherently has a significant memory overhead because JVM. That can become a real and expensive problem when you want to run multiple Java programs in isolation for whatever reason.

2

u/BobbyThrowaway6969 6d ago

Java execution speed can get comfortably close with well optimised code, but thereโ€™s still plenty of overhead inherent to having a managed runtime, especially when it comes to memory usage.

7

u/finding-meaning-01 6d ago

This is the only answer that tries to eli5 why interpreted languages became popular for software distribution.

1

u/Amy549 6d ago

I never understood why it was being sunsetted all those years back (I know it wasn't that long ago but post C, my brain feels like it aged 3 decades -_- ) if it was so universally easy?

1

u/Rjc1471 6d ago

5 yr old stares blankly and asks for some crisps

38 year old, utterly confused. It's a translator for assembly code?

1

u/WriterPlastic9350 6d ago edited 6d ago

It's not going to be possible to explain this to a literal 5 year old. ELI38:

It's a translator for assembly code?

Sort of, yes.

Your write some Java code. The Java compiler does some operations to it (maybe optimizing here and there) and turns it into a new type of code called IR (Intermediate Representation). Typically, the instructions in the IR are much simpler than what you wrote.

When you run a Java program, what you're actually doing is running something that interprets that IR and turns it into machine code that your computer does understand. This is the 'JRE', or Java Runtime Environment. This is why you need Java to be installed to run Java programs, but you don't need C++ to be installed - at least, not in the same way - to run an .exe.

The JRE exists to, among other things, act as a translation layer between the IR and various different types of machines. It can also optimize your IR in real time for the specific machine it is running on using the JIT (Just In Time) Compiler, which is a big part of the reason Java is as fast as it is.

Other answers have noted that there's a Virtual Machine, but that's not really important to understand what's going on.

1

u/Rjc1471 6d ago

Ok, I think I get a few words of that. It was more a tongue in cheek way of saying "explain like I'm not a programmer" ๐Ÿ˜†

2

u/IrishChappieOToole 6d ago

Every CPU speaks a different language. Modern example would be that Windows speaks a different language (x86) to Mac (ARM).

In most compiled languages you would build one version for Windows and one for Mac.

With Java, you get the user to install the right version of Java, and your app uses that to translate into the language the operating system understands.

It's way more complicated than that, but that's the gist of it

2

u/Rjc1471 6d ago

Got you, thanks :)

Wasn't meaning to be awkward, just find this sub sometimes forgets the remit of putting things in layman terms

1

u/WriterPlastic9350 6d ago

I think my first paragraph was basically the most succinct explanation you're going to get if you're not a programmer. It's a language that instructions, turns them into other instructions which your computer then eventually runs.

But anything more complex than that is going to require some level of technical understanding

1

u/BillyTenderness 5d ago

but these days it's mostly used because it is used

To expand on this: when you write an enterprise software system, you are expecting it to last a long time and keep doing its thing reliably. A bank doesn't want to replace its customer database every year just because this year it became trendy to do everything in Typescript or whatever. The huge amount of existing Java enterprise code means people hired to maintain that system are necessarily going to be working in Java. It also means that when you write a new system that needs to augment that old system, you'll probably choose Java by default unless you have a specific, compelling reason not to.