Java Thread: an overriding example code

class A implements Runnable { public void run() { System.out.println(Thread.currentThread().getName()); } }   class B implements Runnable {   public void run() { new A().run(); new Thread(new A(), "name_thread2").run(); new Thread(new A(), "name_thread3").start(); } }   public class Main {   public static void main(String[] args) { new Thread(new B(), "name_thread1").start(); } }class A implements … Read more

Overriding vs. Overloading in Java

Overriding and Overloading are two very important concepts in Java. They are confusing for Java novice programmers. This post illustrates their differences by using two simple examples. 1. Definitions Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same … Read more

Diagram of Exception Hierarchy

In Java, exception can be checked or unchecked. They both fit into a class hierarchy. The following diagram shows Java Exception classes hierarchy. Red colored are checked exceptions. Any checked exceptions that may be thrown in a method must either be caught or declared in the method’s throws clause. Checked exceptions must be caught at … Read more

Java Thread: notify() and wait() examples

This article contains two code examples to demonstrate Java concurrency. They stand for very typical usage. By understanding them, you will have a better understanding about notify() and wait(). 1. Some background knowledge synchronized keyword is used for exclusive accessing. To make a method synchronized, simply add the synchronized keyword to its declaration. Then no … Read more

4 types of Java inner classes

There are 4 different types of inner classes in Java. This post illustrates them by using 4 simple examples. 1. Static Nested Classes class Outer { static class Inner { void go() { System.out.println("Inner class reference is: " + this); } } }   public class Test { public static void main(String[] args) { Outer.Inner … Read more

Java: convert a file to a byte array, then convert byte array to a file.

In this post, I will show you how to convert a file to a byte array and then convert a byte array to a file. To convert a file to byte array, ByteArrayOutputStream class is used. This class implements an output stream in which the data is written into a byte array. The buffer automatically … Read more

Java: convert image to byte array, convert byte array to image

This post shows two different ways to convert an image to a byte array and convert a byte array to an image. First of all, the byte type in Java is an 8-bit signed two’s complement integer. Its range is [-128, 127]. A byte array is just an array of bytes. An image is essentially … Read more

An example problem of Generic types

The following code has a compilation errors. It is confusing because you think somewhere to find the problem. import java.util.*; abstract class Animal{ public abstract void checkup(); } class Dog extends Animal{ public void checkup(){ System.out.println("Dog checkup"); } } class Cat extends Animal{ public void checkup(){ System.out.println("Cat checkup"); } } class Bird extends Animal{ public … Read more

Java PriorityQueue Class Example

In Java, the PriorityQueue class is implemented as a priority heap. Heap is an important data structure in computer science. For a quick overview of heap, here is a very good tutorial. 1. Simple Example The following examples shows the basic operations of PriorityQueue such as offer(), peek(), poll(), and size(). import java.util.Comparator; import java.util.PriorityQueue; … Read more

The Interface and Class Hierarchy Diagram of Java Collections

1. Collection vs Collections First of all, “Collection” and “Collections” are two different concepts. As you will see from the hierarchy diagram below, “Collection” is a root interface in the Collection hierarchy but “Collections” is a class which provide static methods to manipulate on some Collection types. 2. Class hierarchy of Collection The following diagram … Read more

Simple practice with Scanner in Java

What is the result of the following program? import java.util.Scanner; public class Looking { public static void main(String[] args){ String input = "1 2 a 3 45 6"; Scanner sc = new Scanner(input); int x = 0; do{ x = sc.nextInt(); System.out.print(x + " "); }while(x != 0); } }import java.util.Scanner; public class Looking { … Read more

Diagram to show Java String’s Immutability

Here are a set of diagrams to illustrate Java String’s immutability. 1. Declare a string The following code initializes a string s. String s = "abcd";String s = "abcd"; The variable s stores the reference of a string object as shown below. The arrow can be interpreted as “store reference of”. 2. Assign one string … Read more