Wednesday, March 13, 2013

different ways to create an object in Java

There are 4 ways of creating objects in Java.

1. Using new operator

Employee emp = new Employee();

Here , we are creating Employee class object  'emp' using new operator.

2. Using factrory methods:

NumberFormat nf  = NumberFormat.getNumberInstace();

Here, we are creatig NumberFormat object using the factory method getNumberInstacne().

3. Using newInstance() method .

(a) First,store the class name  'Employee' as string into an  object. For this purpose , factory method
forName() of the class 'Class' will be useful.

Class c = Class.forName("Employee");

Note: class with the name 'Class' in java.lang package.
(b) create another object to the class whose name is in the object c . For this purpose , we need
newInstance()  method of the class 'Class' . as:

Employee obj = (Employee)c.newInstance();

4. By cloning an already available object, we can create another object . Creating exact copy of an
existing object is called 'cloning':

Employee obj1 = new Employee();
Employee obj2 = (Employee)obj1.clone();

Constructor VS Method in Java

Constructor
 Method
A constructor is used to initialize the instance variables of a class.
A Method is used for any general purpose processing or calculations.
A constructors name and class name should be same.
A method’s name and class name can be same or different.
A constructor is called at the time of creating the object.
A method can be called after creating the object.
A Constructor  is called only once per object.
A method can be called several times on the object.
A constructor is called and executed automatically.
A method is executed only when we call it.


Tuesday, March 12, 2013

Difference between Set and List in Java

Set:

1. A set represents a collection of elements . Order of the elements may change in the set.
2. Set will not allow duplicate values to be stored.
3. Accessing elements by their index(position number) is not possible in case of sets.
4.Sets will not allow null elements.

List:

1. A List represents ordered collection of elements . List preserves the order of elements in
which they are entered.
2. List will allow duplicate value.
3.Accessing elements by index is possible in lists.
4. Lists allow null elements to be stored.

Sunday, March 10, 2013

Difference between HashMap and Hashtable in Java

HashMap:

1. HashMap object is not synchronized by default.
2. In case of a single thread , using HashMap is faster than the Hashtable.
3. HashMap allows null keys and null values to be stored.
4. Iterator in the HashMap is fail-fast . This means iterator will produce exception if concurrent
updates are made to the HashMap.
5. We can make HashMap object synchronied using synchronizedMap() method as:

Collections.synchronizedMap(new HashMap())

Hashtable:

1. Hashtable object is synchronized by default.
2.In case fo multiple threads , using Hashtable is advisable. With a single thread, Hashtable
becomes slow.
3.Hashtable does not allow null keys or values.
4.Enumeration for the Hashtable is not fail-fast. This means even if concurrent updations
are done to to Hashtable, there will not be any incorrect results produced by the Enumeration.


Saturday, March 9, 2013

Difference between ArrayList and Vector in Java

ArrayList:
---------------
1.Array List object is not synchronized by default.
2.In case of a single thread ,using ArrayList is faster than the vector.
3.ArrayList increases its size every time by 50 percent(half).

4. We can synchronize Arraylist using synchronizedList() mehtod as:

List  l = Collections.SynchronizedList(new ArrayList());

Vector:
--------
1.Vector object is synchronized by default.
2. In case of multiple threads, using Vector is advisable. With a single  thread,
   Vector becomes slow.
3. Vector increases its size every time by doubling it.

Friday, March 8, 2013

difference between sleep() and wait() in Java

Both the sleep() and wait() methods are used to suspend a thread execution for a specified time.
When sleep() is executed inside a synchronized block, the object is still under lock .

When wait() method is executed,it breaks the synchronized block , so that the object lock is removed
and it is available.

Generally , sleep() is used for making a thread to wait for some time.But wait() is used in connection
with notify()  or notifyAll() methods in thread communication.

sleep method present in Thread class, wait method present in Object class.
sleep method is static , wait is non static method

Wednesday, March 6, 2013

About Sachin Ramesh Tendulkar



"Sachin Ramesh Tendulkar"                  
                              God of Cricket

   "Commit all your crimes when Sachin is playing... bcoz even GOD is busy watching at his PLAY! "


Perhaps the most complete batsman and the most worshipped cricketer in the world, Tendulkar holds just about every batting record worth owning in the game Following Sachin Tendulkar records list has been growing every year, and will grow until he plays.
            There are no apparent weaknesses in Tendulkar's game. He can score all around the wicket, off both front foot and back, can tune his technique to suit every condition, temper his game to suit every situation, and has made runs in all parts of the world in all conditions




                                                                                                                *As of on Jan'11


List of 100 Records


1.Highest individual score by a single player – 200*
2.First player to play a record 400 innings
3.Number of consecutive ODI appearances – 190 matches
4.Cricket grounds played on – 90 grounds
5.Record number of matches played – 442 matches6.Most runs in a cricket career – 17,598 runs
7.Record number of ODI centuries – 46 Centuries
8.Record number of centuries in ODIs against an opponent – 9 Centuries
9.Record number of centuries in tournament finals – 6 Centuries
10.Record number of centuries in all formats against an opponent – 19 centuries

11.Record number of Test Centuries - 51 Centuries (More than Half Century Centuries in Test Cricket)
12.First player to score 10,000 runs
13.First player to score 11,000 runs
14.First player to score 12,000 runs
15.First player to score 13,000 runs
16.First player to score 14,000 runs
17.First player to score 15,000 runs
18.First player to score 16,000 runs
19.First player to score 17,000 runs
20.Only male player to score a double century in ODIs
21.Tendulkar has scored more than 1,000 runs against every major cricket playing nation
22.Sachin is the only player to score more than 3000 runs against a single opponent – Sri Lanka and Australia
23.Holds the record to score more than 1,000 runs in a single calender year. He has done this 7 times so far.
24.Most number of runs in tournament finals – 1833 runs
25.Most fours in an innings – 25 against South Africa
26.Record number of scores above 150 in an innings – 5
27.Most Half Centuries in ODIs – 93 Half Centuries
28.Highest number of scores over 50 – 139
29.Most Man of the Match awards in ODIs – 61
30.Most Man of the Series awards in ODIs – 19

31.Most half centuries in world cup matches – 13
32.Sachin Tendulkar holds the record for the most runs in ODIs in a calendar year – 1894 runs in 1998
33.Second Fastest to reach 8000 test runs. Record recently broken by Kumar Sangakarra
34.Fastest to reach 12,000 test runs
35.Fastest to reach 13,000 test runs
36.Record centuries in a calendar year – 9 centuries in 1998
37.Highest partnership for any ODI wicket – 331 runs with Rahul Dravid
38.Highest partnership for 2nd wicket in ODIs – 331 runs with Rahul Dravid
39.Highest partnership for 3rd wicket in ODIs – 237* runs with Rahul Dravid

40.Highest partnership aggregated by a pair – 8227 runs with Sourav Ganguly
41.Record opening partnership pair – 6609 runs with Sourav Ganguly
42.Most century partnerships – 26 partnerships with Sourav Ganguly
43.4 wickets and Century in the same match – Against Australia in 1998
44.Sachin Tendulkar has been dismissed a record 3 times on 99 in ODIs
45.Sachin has been the most dismissed batsman in the 90s in International cricket – 24
46.Most 200 run partnerships in ODIs – 6
47.Most century partnerships by an opening pair – 21 with Sourav Ganguly
48.Record number of runs in a cricket world cup – 673 runs in 2003

49.Most runs in cricket world cups combined – 1796 runs
50.Most centuries in International cricket – 94 centuries
51.On Sachin’s Debut, he was the second youngest debutant after Aaqib Javed of Pakistan52.First batsman to complete 31,000 International runs
53.Most test runs – 13447 runs
54.Most dismissals in the 90s in ODIs – 17
55.Most number of Test appearances - 169
56.Most fours in test cricket – 1734+
57.Most century partnerships in Tests – 17 with Rahul Dravid
58.Highest score by an Indian captain – 217 against New Zealand
59.More than 1000 test runs in a single calendar year – 5 times

60.Record number of man of the match awards by an Indian in Test matches – 14 times
61.First batsman to score 50 international centuries
62.First batsman to score 60 International centuries
63.Tendulkar holds the record for being the only batsman to score 70 International centuries
64.Tendulkar holds the record for being the only batsman to score 80 International centuries
65.Tendulkar holds the record for being the only batsman to score 90 International centuries
66.Sachin Tendulkar is the only player to have scored century on debut in the Irani trophy, Ranji trophy and Duleep trophy
67.Most Centuriess in World cup Cricket – 4
68.Most Half Centuries in World Cup cricket – 17
69.Most boundaries in International cricket – 3675

70.Most half centuries in International cricket – 149
71.Most runs in a single edition of the IPL – 618 runs
72.Most runs scored in test matches away from home – 7819 runs
73.Highest overall partnership runs, in all formats combined – 12,400 runs with Sourav Ganguly
74.Most matches in a career – 606 matches
75.Sachin Tendulkar has played for a record number of consecutive matches for India – 239 matches
76.Highest partnership for the 3rd wicket in World Cup – 237 runs with Rahul Dravid
77.Fastest to reach 10,000 Test runs
78.Sachin is currently second after Sanath Jayasuriya for the most ODI appearances, soon expected to overtake
79.Sachin Tendulkar scored 5 centuries before he turned 20
80.Most fours in ODI cricket – 1927 boundaries


81.Ranked by Wisden as the Best ODI batsman of all time
82.Ranked by Wisden as the Second Best Test batsman of all time
83.Highest Indian last wicket partnership – 133 runs with Zaheer Khan
84.Most number of Man of the Match awards in World Cup
85.Sachin Tendulkar and Vinod Kambli were involved in a 664 run partnership in Harris Shield game in 1988. A record broken this year
86.In 1992, Sachin became the first overseas player to play for Yorkshire at the age of 19
87.Has the least percentage of Man of the Match awards when on the losing side. Out of the 56 times, India has lost 5 times
88.Sachin has won the Padma Shri, Arjuna Award and Rajiv Gandhi Khel Ratna awards, and he is the only cricketer to receive them all
89.One of the few batsmen to have Centuries against all Test Playing countries
90.The first batsman to be given out by a Third Umpire TV reviewal system
91.Sachin holds the record for being the only player to have 40 wickets and 11000 runs in Test cricket
92.Sachin holds the record for being the only player to have 150 wickets and 15000 runs in ODI cricket

93.Sachin Tendulkar holds the best average amongst the batsman who have crossed 10,000 ODI runs
94.The highest run scorer in the 199 and 2003 cricket world cups
95.Sachin is the only player to have more than 100, fifty plus scores
96.The only batsman to have scored more than 2 centuries against all Test playing nations
97.Sachin has completed his Century by hitting a 6, on four different occasions in test matches, a record shared by Ken Barrington
98.Only cricketer to have done 10,000 runs, 100 wickets and 100 catches in ODIs
99.The only player to be in Top 100 of ICC rankings for 10 years
100.Sachin is the cricketer to have a record number of records.

Take a Bow Master!!!