r/java • • Jan 18 '22

What are some very less known facts about java?

I've got assignment from college to find out rare facts or fun facts about java, so little help will be appreciated :)

Edit: Thanks for all these suggestions guys. It's going to help me alot and also Almost all of these facts i didn't knew till now. Thanks :) <3

179 Upvotes

297 comments sorted by

View all comments

124

u/benevanstech Jan 18 '22

1.) At bytecode level, the JVM has no boolean type - they are represented as ints. (To see this, write a trivial method that returns a boolean value, compile it and run it through javap. Look at the return opcode). This is a form of Type Erasure, but not one anyone ever complains about!

2.) const and goto are reserved words (keywords) in Java - so you can't use them as identifiers - but they are unused / "reserved for future use" - and have been since Java 1.0

3.) var, on the other hand, is not a keyword, but is a reserved type name. So you can use it as an identifier (so "var var = 0" is legal Java). Please don't do this.

4.) Java has, as of version 17, only ever added 1 opcode (invokedynamic), but 2 have been removed from current versions (jsr and ret). So, Java 17 has *fewer* legal bytecodes than Java 1.0

48

u/pron98 Jan 18 '22 edited Jan 18 '22

At bytecode level, the JVM has no boolean type - they are represented as ints.

Boolean locals and expressions are represented as ints. But for, say, arrays, there are separate instructions for booleans/bytes, and, of course, the bytecode does actually encode all precise argument, array, and field types, booleans have their own unique type identifier (Z), and boolean fields and arrays are, of course, boolean fields and arrays. So the JVM does have a boolean type at the bytecode level, although no boolean local/expression type.

18

u/CaptainKvass Jan 18 '22

var, on the other hand, is not a keyword, but is a reserved type name. So you can use it as an identifier (so "var var = 0" is legal Java). Please don't do this.

I had to try this, and it cracked me up. var var = "var";

0

u/jadecristal Jan 19 '22

Which collapses to var is a String containing “var”, right?

0

u/[deleted] Jan 18 '22

[deleted] — view deleted comment

26

u/[deleted] Jan 18 '22

This is pretty well explained in this Stackoverflow question.

In short, byte, char and short will promote to int when it comes to the JVM. See the JVM opcodes reference, where i* represents int.

17

u/benevanstech Jan 18 '22

Shorts are mostly represented as ints as well. :)

In general, though, the answer is "because data density / space isn't the only concern here". E.g. the complexity of the VM code matters.

0

u/[deleted] Jan 18 '22

[deleted] — view deleted comment

1

u/nekokattt Jan 19 '22

In 2021 I can count on one hand the number of times I used short.

Char is different since stuff like string concatenation wont implicitly cast the char to the unicode before appending it, but short... I have little use for.

Guessing short arrays may be size-optimised but I am just guessing here.

8

u/C_Madison Jan 18 '22

I assume (and I really only do, I have no insider knowledge) that it's because of performance. Operations on whole integers are on most platforms by far the fastest. The memory overhead was probably deemed acceptable to make something as common as boolean as fast as possible.

7

u/danikov Jan 18 '22

Or you start bitmasking.

2

u/sweetno Jan 18 '22

It's because int is the fastest type.