Category «Programming»

Calculate Height of any Tree in Java (Non-Binary Tree)

Computing height of a tree in Computer Science is very common. Most of the examples and online discussions talk about computing the height of a Binary Tree. The example method I am sharing can be used to compute the height of any tree. So even if you have a non-Binary tree, you can use this method …

Easiest Way to calculate elapsed time in Java

Quite often in our day to day programming, we need to compute how long does a specific portion of the code takes to complete. For e.g; you might want to check how long does a method take to complete.  In Java, the simplest way to accomplish this is to use System.nanoTime() method. Following example shows …

How to compare Strings in Java

One of the most common bugs I’ve seen in Java programs in the use of == operator to compare two String objects. Most of the times the result may be accurate but it is not always guaranteed. We need to understand the difference between == and the Object.equals method.