Cloning In Java
- Shallow copy vs Deep copy
- Techniques to achive
- Object clone method
- Copy contstructor
- Third party tools - Apache common langs, Google gson, Jackson
Immutable Object
Note that final only forbids us from changing the reference the variable holds, it doesn't protect us from changing the internal state of the object it refers to by using its public API:
- declare the class as final so it cannot be extended
- all class members should be private so they cannot be accessed outside of class
- shouldn't contain any setter methods to change the value of class members
- the getter method should return the copy of class members
- class members are only initialized using constructor
Java Generics
Java Serialization
- Classes that are eligible for serialization need to implement a special marker interface, Serializable.
- static fields belong to a class and are not serialized
- we can use the keyword transient to ignore class fields during serialization
- Inheritance : When a class implements the java.io.Serializable interface, all its sub-classes are serializable as well
- Composition : when an object has a reference to another object, these objects must implement the Serializable interface separately, or else a NotSerializableException will be thrown:
- Serial Version UID :
- The JVM associates a version (long) number with each serializable class
- We use it to verify that the saved and loaded objects have the same attributes, and thus are compatible on serialization.
- Any changes result in a different number, and can cause an InvalidClassException
- If a serializable class doesn't declare a serialVersionUID, the JVM will generate one automatically at run-time. However, it's highly recommended that each class declares its serialVersionUID, as the generated one is compiler dependent and thus may result in unexpected InvalidClassExceptions.
- If you're actually using serialization, it only matters if you plan on storing and retrieving objects using serialization directly. The serialVersionUID represents your class version, and you should increment it if the current version of your class is not backwards compatible with its previous version.
- it is good practice to provide the serialVersionUID value and update it after changes to the class so that we can have control over the serialization/deserialization processReferences :
Integer Cache
- Integer Cache was introduced in Java 5 in order to improve memory management.
- Integer Cache works only on auto boxing which means Conversion from a primitive type to an object reference
- By default keeps cache of range between -128 to 127. But this can be increased with
-XX: AutoBoxCacheMax=size parameter - Integer cache gives you the facility to cache other objects also for Example like Byte, Short, Long, Character etc.
Comparing Doubles
- it isn't as easy as comparing other primitive types
- As a matter of fact, it's problematic in many other languages, not only Java.
- Inaccuracy with comparisons using the == operator is caused by the way double values are stored in a computer's memory.
- we can't have an exact representation of most double values in our computers. They must be rounded to be saved
- The recommended algorithm to compare double values in plain Java is a threshold comparison method. In this case, we need to check whether the difference between both numbers is within the specified tolerance, commonly called epsilon:
double epsilon = 0.000001d;
assertThat(Math.abs(d1 - d2) < epsilon).isTrue(); - The smaller the epsilon's value, the greater the comparison accuracy. However, if we specify the tolerance value too small, we'll get the same false result as in the simple == comparison. In general, epsilon's value with 5 and 6 decimals is usually a good place to start.
- Third party libraries to compare doubles : Apache Commons Math, Guava, Junit
How to lose object in Map?
- Equals and hashCode methods implemented by keys are mutable
- Solution : to make keys immutable. If not possible, make sure keys are not changed
BitSet
- we can use BitSets to represent a vector of bits
- similar to boolean[] but takes less memory
- new boolean[1024] take 1 MB
- new BitSet(1024) takes 1024 bit, 168 bytes which is 130 KB