Monday, June 9, 2008

Swapping two values without a temp variable.....

Itz done by using XOR Operator...
int a = 2, b = 3;
a ^= b;
b ^= a;
a ^= b;
System.out.println("A Value : " + a + " : " + "B Value : " +b);

a^=b ---> 010(2) ^ 011(3) ---> 001(1) ---> a = 1
b^=a ---> 011(3) ^ 001(1) ---> 010(2) ---> b = 2
a^=b ---> 001(1) ^ 010(2) ---> 011(3) ---> a = 3

Try this out......

Friday, May 30, 2008

How to make an Class immutable...?

Class should be declared final with all members declared as private and no setter methods....
Immutable class in the sense...the content in the Object of the class can't be changed..

Know about Hashing

Everyone might have used HashMap and HashTable.....have you ever thought howz it handling the keys and values inside...? Even i havnt thought about it....:)

HashTable hashTable_Emp = new HashTable();
hashTable_Emp.put("E1","empName");

It transforms your key to a index value using hash function which enables it for a faster searching mechanism...
To know about hash function----> en.wikipedia.org/wiki/Hash_function
And don forget to hav a look over the hashCode() method under Object class...
http://www.cafeaulait.org/course/week4/38.html