Did you know? Programmers convert coffee to code.

If you like my articles, sponsor me a coffee.

Ten lines of code

I am reading The Passionate Programmer by Chad Fowler (review follows when I am done with the book) and there he mentions that a Java expert should know other things of the language he/she uses than just coding.

In this article I will look at some core points you should know if you want to consider yourself a Java expert — but again, this is mostly my own point of view.

Make the JVM fall

One interesting question was to write a simple application which makes the JVM fall. I thought about a simple and minimalistic solution to accomplish this task. I’ve ended up with the following 8 lines of code:

package minideath;

public class MiniDeath {

    public static void main(final String[] args) {
        main(args);
    }
}

If you do not want to try it yourself this application dies with a nasty StackOverflowError and the stack trace is filled with the same line over and over again:

Exception in thread "main" java.lang.StackOverflowError
  at minideath.MiniDeath.main(MiniDeath.java:6)
  at minideath.MiniDeath.main(MiniDeath.java:6)
  at minideath.MiniDeath.main(MiniDeath.java:6)
  at minideath.MiniDeath.main(MiniDeath.java:6)
  at minideath.MiniDeath.main(MiniDeath.java:6)
  at minideath.MiniDeath.main(MiniDeath.java:6)
  at minideath.MiniDeath.main(MiniDeath.java:6)
  at minideath.MiniDeath.main(MiniDeath.java:6)
  at minideath.MiniDeath.main(MiniDeath.java:6)
...

As you can see the 8 lines contain well-formatted code including a package definition which is not even required for this simple test — but this code compiles without any warning in my JBoss DevStudio.

If I let FindBugs run over it (as I suggested in an earlier article) I get a nice Scary error mentioning that this code has “an infinite recursive loop that will result in a stack overflow”. So use FindBugs.

Make the JVM stay still

Alternatively to the previous task: write an application which never terminates.

OK, this one is easy because there is no developer on this earth who never accidentally created an infinite and non-recursive loop. Again, keep it short and simple:

package minideath;

public class NeverEnding {

    public static void main(final String[] args) {
        while (true) {

        }
    }
}

The formatting makes it 10 lines long but this is the really simplest task to make the JVM run forever — or at least until you call “kill dash nine”.

However there is one very scary thing about this code: FindBugs does not find bugs. I do not have any hint on this never ending loop — even with the maximal analysis effort. And this is really sad.

Make the JVM work

After killing the JVM and making it hang let’s create a simple application which is valid and does not kill or hang the JVM. Again, the simplest solution has the most value.

package minideath;

    public class WorksNothing {
        public static void main(final String[] args) {

        }
    }

Well, this block of code does nothing but it is a valid Java application which does not have any errors hidden.

Conclusion

These were three mini-questions which could fit into Java interviews. And I can assure everyone who will face me trying to get into my team that I will ask these questions to find specialists in Java who know the idea behind what they are doing.

GHajba
 

Senior developer, consultant, author, mentor, apprentice.

I love to share my knowledge and insights what I achieve through my daily work which is not trivial — at least not for me.

Click Here to Leave a Comment Below 0 comments
%d bloggers like this: