Category: Java

  • Soft, Weak, Phantom Reference

    java.lang.ref package provides soft reference, weak reference and phantom reference as a class. The objects that are created from those classes are considered specially by GC which makes the developers to interact with GC with a limited access. Weak Reference java.lang.ref.WeakReference class creates a WeakReference object by encapsulating the object…

  • Java Garbage Collector and Reachability

    What does Java Garbage Collector do? Finds out the garbage objects from Heap space. Cleans up the garbage objects and retrieve the heap memory In the early Java, application codes were not able to involve into what GC does. However, after JDK 1.2, java.lang.ref enabled the developers to interact with…

  • [Effective Java] Item 7 – Eliminate obsolete object references

    1. Null out references once they become obsolete The Figure 1. code has a possible memory leak, as the objects popped off from the stack will not be garbage collected. This is because the stack will maintain obsolete references (References that will never be dereferenced again) to the objects that…

  • Why is String class immutable?

    There are 5 different reasons why String class is made as immutable in Java. 1. String Pool String literals are stored in a special space which is called String Pool in heap space. Variables that refer to the same object on String Pool would get affected if the String literals…

  • Difference between String literal and String object

    There are two different ways to express String in Java, first one is to use String literal like and the other one is to use String object like String literal vs String object Creating a String object with new(), it always creates a new object in heap memory where all…

  •  [Effective Java] Item 6 – Avoid creating unnecessary objects

    1. Reuse objects Rather than creating new objects that does the same job every time, reuse the single object by using static factory methods or immutable object. For example, use Boolean.valueOf(String) which is a static factory method rather than Boolean(String). Immutable objects like String can be reused any time. For…

  • [Effective Java] Item 5 – Prefer dependency injection to hardwiring resources

    Summary Classes that depends on underlying resources are not appropriate to be static utility class or singleton class. For example, the dictionary can be different maps per usage, but we cannot declare new classes every time per purpose. Pass the resource into the constructor when creating a new instance

  • [Effective Java] Item 4 – Enforce noninstantiability with a private constructor

    Summary Utility classes which are made of static methods and static fields are not designed to be instantiated. Without explicit constructors, the compiler automatically creates a default constructor, which means it creates a public constructor with no parameter. Regardless of its intention, this leaves the class to be instantiable. To…

  • Numeric Data Types in Java

    Numeric Data Types in Java

    Integral Data Types Integral data type is a data type that represents numeric data whose values are integers. From Java, there are 5 integral data types, byte , short, int long and char. 1. byte data type byte data type is a 8 bit signed integral data type Range from…

  • Bit Manipulation in Java

    Signed vs Unsigned Signed variables – Such as signed integers, which can store both positive and negative range of numbers. Unsigned variables – Such as unsigned integers that will only represent positive numbers. Both signed and unsigned variables of the same data type have the same range, but unsigned variable…