Thursday, July 18, 2019

How memory works in Java?

As a developer, it is extremely important to understand how memory management in java works. It can help avoid creating difficult to trace problems.

This blog post would give a simplistic view of memory management in java.

With respect to memory management of Java applications, you need to understand two terms, the stack and the heap
The memory of a Java application is divided into two sections, the STACK and the HEAP.

STACK

In a single Java application, there can be one or more than one stack(s). That is, each thread in Java has its own stack.

Stack is a last in first out data structure. The JVM knows exactly when the data on the stack can be destroyed.
All local variables are created on the stack, and they are automatically popped from the stack on encountering the closing braces of the block that created the variable.

As each thread has its own stack, the data on the stack can only be seen by the thread that owns the stack.

HEAP

Heap allows us to store data that has a longer lifetime than a single code block.
In an application you have a single heap, that is shared across all threads.
In java all objects are stored on the heap.
For an object on the heap, the pointer reference to it will be stored on the stack.

Monday, July 15, 2019

Java Strings


String literals and String Objects


String s1 = "abc";
String s2 = "abc";

Both string s1 and s2 refer to the same string object and value residing in the string literal pool.


String A = new String("abc");
String B = new String("abc");


The two strings A & B are two different string objects, because we used the string constructor to create these two objects. These live in the heap.

Usually you would create string literals over string objects because it gives the compiler a chance to optimize your code.

Immutability of strings.


Strings are also immutable in java. For understanding immutability you can read my post here.

Since strings are immutable, we have classes like StringBuilder and StringBuffer to make string like objects that are immutable.
Both StringBuilder and StringBuffer provide methods like append(), insert(), delete(), substring(), toString() etc. for string manipulation.

You can look at the String APIs here.

StringBuilder vs. StringBuffer


StringBuffer is thread safe as its methods are synchronized whereas StringBuilder is not.


String equality


Given below are three strings.

String a = "hello";
String b = "hello";
String obj = new String("hello");

What would the following equality operators return?

a==obj; // false
a==b; // true
a.equals(obj); //true
a.equals(b); //true

P.S From Java9, strings are now stored as byte arrays instead of char arrays.

Tuesday, July 9, 2019

Java Static Initialization vs. Instance Initialization - When to use which?


Static Initialization

Typically static initialization block is used to initialize static variables of a class. The block is called at the time of class initialization. It is called only once. You can initialize static variables inline. If more complicated logic is required for initialization, a static initialization block can be used. The static initialization blocks are called in the order in which they occur, and they are called before the constructors.

For example:


The output of the above code is as shown below.


Instance Initialization

Instance Initialization or Initializer Block is called whenever an instance of the class is created.
It can be used to execute code that is common to all constructors. This block is executed before the constructor is executed.

For example:

The output of the above code is as shown below.


Ref: https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html