raghava.javvaji
BAN USERWe are passing copy of references as arguments to the method. Method manipulates the copies of references but not original reference.
public class PassByReference
{
public static void main(String args[])
{
String st = new String("hi");
method(st);
System.out.println(st);
}
//here q is another reference to 'hi' object
static void method(String q)
{
System.out.println(q);
q = new String("bye");
}
}
Yes, Atomic operation is achieved by only using synchronized array list but not using vector.
- raghava.javvaji September 21, 2012import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class OwnHashSet <E>
{
Map<E,Object> map = new HashMap<E,Object>();
void add(E e)
{
map.put(e,null);
}
Set<E> get()
{
return map.keySet();
}
int size()
{
return get().size();
}
}
Heap dumps are useful to address issues like performance tuning,out of memory exception.Usually in Dev and QA environment,it's difficult to reproduce few issues like OME.So we can generate memory dumps in production in environment by setting argumnts like 'XX: -heapdumponoutofmemryexception' and by using 'visual vm' tools,we can analyze dumps. Dumps give info like how many live instance are there per class and there memory occupation. This info helps u to pin point the root cause.however it's best practice setting references explicitly to null to avoid OME.
- raghava.javvaji September 21, 2012public class SortBinaryArray
{
//Imagine 0 - false , 1 - true
public static void main(String args[])
{
boolean[] bool = new boolean[]{false,true,true,false,true,false,true,false,false,true,true,true,false};
int k =0;
int j =1;
for(int i=0;i<bool.length;i++)
{
if(!bool[i])
{
bool[k++] = false;
}
else
{
int index = bool.length-j++;
if(index > k)
{
bool[index] = true;
}
}
}
for(int i=0;i<bool.length;i++)
{
System.out.println(bool[i]);
}
}
}
public class OneMoreDeadlock
{
public static void main(String args[])
{
Thread t1 = new Thread(new Runnable()
{
public void run()
{
synchronized(String.class)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Waiting for integer");
synchronized(Integer.class)
{
System.out.println("Thread1");
}
}
}
});
Thread t2 = new Thread(new Runnable()
{
public void run()
{
synchronized(Integer.class)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Waiting for string");
synchronized(String.class)
{
System.out.println("Thread2");
}
}
}
});
t1.start();
t2.start();
}
}
Fix: change the order of resources.
Every loaded class in jvm will have java.lang.Class object. To synchronize static method, we have to acquire lock on java.lang.Class object of that particular class.All objects of that particular class have only one lock.
Ex:
public static int getter()
{
syncronize(MyClass.class)
{
}
}
or
public static getter()
{
Class cl = Class.forName("MyClass")
syncronize(cl)
{
}
}
public class TestCollection
{
public static void main(String args[])
{
ArrayList<Number> list = new ArrayList<Number>();
list.add(10);
list.add(20);
list.add(1.5f);
list.add(3);
Collections.sort(list,new xyz());
Iterator<Number> iterator = list.listIterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}
}
class xyz implements Comparator<Number>
{
public int compare(Number arg0, Number arg1)
{
BigDecimal ar0 = new BigDecimal(arg0.doubleValue());
BigDecimal ar1 = new BigDecimal(arg1.doubleValue());
return ar0.compareTo(ar1);
}
}
How about using tree map. It sorts key values automatically. We can put each number from file as key and number of occurrences of that number as value pair. Finally take top k values.
- raghava.javvaji December 26, 2011
RepHello friends my name Neha Nanda from India Chandigarh city. Doing work in SEO line in Softsys company.
RepMy name Madhu Nanda from Himachal pardesh. I am a writter in English lectractrue.
RepI am Susan From Wasilla USA. and my strong interest in yoga and reading historical books. I have a large ...
RepI am an energetic sales professional with a track record of consistently increasing sales revenue in a competitive market. Contract ...
To obey rule i.e if two objects are equal,it means their hash codes are equal. Just imagine an object with 3 properties and respective class has equals method definition which considers only 2 proprieties out of 3. then we should implement hashcode() also by using same prosperities.
- raghava.javvaji September 23, 2012