Tuesday, November 16, 2021

Apache vs Tomcat


When we say three tier : 
Apache <--> Tomcat <--> {DB, MessageQues,..}
ClientFace<-> web logic <-->External

Courtesy: https://youtu.be/XABDkzxA6hM

Monday, November 15, 2021

Java exceptions and Catching Throwable

Courtesy: https://www.baeldung.com/java-catch-throwable-bad-practice

- create exception by extending RuntimeException is unchecked 
- create exception by extending Exception is checked.

Thursday, November 4, 2021

Transient and Serialization

 Learning Notes:

- Any variable/member is marked as "transient", then the content of this variable will not be serialized. It sets default values while serialization. 

public class Student implements Serializable { private static final long studentUID = -2936687026040726549L; private String studentName; private transient String subject; private transient int marks; // getters and setters 

} 

and creates a Student object with ("VRN", "Java", 85)

When we serialize this object using ObjectoutputStream and deserialize then the content of 'subject' variable will be null and 'marks' will be zero.


Note: Please note that when a variable is declared with final + transient: Behaviour will be the same. Means, while serialization value wont be written , while desrialization because there is no value the final value will be taken as default. 

private final String subject = "C++";

while seirialization the file will be written with NULL, but when we deserialize 'C++' will come as this is default value ( because of final )

--


So, while writing custom "serialization", we can declare that variable  as a transient and implement  customized writeObject and readObject functions. Use this functions while serializing/deserializing.


public class Student implements Serializable {

private transient Address address;

..

private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); oos.writeObject(address.getHouseNumber()); } private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); Integer houseNumber = (Integer) ois.readObject(); Address a = new Address(); a.setHouseNumber(houseNumber); this.setAddress(a);

}

}


courtesy: baeldung knowledge.

Wednesday, November 3, 2021

Converting Mutable class to Immutable class in java

 - Make class as final to ensure this class wont get extended

- Initialize all fields in Constructor

- Remove/Dont-Add Setters

- Make all mutable members as final , so that all these members gets initialised only once.

- Make all fields Private so that they are not accessible through object

- Special attention needs to be given for Mutable members of the class. Incase to return the value of mutable, then do deepcopy/clone and send it rather than the reference.


Found one good example: 

courtesy:https://howtodoinjava.com/java/basics/how-to-make-a-java-class-immutable/


https://www.informit.com/articles/article.aspx?p=20530


public final class ImmutableClass
{
 
    /**
    * Integer class is immutable as it does not provide any setter to change its content
    * */
    private final Integer immutableField1;
 
    /**
    * String class is immutable as it also does not provide setter to change its content
    * */
    private final String immutableField2;
 
    /**
    * Date class is mutable as it provide setters to change various date/time parts
    * */
    private final Date mutableField;
 
    //Default private constructor will ensure no unplanned construction of class
    private ImmutableClass(Integer fld1, String fld2, Date date)
    {
        this.immutableField1 = fld1;
        this.immutableField2 = fld2;
        this.mutableField = new Date(date.getTime());
    }
 
    //Factory method to store object creation logic in single place
    public static ImmutableClass createNewInstance(Integer fld1, String fld2, Date date)
    {
        return new ImmutableClass(fld1, fld2, date);
    }
 
    //Provide no setter methods
 
    /**
    * Integer class is immutable so we can return the instance variable as it is
    * */
    public Integer getImmutableField1() {
        return immutableField1;
    }
 
    /**
    * String class is also immutable so we can return the instance variable as it is
    * */
    public String getImmutableField2() {
        return immutableField2;
    }
 
    /**
    * Date class is mutable so we need a little care here.
    * We should not return the reference of original instance variable.
    * Instead a new Date object, with content copied to it, should be returned.
    * */
    public Date getMutableField() {
        return new Date(mutableField.getTime());
    }
 
    @Override
    public String toString() {
        return immutableField1 +" - "+ immutableField2 +" - "+ mutableField;
    }
}