Singhal Anuj

Greenhorn
+ Follow
since Mar 28, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Singhal Anuj

Hi,

I do not think this code will compile in java 1.4

Regards,
Anuj
11 years ago
Can I call the overridden version of an object's foo() method
outside that object's class, without using super? EG:

class MainProgram {
public static void main(String[] args) {
Beta b = new Beta();

// I want to invoke Alpha.foo() but on the Beta instance.
// I want to cancel the dynamic binding that makes Beta.foo()
// get called instead
// b.super.foo() <---- obviously doesn't compile
}
}

class Alpha {
public void foo() { System.out.println("inside Alpha.foo()"); }
}

class Beta extends Alpha {
public void foo() { System.out.println("inside Beta.foo()"); }
}

I know, it is possible through JNI , but is it possible through reflection?
11 years ago
Hi ,
thanks for the input.
yes, you are right, here, reason that i have not overridden hashCode() method appropriately.
But, i would like to address another aspect of it:-
From API of hashCode() in Object class,
-------------------------------------------------------------
3) It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
--------------------------------------------------------------
Two unequal objects, say o1 and o2, are allowed to return same hashCode,
but going by the Theory of "added in HashSet only when the hashcode is not same "and" == or.equals return false"
suggests that o1 and o2 will not be added in HashSet inspite being unEqual()

I am trying to convey that hashCode should not be the criteria to detect Duplication.
Let me know, if i am wrong.
thanks,
Anuj
12 years ago
Hi,
I am using jdk1.4.2.
I am using HashSet, and seem, it allows me to add duplicate values.

HashSet implements Set interface,
and in Set interface javadoc, it is clearly written that
"A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2)"
Similiar thing is written for add() javadoc.

My code snippet is as follows:-
--------------------------------------------------------------
import java.util.HashSet;

public class TestHashSet
{
public static void main(String arg[])
{
HashSet set = new HashSet();
set.add(new Inner(1));
set.add(new Inner(1));
set.add(new Inner(1));
System.out.println(set.size());
}
}
class Inner
{
private int i=0;
public Inner(int _i)
{
i=_i;
}
public boolean equals(Object o)
{
Inner inner = (Inner)o;
if(inner.i==this.i)
{
return true;
}
else
{
return false;
}
}

/* public int hashCode()
{
return i*1000;
} */
}
-------------------------------------------------------------

This code returns me size as 3.
If i uncomment hasCode() method,
it correctly prints me size as 1.
But, i believe, it should be using only equals() method to check duplication, and not hashCode()

After looking into implementation of HashSet class, found that it uses HashMap to store values.

I am wondering, where i am going wrong?
Please suggest.
thanks,
Anuj
12 years ago
Hi,
I think one way of doing it to create an Object Pool of instances of your class.
You can use WeakHashMap for implementing the Object Pool.
As soon as objects go out of scope, they will be removed from WeakHashMap as well.
Object Pool should be the only source of instances.

Or, you can also use WeakReference, instead of using finalize() method.

thanks,
Anuj
12 years ago
Hi Ley,
Congratulations on your success!
Do u think, RMH is good enough for all objectives except objective 8,10 and 11.
Also, how much weightage does UDDI & JAXR have? Can i think of leaving this topic?
Which chapters did u read from Blue Print? I Guess, chapter 3,7 and 8.
12 years ago
Why java.lang.String overrides equals() method, but java.lang.StringBuffer does not?
13 years ago