Skip to main content

Posts

Showing posts from 2018

Java Constructors

Constructor as the name suggests is used to construct something and here in case of JAVA it is used to construct the object of class. This is the first point where the object is assigned memory in heap and its member variables gets initialized by default values.Whenever we create an object by using new() keyword,it internally gives call to the default constructor of class. Even if you don't declare the default constructor , java compiler provides automatically a default constructor. Let us test this out with a example . We have created a java class Student.java with a test class TestConstructor.java as shown below  : Student.java TestConstructor.java Here we have created a object of Student class at line 9 using new keyword. This gives call to the default constructor of Student class. Note that we have not defined the default constructor explicitly in Student.java . This is provided by java compiler internally. The equivalent code that java compiler provides is as bel

Deeper Insight Into Java Serialization

Sometimes when a object is serialized at one server and deserialized at other server i.e on different JVMs , deserialization may fall and you can come across  InvalidClassException . This seems to be quite weird. Right? This post is aimed at explaining the root cause of this issue and what should be the better approach while implementing  serialization . You can refer to my other blog on Serialization where we have learnt the serialization mechanism and how to implement serialization  in Java to get started on  serialization. Basically , when we serialize any object , the class metadata is also saved along with object's state which is used while that object is deserialized. One thing that is part of this metadata is  serialVersionUID . In my previous post , I told you that it is used for versioning. We will now see what does it actually mean . You must have seen that whenever you implement serializable interface , compiler gives you warning to generate  serialVersionUID( suid )

Serialization In Java - A Complete Guide

This post is aimed to explain the concept of serialization in depth taking into account all the ins and outs that are required to be known to a Java Developer. Basic Usage And Information : Serialization is a very powerful JAVA instrument aimed at saving only the state of a object not its behaviour. This can be used to exchange objects between different systems and networks as is the case in typical enterprise applications.In a way,we can say that we want the object to exist beyond the lifetime of a JVM. Serialization Mechanism : This involves basically below steps : Firstly the metadata related to serializable instance is written which includes name of class,length of class, number of fields,serialversionuid etc. This metadata is written recursively for superclass if any till it reaches to java.lang.Object superclass. This is helpful when deserializing the class. Once metadata is written, the actual data is written. This time it begins from superclass to main class.

JAVA-Pass By Value Or Reference? Confusion Solved !

This is one of the most confusing topics in Java .The main aim of this post is to solve the confusion in the most simple way possible. Before going into this discussion, first of all we need to understand the meaning and difference between Pass By Value and  Pass By Reference ? Pass By Value : This concept says that whenever a variable is passed to a method/function , a copy of this variable is passed not the actual variable. So the changes that are made to the variable in method are not reflected in the actual variable. Pass By Reference : This concept says that whenever a variable is passed to a method/function, actual variable is passed. So the changes that are made to the variable in method are reflected in the actual variable. Since we have got the basic understanding, lets look at this concept from Java's perspective. In Java there is concept of primitives and non-primitives . Primitives are the basic data types which store the value directly. Non-primitives are def