MARKER INTERFACE

What is a Marker Interface?

Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface.

What Is the Use?

It is used to convey to the JVM that the class implementing an interface of this category will have some special behavior. It means it will tell jvm that this class have some functionality or capable of doing something.

Simple Understanding:

I will give you a non-technical example.Two things are there, a MOBILE and a CAR. I am having one room. What ever i will put into that room it will wash it give me back. if i will put my car then it will wash it and give me back,but if i put my mobile then it will be washed and it will be damaged. So what exactly I am going to do here is I will tell to the room that which is washable and which is not.So in this case we go for a MARKER interface.

In technical words i will create one marker interface “Washable” and i will implement this interface in Car but not in Mobile, so before washing the room should check is it a instance of washable interface or not.So by this we can overcome our problem.

More On Marker Interface:

In java we have the following major marker interfaces as under:

  • Searilizable interface
  • Cloneable interface
  • Remote interface
  • ThreadSafe interface

The marker interface can be described as a design pattern which is used by many languages to provide run-time type information about the objects. The marker interface provides a way to associate metadata with the class where the language support is not available.

A normal interface specifies functionality which an implementing class must implement. But a marker interface does not follow that pattern. On the other side, the implementing class defines the behavior. There are some hybrid interfaces which act as a marker interface along with some methods. But this type of design is confusing if not handled carefully.

Usage of Marker Interface in java:

Marker interface in Java e.g. Serializable, Clonnable and Remote is used to indicate something to compiler or JVM that the class implementing any of these would have some special behavior. Hence, if the JVM sees a Class is implementing the Serializable interface it does some special operation on it and writes the state of the object into object stream. This object stream is then available to be read by another JVM. Similarly if JVM finds that a class is implementing Clonnable interface, it performs some special operation in order to support cloning. The same theory goes for RMI and Remote interface. This indication (to the JVM) can also be done using a boolean flag or a String variable inside the class.

Apart from using the built-in marker interface, to mark a class as serializable or clonnable, we can also have our own marker interface. Marker interface is a good way to logically segregate the code and also if we have our own tool to perform some preprocessing operation on the classes. It is very useful for developing frameworks or APIs e.g. struts or spring.

With the introduction of annotation in java 5, annotation has become a better choice over maker interface.

The Thread Safe interface is a marker interface which can be used to communicate to other developers that classes implementing this marker interface gives thread-safe guarantee and any modification should not violate that. Marker interface also helps code coverage or code review tool to find bugs based on specified behavior of marker interfaces. Here also annotations are better choice. @ThreadSafe looks lot better than implementing Thread Safe marker interface.

In the next part I will come up with real time example of marker interface to make you understand better.Follow for more topics.

Java-Marker-Interface

 

GARBAGE COLLECTION

  • Garbage Collection Is The Process Of Looking At Heap Memory, Identifying Which Objects Are In Use And Which Are Not, And Deleting The Unused Objects.
  • An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.
  • In java the process of garbage collection is automated.

STEPS OF GARBAGE COLLECTION:

STEP-1 (MARKING)

The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not.

part1

STEP 2:( NORMAL DELETION)

Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.

part2

 

STEP-3: (COMPACTING)

To further improve performance, in addition to deleting unreferenced objects, you can also compact the remaining referenced objects. By moving referenced object together, this makes new memory allocation much easier and faster.

part3

 

GENERATIONAL GARBAGE COLLECTION:

The partition of objects into different generations (time intervals) based on time of allocation, and giving them different GC policies depending on age. Based on the heuristic that most objects are discarded shortly after being used–hence the GC is tuned to get rid of those first.

part4

Here is an example of such data. The Y axis shows the number of bytes allocated and the X access shows the number of bytes allocated over time.

As you can see, fewer and fewer objects remain allocated over time. In fact most objects have a very short life as shown by the higher values on the left side of the graph.

part5

The heap is broken up into smaller parts or generations. The heap parts are:   Young Generation, Old or Tenured Generation, and Permanent Generation.

New Feature of Java 1.8

Want to share some new features of Java 1.8. It’s high time to look into these features.

 One of the new feature of Java 1.8 is from Java 8, interfaces are enhanced to have method with implementation. We can use default and static keyword to create interfaces with method implementation. 

We know that Java doesn’t provide multiple inheritance in  Classes because it leads to Diamond Problem. So how it will be handled with interfaces now, since interfaces are now similar to abstract classes. The solution is that compiler will throw exception in this scenario and we will have to provide implementation logic in the class implementing the interfaces.

I will discuss with examples for a better understanding in my next post. 


Java: The beginning

Just going to start with Java.this is the first blog.share your opinion.

Java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

1) James GoslingMike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The small team of sun engineers called Green Team.

2) Originally designed for small, embedded systems in electronic appliances like set-top boxes.

3) Firstly, it was called “Greentalk” by James Gosling and file extension was .gt.

4) After that, it was called Oak and was developed as a part of the Green project.Latter in 1995 it named as “JAVA”.