Saturday, 5 September 2015

Benefits of Spring Framework

Lightweight:
Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB.
IoC containers tend to be lightweight, especially when compared to EJB containers.
This is beneficial for developing and deploying applications on computers with limited memory and CPU resources.

Inversion of control (IOC):
Loose coupling is achieved in Spring, with the Inversion of Control technique. The objects give their dependencies instead of creating or looking for dependent objects.
With the Dependency Injection (DI) approach, dependencies are explicit and evident in constructor or JavaBean properties.

Aspect oriented (AOP):
Spring supports Aspect oriented programming and separates application business logic from system services.

Container:
Spring contains and manages the life cycle and configuration of application objects.

MVC Framework:
Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks.

Transaction Management:
Spring provides a consistent transaction management interface that can scale down to a local transaction and scale up to global transactions (JTA).
Spring provides a consistent transaction management interface that can scale down to a local transaction (using a single database, for example) and scale up to global transactions (using JTA, for example).

Exception Handling:
Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.

Testing:
Testing an application written with Spring is simple because environment-dependent code is moved into this framework. Furthermore, by using JavaBean-style POJOs, it becomes easier to use dependency injection for injecting test data.

Spring does not reinvent the wheel instead; it truly makes use of some of the existing technologies like several ORM frameworks, logging frameworks, JEE, Quartz and JDK timers, other view technologies.
Spring is organized in a modular fashion. Even though the number of packages and classes are substantial, you have to worry only about ones you need and ignore the rest.
Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over engineered or less popular web frameworks.

Thursday, 3 September 2015

Stack vs. Heap Memory

Java Heap Memory

Heap memory is used by java runtime to allocate memory to Objects and JRE classes. Whenever we create any object, it’s always created in the Heap space.

Garbage Collection runs on the heap memory to free the memory used by objects that doesn’t have any reference. Any object created in the heap space has global access and can be referenced from anywhere of the application.

Java Stack Memory

Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method.

Stack memory is always referenced in LIFO (Last-In-First-Out) order. Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. As soon as method ends, the block becomes unused and become available for next method.

Difference between Heap and Stack Memory

Heap memory
Stack memory
Heap memory is used by all the parts of the application.
whereas stack memory is used only by one thread of execution.

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.
Objects stored in the heap are globally accessible.

Stack memory can’t be accessed by other threads.
Heap memory is more complex because it’s used globally and  Heap memory is divided into Young-Generation, Old-Generation etc.

Memory management in stack is done in LIFO manner.
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.
If heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error.

When stack memory is full, Java runtime throws java.lang.StackOverFlowError.

Because of simplicity in memory allocation (LIFO), stack memory is very fast when compared to heap memory.

Heap memory lives from the start till the end of application execution.

Stack memory is short-lived.
Stack memory size is very less when compared to Heap memory.


Tuesday, 1 September 2015

Method Overloading

Method Overloading

Suppose that you have a class that can use calligraphy to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type.

It is cumbersome to use a new name for each method—for example, drawString, drawInteger, drawFloat, and so on.

Alternatively, we can use four methods named draw with different parameter list.

Thus, the data drawing class might declare four methods named draw, each of which has a different parameter list.

public class DataArtist {
    ...
    public void draw(String s) {
        ...
    }
    public void draw(int i) {
        ...
    }
    public void draw(double f) {
        ...
    }
    public void draw(int i, double f) {
        ...
    }
}

static String valueOf(boolean b)
       static String valueOf(char c)
       static String valueOf(char[] data)
       static String valueOf(char[] data, int offset, int count)
       static String valueOf(double d)
       static String valueOf(float f)
       static String valueOf(int i)
       static String valueOf(long l)
       static String valueOf(Object obj)

This means that ifwe have any type of variable, we can get a String representation of it by using String.valueOf(variable).


draw(String s) and draw(int i) are distinct and unique methods because they require different argument types.

Monday, 31 August 2015

Method Overriding

Method overriding

It is used when a class that extends from another class wants to use most of the feature of the parent class and wants to implement specific functionality in certain cases.

It is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes.

The implementation in the subclass overrides (replaces) the implementation in the super-class by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the super-class overrides the super-class's method.


Overriding is a feature that is available while using Inheritance.


This way the new method masks the parent method and would get invoked by default.

class Thought {
       public void message() {
              System.out.println("I feel like I am diagonally parked in a parallel universe.");
       }
}

public class Advice extends Thought {
       @Override  // @Override annotation in Java 5 is optional but helpful.
       public void message() {
              System.out.println("Warning: Dates in calendar are closer than they appear.");
       }
}

Return type in method Overriding

Before Java 5.0, when you override a method, both parameters and return type must match exactly.

In Java 5.0, it introduces a new facility called covariant return type, override a method with the same signature but returns a subclass of the object returned.
A method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the super-class.

After JDK 1.5, java supports covariant returns, that is, the specialization of the return type to a subtype.
A method declaration d1 with return type R1 is return-type-substitutable for another method d2 with return type R2, if and only if the following conditions hold:
· If R1 is void then R2 is void.
· If R1 is a primitive type, then R2 is identical to R1.
· If R1 is a reference type then:
· R1 is either a subtype of R2 or R1 can be converted to a subtype of R2 by unchecked conversion, or

· R1 = |R2|

Rotate 2-D matrix by 90

Given N*M matrix, rotate it by 90 degree right

package core.geeks;
class Rotate90Degree {
      
       private static void rotateMatrix(int[][] array) {
              int oRow = array.length;
              int oCol = array[0].length;
              int[][] tArr = new int[oCol][oRow];

              int tCol = 3;
              for (int i = 0; i < oRow; i++){
                     for(int j = 0;  j < oCol; j++){
                           tArr[j][tCol] = array[i][j];
                     }
                     tCol--;
              }

              printGrid(tArr);
       }
      
       public static void main(String args[] ) throws Exception {
              int[][] array = {  {0, 1, 2, 3, 4},
                           {5, 6, 7, 8, 9},
                           {10, 11, 12, 13, 14},
                           {15, 16, 17, 18, 19}};
              rotateMatrix(array);
       }

       public static void printGrid(int[][] a) {
              for(int i = 0; i < a.length; i++) {
                     for(int j = 0; j < a[i].length; j++) {
                           System.out.printf("%5d ", a[i][j]);
                     }
                     System.out.println();
              }
       }
}

Output:
   15    10     5     0
   16    11     6     1
   17    12     7     2
   18    13     8     3
   19    14     9     4


Related Posts Plugin for WordPress, Blogger...