How Java Compiler Generate Code for Overloaded and Overridden Methods?

Here is a simple Java example showing Polymorphism: overloading and overriding. Polymorphism means that functions assume different forms at different times. In case of compile time it is called function overloading. Overloading allows related methods to be accessed by use of a common name. It is sometimes called ad hoc polymorphism, as opposed to the … Read more

What is Instance Initializer in Java?

In this post, I will illustrate what are instance variable initializer, instance initializer, static initializer, and how the instance initializer works in Java. 1. Execution Order Look at the following class, do you know which one gets executed first? public class Foo {   //instance variable initializer String s = "abc";   //constructor public Foo() … Read more

Get Current Time By Using java.util.Calendar#getInstance()

Below is a simple example for showing how to use Calendar.getInstance() to get current time. public static String getCurrentTime(){ Calendar c=Calendar.getInstance(); String hour="" + c.get(Calendar.HOUR_OF_DAY); String min="" + c.get(Calendar.MINUTE); String sec="" + c.get(Calendar.SECOND); if (hour.length() == 1) hour="0" + hour; if (min.length() == 1) min="0" + min; if (sec.length() == 1) sec="0" + sec; return … Read more

Firefox is already running – linux .parentlock problem

While using a Ubuntu linux machine, this can happen for a lot of people. This is the solution. Error Message: “Firefox is already running but is not responding …” The problem: Many packages (like firefox) create lock files in your account to prevent database corruption with multiple copies of the application. Look in your account … Read more

Topics Covered in Algorithms Class

Topics covered in Algorithms class and selective slides from Internet. Josephus Problem Heaps and 2-3 trees slides 1 slides 2 Fibonacci Heaps slides animation Amortized complexity video slides Union/Find – average and worst case slides 1 slides 2 Sorting and selection slides Minimum spanning tree algorithms slides 1 slides 2 NP-completeness – definitions, implications, reductions … Read more

Syntactic vs. Semantic vs. Runtime Errors

The following are three Java examples for showing what are syntax error, semantic error, and runtime error. Syntactic Error If a program contains syntax error, it will not pass compilation. public static int returnNull(){ System.out.println("haha"); }public static int returnNull(){ System.out.println("haha"); } Semantic Error If a program contains only semantic errors, it means that it can … Read more

Java Design Pattern: Bridge

In brief, Bridge Design Pattern is a two layer abstraction. The bridge pattern is meant to “decouple an abstraction from its implementation so that the two can vary independently”. The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes. 1. Bridge pattern story The example of TV and Remote Control(typo … Read more