Java Scrappers

Java Scrappers

Share

02/09/2014

Concepts of Serialization :

1) Serialization is the translation of your Java object’s values/states to bytes to send it over network or save it.On other hand,Deserialization is conversion of byte code to corresponding java objects.
2) Good thing about Serialization is entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.\
3) If you want to serialize any class then it must implement Serializable interface which is marker interface.
Marker interface in Java is interface with no field or methods or in simple word empty interface in java is called marker interface
4) serialVersionUID is used to ensure that same object(That was used during Serialization) is loaded during Deserialization.serialVersionUID is used for version control of object.
5) When you serialize any object and if it contain any other object reference then Java serialization serialize that object’s entire object graph.
6) If you don’t want to serialize any field,then make it trasient.
7) If superclass is Serializable then its subclasses are automatically Serializable.
8) If superclass is not Serializable then all values of the instance variables inherited from super class will be initialized by calling constructor of Non-Serializable Super class during deserialization process.
9) If you don’t want subclass to serializable then you need to implement writeObject() and readObject() method and need to throw NotSerializableException from this methods.
10) You can’t serialize static variables.

27/08/2014

Method Overriding with Exception Handling :

There are few things to remember when overriding a method with exception handling. If super class method does not declare any exception, then sub class overridden method cannot declare checked exception but it can declare unchecked exceptions.

1) Example of Subclass overridden Method declaring Checked Exception :

import java.io.*;
class Super
{
void show() { System.out.println("parent class"); }
}

public class Sub extends Super
{
void show() throws IOException //Compile time error
{ System.out.println("parent class"); }
public static void main( String[] args )
{
Super s=new Sub();
s.show();
}
}
As the method show() doesn't throws any exception while in Super class, hence its overridden version can also not throw any checked exception.

2) Example of Subclass overridden Method declaring Unchecked Exception :

import java.io.*;
class Super
{
void show(){ System.out.println("parent class"); }
}

public class Sub extends Super
{
void show() throws ArrayIndexOutOfBoundsException //Correct
{ System.out.println("child class"); }

public static void main(String[] args)
{
Super s=new Sub();
s.show();
}
}
Output : child class
Because ArrayIndexOutOfBoundsException is an unchecked exception hence, overridded show() method can throw it.

If Super class method throws an exception, then Subclass overridden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.
It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).

3) Example of Subclass overridden method with same Exception :

import java.io.*;
class Super
{
void show() throws Exception
{ System.out.println("parent class"); }
}

public class Sub extends Super {
void show() throws Exception //Correct
{ System.out.println("child class"); }
public static void main(String[] args)
{
try {
Super s=new Sub();
s.show();
}
catch(Exception e){}
}
}
Output : child class

4) Example of Subclass overriden method with no Exception :

import java.io.*;
class Super
{
void show() throws Exception
{ System.out.println("parent class"); }
}

public class Sub extends Super {
void show() //Correct
{ System.out.println("child class"); }
public static void main(String[] args)
{
try {
Super s=new Sub();
s.show();
}
catch(Exception e){}
}
}
Output : child class

5) Example of Subclass overriden method with parent Exception :

import java.io.*;
class Super
{
void show() throws ArithmeticException
{ System.out.println("parent class"); }
}
public class Sub extends Super {
void show() throws Exception //Compile time Error
{ System.out.println("child class"); }

public static void main(String[] args)
{
try {
Super s=new Sub();
s.show();
}
catch(Exception e){}
}
}

19/08/2014

Difference between Heap and Stack Memory

1) Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of ex*****on.
2) Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.
3) Objects stored in the heap are globally accessible whereas stack memory can’t be accessed by other threads.
4) Memory management in stack is done in LIFO manner whereas it’s more complex in Heap memory because it’s used globally. Heap memory is divided into Young-Generation, Old-Generation etc.
5) Stack memory is short-lived whereas heap memory lives from the start till the end of application ex*****on.
6) We can use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory. We can use -Xss to define the stack memory size.
7) When stack memory is full, Java runtime throws java.lang.StackOverFlowError whereas if heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error.
8) Stack memory size is very less when compared to Heap memory. Because of simplicity in memory allocation (LIFO), stack memory is very fast when compared to heap memory.

Photos 17/08/2014

Wish u all a very Happy Janmashtami from Java Scrappers Team.

Want your business to be the top-listed Computer & Electronics Service in Noida?
Click here to claim your Sponsored Listing.

Telephone

Address


Sec/62
Noida
201301