Wednesday 21 February 2018

Ways to create objects in Java

Different ways to create objects in java


1. Using the new keyword

It is the most common and regular way to create an object and actually very simple one also. By using this method we can call whichever constructor we want to call (no-arg constructor as well as parametrised).

 Employee emp1 = new Employee(); 

2. Using Class.newInstance() method

We can also use the newInstance() method of the Class class to create objects, This newInstance() method calls the no-arg constructor to create the object.
We can create objects by newInstance() in following way.

Employee emp2 = (Employee) Class.forName("com.Employee").newInstance();

Or

Employee emp2 = Employee.class.newInstance();

3. Using newInstance() method of Constructor class

Similar to the newInstance() method of Class class, There is one newInstance() method in the java.lang.reflect.Constructor class which we can use to create objects. We can also call a parameterized constructor, and private constructor by using this newInstance() method.

Both newInstance() methods are known as reflective ways to create objects. In fact newInstance() method of Class class internally uses newInstance() method of Constructor class. That's why the later one is preferred and also used by different frameworks like Spring, Hibernate, Struts etc. 

Constructor<Employee> constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();

4. Using clone() method

Whenever we call clone() on any object JVM actually creates a new object for us and copy all content of the previous object into it. Creating an object using clone method does not invoke any constructor.

To use clone() method on an object we need to implements Cloneable and define clone() method in it.

Employee emp4 = (Employee) emp3.clone();

Java cloning is the most debatable topic in Java community and it surely does have its drawbacks but it is still the most popular and easy way of creating a copy of any object until that object is full filling mandatory conditions of Java cloning.

5. Using deserialization

Whenever we serialize and then deserialize an object JVM creates a separate object for us. In deserialization, JVM doesn’t use any constructor to create the object.
To deserialize an object we need to implement the Serializable interface in our class.

ObjectInputStream in = new ObjectInputStream(new FileInputStream("data.obj"));
Employee emp5 = (Employee) in.readObject();





Saturday 17 February 2018

Does ArrayList in Java have default size?

Surprised ? Many developers might not aware of the fact that arraylist have some default size. Normally we consider arraylist with "0" size. Lets have a dig at the concept : 
ArrayList uses an array to store the elements. Arrays have a fixed size. The array that ArrayList uses has to have a default size, obviously. 10 is probably a more or less arbitrary number for the default number of elements. When you create a new ArrayList with nothing in it, then ArrayList will have made an array of 0 element, and when you add first element then it create array of 10 elements behind the scenes. Ofcourse those 9 elements are all null.
Using This line ...! 
List list=new ArrayList<String>();
Made an array of 0 element
list.add("FirstValue");
It create array of 10 elements behind the scenes with "FirstValue" at its 0 index.
Every time the array is full, ArrayList creates a new, larger array and copies the elements from the old array to the new array.

Note :- (copying the array time so better to think about its first)
if list.size == 9 and we do list.add("EleventhString")

It will create new array and copy all the data in newly created array.
The size of new arary depends on the current size of array and the algorithm is 
(CurrentSize of Array * 3/2) + 1

Wednesday 14 February 2018

Why Java declared String as immutable?

The Strings in java are immutable because they are stored in the "String Pool". The basic property of String pool is not to allow the "duplicate strings". 

i.e. if we declare,

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

instead of 2 String objects only one String object will be created in String pool as content of both the Strings are equal. Ofcourse, this saves lot of memory at runtime. 

But because of this, there might be possibility that client1 changes the object referred by abc and client2, would get the modified String. So, to avoid this String objects are immutable in Java.

Another reason of why String class is immutable could die due to HashMap.

Since Strings are very popular as HashMap key, it's important for them to be immutable so that they can retrieve the value object which was stored in HashMap. Mutable String would produce two different hashcodes at the time of insertion and retrieval if contents of String was modified after insertion, potentially losing the value object in the map.

At the same time, String was made final so that no one can compromise invariant of String class e.g. Immutability, Caching, hashcode calculation etc by extending and overriding behaviors. 


Attend Online Java Certification Training and excel your career

Hello Java Developer,  Are you staying at home and worried about your future? Do not waste the time in worrying. International certifi...