One of the most significant advantages of Java is its memory management. You simply create objects and Java Garbage Collector takes care of allocating and freeing memory. However, the situation is not as simple as that, because memory leaks frequently occur in Java applications.
Diagram
String is passed by “reference” in Java
This is a classic question of Java. Many similar questions have been asked on stackoverflow, and there are a lot of incorrect/incomplete answers. The question is simple if you don’t think too much. But it could be very confusing, if you give more thought to it.
Why String is immutable in Java?
String
is immutable in Java. An immutable class is simply a class whose instances cannot be modified. All information in an instance is initialized when the instance is created and the information can not be modified. There are many advantages of immutable classes. This article summarizes why String
is designed to be immutable. This post illustrate the immutability concept in the perspective of memory, synchronization and data structures.
What does a Java array look like in memory?
In Java, an array stores either primitive values (int, char, …) or references (a.k.a pointers) to objects.
When an object is created by using “new”, a memory space is allocated in the heap and a reference is returned. This is also true for arrays, since arrays are objects in Java.
ArrayList vs. LinkedList vs. Vector
1. List Overview List, as its name indicates, is an ordered sequence of elements. When we talk about List, it is a good idea to compare it with Set which is a set of unique and unordered elements. The following is the class hierarchy diagram of Collection. From the hierarchy diagram you can get a … Read more
How does Java handle aliasing?
What is Java aliasing? Aliasing means there are multiple aliases to a location that can be updated, and these aliases have different types. In the following example, a and b are two variable names that have two different types A and B. B extends A. B[] b = new B[10]; A[] a = b; … 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 equals() and hashCode() Contract
The Java super class java.lang.Object defines two important methods: public boolean equals(Object obj) public int hashCode()public boolean equals(Object obj) public int hashCode() In this post, I will first show an example of a common mistake, and then explain how the equals() and hashCode() contract works. 1. A common mistake The common mistake is shown in … 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
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
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