I program in Java pretty much every day or at least every week day. Yet I could think of very few peculiarities.

#1

One peculiarity that did spring to mind is that you can create an executable Java class without creating a main method. By defining a static block within a class the static block will be executed in the same way as the Main method but cannot accept parameters. For example:

public class HelloWorld {
    static {
        System.out.println(Hello World);
    }
}

is equivalent to:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println(Hello World);
    }
}

In the second it is obviously possible to pass arguments to the main method however they would not be used anyway. There is one thing to note though, as of Java 7 this is no longer the case.

Not wanting to seem partisan I then went on the Internet to look for some other examples of peculiar behaviour in Java and found some interesting ones.

#2

You can put URLs in your code when ever and where ever you want and it will compile for example:

public class DZone {
    public static void main (String[]   args) { 
        http://dzone.com
        System.out.println(Hello DZone);   
    }
}

The reason being that anything ending in a colon is considered a label and anything beginning with two forward slashes is considered a comment. label://Comment

#3

We all know the difference between == and equals. Well I’ll give you a refresher, == acts on the reference to the object or does it? Another interesting peculiarity I found while looking was this:

Integer a = 123;
Integer b = 123;
if (a == b) {
  System.out.println(So do these have the same reference?);
}

Well not exactly, it appears that the JVM caches integer values between -128 and 128. So == equals equals in this case.

#4

Now as you would expect it is possible to handle unicode in Java but it is also possible to include unicode in your program as well for naming variables. This can have some strange effects if you use certain characters, could be an idea for obfuscation ;-)

public class HelloWorld {
  public static void main(String[] args) {
        String hello = "Hello World!";
        System.out.println(hello);
  }
}

The character to blame for this is \202E and its called the “Right to Left Override” character.

Oh and if you intend to give this a try don’t forget to use the [shell]-encoding UTF8[/shell] javac option.

#5

Random isn’t quite so random after all.

I wouldn’t have expected it to be, to be honest, because computers generate pseudo random numbers. You can read more about this here CAN A COMPUTER GENERATE A TRULY RANDOM NUMBER? However this next example really is incredible.

Random random = new Random(-6732303926L);
for(int i=0;i<10;i++)
    System.out.println(random.nextInt(10)+" ");
}

So why is this example so incredible you ask. Well because it counts from zero to nine.

#6

The last example is when is a null not a null.

public class LuckyNumber {

    static int luckyNumber = 21;

    public static LuckyNumber getInstance() {
        return null;
    }

    public static void main(String[] args) {
        System.out.println(LuckyNumber.getInstance().luckyNumber);
        System.out.println(LuckyNumber.getInstance().getInstance());
    }
}

We can see the LuckyNumber.getInstance() method returns an object of type LuckyNumber but is returning a value of null. So we would expect that accessing a member variable even if static would throw a NullPointerException. It does not, you’ve probably done something else that works in exactly the same way without even realizing it. When you test for instanceof and the value is null. It is still a class instance but with its value set to the special value null. So static methods that could be called without an instance can still be called upon it.

Well that rounds off this edition of “Programming Language Peculiarities”, until the next time.