Recent Updates Toggle Comment Threads | Keyboard Shortcuts

  • MohanaRao SV 7:28 pm on April 27, 2012 Permalink | Reply
    Tags: java memorymanagement   

    Memory management in java 

    Heap: is a single area where JVM allocates memory for -Objects, including method code , static variables & instance variables.

    Stack: Stack is created for each individual thread, JVM allocates memory for – local variables & arguments(reference) (values passed to method variables)

    Note : interface – all values in interface are constants i.e final static, so it’s stored on Heap only.

    When we have a declaration like this

    class Sample{ 
    int e = 1; 
    public int math (int x, int y){ 
    A a = new A(); 
    return (A.e + x + y); 
    } 
    } 
    

    Then we have:
    Stack: x, y, a
    Heap: instance a (it is A object), a.e = 1
    (Note that a in stack points out to instance a in heap)
    If instance a is no longer used, it is garbage collected

     
  • MohanaRao SV 3:22 pm on March 18, 2012 Permalink | Reply
    Tags: Java   

    Method to count number of repeated special characters in a given String. 

    
    public Map<Character, Integer> charIntMap(String str) {
    		Map<Character, Integer> map = new HashMap<Character, Integer>();
    		char[] charString = str.toCharArray();
    		int count = 1;
    		for (int i = 0; i < (charString.length - 1); i++) {
    			if (!Character.isLetter(charString[i])) {
    				if (!map.containsKey(charString[i])) {
    					map.put(charString[i], count);
    				} else {
    					map.put(charString[i], count++);
    				}
    			}
    		}
    		return map;
    	}
    
    
     
  • MohanaRao SV 1:02 am on March 12, 2012 Permalink | Reply
    Tags: core java.   

    Example for overriding Hashcode and Equals methods. 

    Whenever you are working with the collections HashMap, HashSet then you have to override pojo hashcode and equals method.

    
    package com.stackoverflow.java.core;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class Employee {
    	private String name;
    	private String sex;
    	private int salary;
    	private int age;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getSex() {
    		return sex;
    	}
    
    	public void setSex(String sex) {
    		this.sex = sex;
    	}
    
    	public int getSalary() {
    		return salary;
    	}
    
    	public void setSalary(int salary) {
    		this.salary = salary;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	@Override
    	public int hashCode() {
                      //Not taken care when genrating hashcode.
    		int hashcode = this.age * 2 + this.salary * 2;
    		return hashcode;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != this.getClass())
    			return false;
    		Employee employee = (Employee) (obj);
    		if ((this.name.equals(employee.name) && (this.sex.equals(employee.sex)
    				&& (this.age == employee.age) && (this.salary == employee.salary)))) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    
     
  • MohanaRao SV 1:44 pm on February 22, 2012 Permalink | Reply
    Tags: Struts 2.   

    Advantages of Struts 2.0 compare to Struts 1.2 


    1) Struts 1.2 is class driven but the framework should be an interface driven(according to the proper design) so that we can extend some other classes

    Struts 2.0 is interface driven we can write without using interface or with interface or with a class

    2) In Struts 1.2 checkboxes are required to control it using reset() method

    In Struts 2.0 check boxes does not require any kind of special appl for false values

    3) In struts 1.2 all the action classes must extend action.

    In struts 2.0 any class can be used as an action class and it can input properties by using any Java Bean directly to the action class.

    4)Struts 2.0 actions are spring friendly and so easy to integrate with spring.

    5)Struts 1.2 we have to use DWR if we require the Ajax feature.

    In struts 2.0 we will be getting the Ajax features directly.

    6)In struts 1.2 Request Processor is applicable for all the flows and it is a drawback.

    In Struts 2.0 we can implement Request Processing logic for specific flows using Interceptor concept.

    Struts  +   WebWork     =    Struts 2.0

    7) In struts 1.2 for all the requests one action object will be created and used for all the requests.

    In Struts 2.0 for each request one action object will be created because it contains the form data also.

    8)In Struts 1.2 we should not declare instance variables in the action class because multiple threads will be using the same action object and instance variables.

    In Struts 2.0 we can declare instance variables.

    In Struts 1.2 if we want to use instance variables then use synchronized blocks

    9) Struts 2.0 is a simplified design because it is interface driven and those are just like POJO’s.

    10)Struts 2.0 is not http dependent, It is loosely coupled.

    11)In Struts 2.0 action classes are simplify it’s like POJO’s and using inversion of control If we require we can get request and response objects.

    12)No more action forms in Struts 2.0

    13)Struts 2.0 testing is more easy because it’s not API dependent.

    14)In Struts 2.0 all the basic default configuration we can get it automatically.

    extends =”struts-default”

    15)In Struts 1.2 it is difficult to switch from one presentation to another presentation layer.

    In Struts 2.0 it is very simple to switch from one presentation to another.

    16) In Struts 2.0 we get improved Results it’s like Action Forwards.

    Struts 2.0 Results provides flexibility to create multiple types of outputs and in actual it helps to prepare the response.

    17) In struts 2.0 we can get better Tag features like we can get the tags for freemarker also.

    18)In struts 2.0 Annotations are introduced Applications in 2.0 can use the java 5 annotations as an alternative to xml and java properties configuration. Annotations minimize the use of XML.

    19)Validations

    Struts 1.2 and Struts 2.0 both supports the manual validation via a validate method.

    Struts 1.2 uses validate method on the ActionForm, or validates through an extension to the commons validator.

    Struts 2.0 supports manual validation via the validate method and the xwork validation framework, The xwork validation framework supports chaining validation into sub-properties using the validations defined for the properties class type and the validation context.

    20) Threading Modal

    In struts 1.2 action resources must be thread safe or synchronized, so actions  are singleton and thread safe.

    There should only be one instance of a class to handle all requests for that action. The singleton strategy  places restrictions on what we can be done with struts 1.2. Actions and require extra care to develop.

    In Struts 2.0, Actions objects are instantiated each request, so there are no thread-safety issues. In practice, servlet containers generate many throw away objects per request, and one more objet doesn’t impose a performance penalty or impact garbage collection.

    21) Type Conversion

    Usually Struts 1.2 ActionForm properties are all strings, Struts 1.2 uses “common-BeanUtils” for type conversion. These type convertors are per-class and not configurable per instance.

    Struts 2.0 uses OGNL for type conversion, The Framework includes converters for basic and common  object types and primitives.

    22) Binding Values into views in the view Section.

    Struts 1.2 uses the standard jsp mechanism to bind objects (processed from the model section) into the page context to access.

    Struts 2.0 uses a “ValueStack” technology so that the taglibs can access the values without coupling your view to the object type it is rendering. The ValueStack strategy allows the reuse of views across the range of types which may have the same property name b different property types.

    23)Expression Language

    Struts 1.2 integrates with JSTL, so it uses the JSTL-EL. The struts EL has logic object graph traversal, but relatively weak collection and indexed property support.

    Struts 2.0 can also use JSTL, however it supports more powerful and flexible expression language called “Object Graph Notation Language”(OGNL)

    24)Harvesting Input

    Struts 1.2 uses and ActionForm object to capture input, and all ActionForms needs to extend a framework dependent base class. Java Beans cannot be used as ActionForms, so the developers have to create redundant classes to capture input.

    Struts 2 uses Action Properties (as input properties independent of underlying Framework) that eliminates the need for a second input object, hence reduce redundancy.

    Additionally Struts2, Action properties can be accesses from the web page via the tag libs.

    Struts 2.0 also supports the Action form properties as well as POJO from objects and POJO Actions. Even rich object types, including business or domain objects, can be used as input/output objects.

    Interceptors that can be preprocess and/or postprocess a request.

    Action classes that can call business logic and data access code.

    Results that can prepare views using JavaServerPages, Velocity and FreeMarker templates.

     
    • Umesh 9:11 pm on March 21, 2012 Permalink | Reply

      Nice and exhaustive list!!!
      but i am wondering why we need to compare something which is beyond comparison except there name are same

      • MohanaRao SV 7:03 pm on March 23, 2012 Permalink | Reply

        Truly it would be helpful to the people who are migrating from strut1.x to Struts2.

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
Follow

Get every new post delivered to your Inbox.