Updates from April, 2012 Toggle Comment Threads | Keyboard Shortcuts

  • Unknown's avatar

    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

     
  • Unknown's avatar

    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;
    	}
    
    
     
  • Unknown's avatar

    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;
    		}
    	}
    
     
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
Design a site like this with WordPress.com
Get started