Java and Computer Science Courses

A good programmer does not only know how to program a task, but also knows why it is done that way and how to do it efficiently. Indeed, we can find almost any code by using Google, knowing why it is done that way is much more difficult than knowing how to do it, especially … Read more

Comparable vs. Comparator in Java

Comparable and Comparator are two interfaces provided by Java Core API. From their names, we can tell they may be used for comparing stuff in some way. But what exactly are they and what is the difference between them? The following are two examples for answering this question. The simple examples compare two HDTV’s size. … Read more

How an IP-PDU is sent from one host to another

This shows how a host sends a IP-PDU(Protocol Data Unit) to a host in another network. Mainly about how an IP-PDU goes through Network and Link Layers. It demonstrate how switch table, ARP table, routing table and forwarding table works with each other in the whole process. Click to download the diagram. This diagram is … Read more

Monitors – The Basic Idea of Java Synchronization

If you took operating system course in college, you might remember that monitor is an important concept of synchronization in operating systems. It is also used in Java synchronization. This post uses an analogy to explain the basic idea of “monitor”. 1. What is a Monitor? A monitor can be considered as a building which … Read more

Java Code – Convert a file to a String

How to read file content into a string? The following is the Java code to do that. To make it work, the filePath need to be changed. public static String readFileToString() throws IOException { File dirs = new File("."); String filePath = dirs.getCanonicalPath() + File.separator+"src"+File.separator+"TestRead.java";   StringBuilder fileData = new StringBuilder(1000);//Constructs a string buffer with … Read more

Eclipse SWT and JFace widgets references

If you want to develop Java Desktop application, you probably have two choices of libraries: SWT vs. Swing. Where there is pros and cons for both of them, here is a comparison between them. Recall the role of Standard Widget Tootlkit (SWT) in eclipse platform below. JFace is a high-level user interface(UI) widget library built … Read more

Java Access Level for Members: public, protected, private

Java access level contains two parts: 1) access level for classes and 2) access level for members. For class access level, the keyword can be public or no explicit modifier(package-private). For member access level, the keyword can be public, protected, package-private (no explicit modifier), or private. The following table summarizes the access level of different … Read more

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