
Java language oddities - osopanda
https://www.javaworld.com/article/3252845/java-language/java-language-oddities.html
======
dvh
Some time ago my Java app stopped showing notifications. I looked at the code
and discovered that I accidentally posted URL before the line that shows
notifications:

[http://example.com/](http://example.com/) showNotification();

It compiled without problem because http: is label and // commented out the
rest of the line:)

~~~
_pmf_
If had a similar issue (in C) that took me at least an hour to find:

    
    
       switch (whatever)
       {
       E_START:
           // code
           break;
       E_PAUSE:
           // code
           break;
       E_END:
          // code
          break;
       }
    

i.e. accidentally using labels instead of "case E_START:". GCC at least had
the decency of generating a warning about unused labels, while I suppose
`javac` does no such thing.

~~~
astura
Just tested in my Eclipse project

This mistake (unsurprisingly) isn't possible in Java:

"Syntax error on token "{", case expected after this token"

With an unused label I get the warning "The label E_START is never explicitly
referenced"

A good software process prevents these types of errors. You shouldn't have any
warning in your project at all, and if you see one you'll know to check it
out. You should use @SuppressWarnings for any warnings you have reviewed and
have determined are not errors. Your IDE should mark warnings clearly at the
project level, the file, and the offending line. They also should have an
overall list of warnings (and errors) and you should go through them before
building a release.

~~~
kwhitefoot
I rather like the JPL rules which say that code MUST compile with NO warnings
even if the warning is wrong. See rule 2 in [https://lars-
lab.jpl.nasa.gov/JPL_Coding_Standard_C.pdf](https://lars-
lab.jpl.nasa.gov/JPL_Coding_Standard_C.pdf).

------
mav3r1ck
I’m confused by “listing 5” that states private methods and variables are
accessible without reflection. The example it gives shows a static main within
the same class that declared private.

That’s the definition of private, that only the declaring class can access. I
believe the main beef is that one object _can access_ another object, even
though they are the same type. That makes perfect sense to me. How else would
equals and hashcode work?

~~~
_pmf_
It's commonly called "inheritance anomaly".

~~~
toolslive
"Inheritance anomaly" is something else. It refers to the combination of
inheritance and concurrency not working out well together.

[https://dl.acm.org/citation.cfm?id=968159](https://dl.acm.org/citation.cfm?id=968159)

------
Promarged
Bytes being signed is the ultimate Java oddity. Can't say how many times I've
been bitten by this.

~~~
pjmlp
I also don't like it.

Legend goes that Gosling went around Sun offices and almost everyone failed to
get unsigned versus signed math right, so they went with signed only types.

[http://www.gotw.ca/publications/c_family_interview.htm](http://www.gotw.ca/publications/c_family_interview.htm)

> Quiz any C developer about unsigned, and pretty soon you discover that
> almost no C developers actually understand what goes on with unsigned, what
> unsigned arithmetic is. Things like that made C complex. The language part
> of Java is, I think, pretty simple. The libraries you have to look up.

At least Java 8 introduced unsigned math helper methods.

~~~
kodablah
> so they went with signed only types

Except chars

> At least Java 8 introduced unsigned math helper methods.

They still aren't enough IMO. They don't have way ways to take LE/BE bytes out
of integers or vice versa (not worth creating ByteBuffer for). I always end up
recreating these utilities in projects where it isn't worth depending on or
shading an entire Guava, e.g. [0]

0 - [https://github.com/cretz/javan-warty-
pig/blob/master/fuzz/sr...](https://github.com/cretz/javan-warty-
pig/blob/master/fuzz/src/main/java/jwp/fuzz/Util.java)

~~~
pjmlp
Yep, lack of unsigned primitive types belongs to my list of Java pet peeves.

------
cel1ne
The way private visibility is handled is actually pretty useful for data-
structures like Linked-Lists or trees where one creates more of itself and
needs to adjust internal pointers without providing an external interface for
anyone else to do so.

------
noxToken
Java Puzzlers[0] has a few entries similar to this. Some of the entries are
things that would trip you up (but logically makes sense) rather than
oddities, but it's an interesting read. Note that the book is from 2005 and
uses Java 5.

[0]: [https://www.amazon.com/Java-Puzzlers-Traps-Pitfalls-
Corner/d...](https://www.amazon.com/Java-Puzzlers-Traps-Pitfalls-
Corner/dp/032133678X)

~~~
alangpierce
You can also search YouTube for "Java Puzzlers" for a few talks by Josh Bloch
(the author) where he goes through more puzzlers.

------
nayuki
Many more Java language oddities are pointed out in the book "Java Puzzlers".
One of its authors is Joshua Bloch, a major contributor to the Java language
and libraries. [http://www.javapuzzlers.com/](http://www.javapuzzlers.com/)

------
toolslive
as far as oddities are concerned, what about java.lang.NullPointerException
for a language that is free of pointers ?

~~~
mikejb
Thinking about this:

For 'Object x', x is nothing but a (typed) pointer.

Pointer manipulation is heavily limited, but technically, Java has pointers.

~~~
toolslive
it's a reference, not a pointer (fe, you cannot get its value/address), so it
should have been NullReferenceException or NullDereferenceException (or
something similar)

~~~
pjmlp
You can get its address via the unsafe package, and Panama will provide an
alternative API to do it.

