Object Initialization concept:-
The Java language has three mechanisms dedicated to ensuring proper initialization of objects:
1) instance initializers (also called instance initialization blocks),
2) instance variable initializers, . (Instance initializers and instance variable initializers collectively are called "initializers.")
3) constructors All three mechanisms result in Java code that is executed automatically when an object is created. When you allocate memory for a new object with the
new
operator or the newInstance()
method of class Class
, the Java virtual machine will insure that initialization code is run before you can use the newly-allocated memory. If you design your classes such that initializers and constructors always produce a valid state for newly-created objects, there will be no way for anyone to create and use an object that isn't properly initialized.you can't have both
this()
and super()
in the same constructor. You can only have one or the other (or neither, if the direct superclass includes a no-arg constructor). If a constructor includes a this()
or super()
invocation, it must be the first statement in the constructor.what is the difference between iterator and loops
One big advantage of the first option (Loop ) is, that you always know, at which position you are.
One big advantage of the second option (iterater) is, that you don't have to know the size of your Collection.
One big advantage of the second option (iterater) is, that you don't have to know the size of your Collection.
Differences between Iterator & Enumeration:
Enumeration is twice as fast as Iterator and uses very less memory. Enumeration is very basic and fits to basic needs.
But Iterator is much safer as compared to Enumeration, b’ coz it always denies other threads to modify the collection object which is being iterated by it. Whenever a second thread tries for that Iterator will throw a ConcurrentModificationException.
Iterator.remove() is the only safe way to modify a collection during iteration. In Enumeration, there is “no safe way” to remove elements from a collection while traversing.
What is the difference b/w Iterator & ListIterator...
ListIterator:--An iterator for lists that allows one to traverse the list in either direction.modify the list during iteration and obtain the iterator's current position in the list. A ListIterator has no current element. its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). In a list of length n there are n+1 valid index values from 0 to n inclusive.
No comments:
Post a Comment