Constructor
•A constructor initializes an object immediately upon creation.
•It has the same name as the class in which it resides and it is syntactically similar to a method.
•Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes.
•Constructors look a little strange because they have no return type, not even void.
•This is because the implicit return type of a class’ constructor is the class type itself. It is the constructor’s job to initialize the internal state of an object.
•By doing this the code creating an instance will have a fully initialized, usable object instantly.
Types of Constructor
•Default Constructor:
It is a constructor with no arguments passed to it.
•Parameterized Constructor:
It is a constructor with parameters or arguments.
•Constructor Overloading:
Usually in this more than one type of constructor is used in same program.
this Keyword
•‘this’ is always a reference to the object of the current class’ type.
•Uses:
1) It prevents hiding of instance variables due to same name given
to the parameters passed in Constructor or Method.
2) Calling the constructor
Array
•An array is contiguous or related data items that share the common name.
•It offers the convenient means of grouping information.
•A position in the array is indicated by a non-negative integer value called as index.
•The size of array is fixed and can not increase to accommodate more elements.
•In Java, arrays are objects.
Vector
•Vector is like the dynamic array which can grow or shrink its size.
•Unlike array, we can store n-number of elements in it as there is no size limit.
•It is a part of Java Collection framework since Java 1.2.
•It is found in the java.util package
Difference between String and StringBuffer
•Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects. The Java platform provides the String class to create and manipulate strings.
•Whereas, StringBuffer class is a thread-safe, mutable sequence of characters.
•A string buffer is like a String, but can be modified.
•It contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
•They are safe for use by multiple threads.
•Every string buffer has a capacity.