Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

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

Thursday, May 15, 2008

Java Versionz...

  • JDK 1.0 (January 23, 1996)
  • JDK 1.1 (February 19, 1997)
  • J2SE 1.2 (December 8, 1998)
  • J2SE 1.3 (May 8, 2000)
  • J2SE 1.4 (February 6, 2002)------------>Merlin
  • J2SE 5.0 (September 30, 2004)-------->Tiger
  • Java SE 6 (December 11, 2006)-------->Mustang
  • Java SE 6 Update 10
  • Java SE 7------------------------------>Dolphin

Converting String array to a ArrayList

String[] myArray = new String[]{"One", "Two", "Three"};
ArrayList myList = new ArrayList(Arrays.asList(myArray));

Arrays.asList returns you a fixed size list backed by the specified array...