Things that I still discover about Java
Once in a while I find those little tricky things in the core Java that I didn’t know before. It makes me feel funny because I always thought I knew everything
Surely almost everything about core Java. All developers come to this point I guess, when they stop discovering new features and start discovering more of new strategies, frameworks , methodologies etc. But still sometimes I stumble upon one of such little features in core Java! Well I think partly this is because I never really used any books or official language specs to get acquainted with the platform, and partly because most of these features are pretty useless. On this page will try to accumulate them as time goes.
So, here is my list:
-
There are instance inistialisers in Java
Discovered May 2006
In java, a class can have an instance initialiser, which is executed before constructor is called. I knew about static initialisers that are called when the class is loaded, but never heard about instance initialisers. A colleague taught me that
class Foo { private int i; {i = 5;} //instance initialiser }It is sometimes useful in anonymous classes as they cannot have a constructor.
-
& operator is applicable to booleans
Discovered February 2006
& operator appears to be perfectly applicable to boolean operands and works exactly the same way as &&://always true: assertTrue(boolean1 & boolean2 == boolean1 && boolean2);Update: well, not exactly the same, it actually evaluates both sides first and then evaluates the boolean expression.
-
Object.wait(long timeout) is more or less precise
Discovered October 2005
I found that Object.wait(200) can suspend the current thread for… 180ms. Surprising and unexpected, isn’t it? All the guys at work always assumed that it should be at least the specified amount of milliseconds. Object’s javadoc is simple about it:
«…the specified amount of real time has elapsed, more or less.»
-
Classes can be declared inline
Discovered about 13th October 2005
Simply put, classes can be declared inside a method. Not the anonymous ones, but the named ones:public void run() { class InlineClass { public String yahoo() { return "Yahoo"; } } System.out.println(new InlineClass().yahoo()); } -
Access to public non-static inner classes
Discovered about 13th October 2005
I always thought that only static inner classes were accessible from outside the outer class. It appeared that instance (non-static) classes can also be instantiated and accessed from outside outer class as long as they are public (naturally isn’t it?):class Outer { public class Inner {} } public class Test2 { public void testIt() { System.out.println(new Outer().new Inner()); } }This
new Outer().new Inner()syntax still looks unusual to me.
By the way, the fact that members of outer class that have same name in the inner class can be accessed from inner class usingOuter.this.memberNamesyntax also was «discovered» about half a year ago:class Outer { int m = 1; public class Inner { int m = 2; public void runme() { System.out.println(Outer.this.m); } } } -
void.class
Discovered 22nd October 2005
Expressionvoid.classis legitimate and results in ajava.lang.Classinstance representingvoidtype. I did know that each primitive type had its own Class referenced asbyte.classorchar.class, but had no clue that you could do same thing withvoid. -
final static fields can be initialised in static initialiser
Discovered 30th July 2007
Well the title says it all — just as final instance fields can be initialised in the constructor (not only inline), static final fields can be initialised in the static intialiser, a «static class constructor». I really like this language feature as it adds compiler checks on potential encapsulation violations:private final static File someFile; static { someFile = ...; }
#1 by Стас on Февраль 10, 2006 - 06:27
Почитай JLS, там много интересного.
Отличие & и && только в том, что выражение с двойными знаками прекращает вычисляться, если правая оставшаяся часть уже не может повлиять на результат вычислений, а в выражении с одинарными честно вычисляется каждый операнд.
#2 by Marian on Февраль 23, 2009 - 13:25
Regarding the ‘& operator is applicable to booleans’ entry; while your assert is correct, it may be confusing to say that short circuit AND and normal AND operators work in the same way.
http://www.meshplex.org/wiki/Java/Operators#.26___Logical_AND
#3 by Andrew on Март 19, 2009 - 22:49
Are you suggesting that you haven’t learned anything since joining Barclays Capital ?
#4 by Alexey on Март 19, 2009 - 23:30
Mmmm. I’ve learned some French… and how to put babies to sleep. Nothing else I’m afraid!