05 VectorAndWrapper
05 VectorAndWrapper
The package java.util contains a library of Java’s utility classes. One of them is Vector class.
It implements a dynamic array which can hold the array of any type and any number.
These objects do not have to be homogenous.
Vector size can be grows autometically when we add the new element and shrink when we remove an element
from it.
After this initial capacity is reached, and when we attempt to store an object in the vector, the vector automatically
allocates space for that object (increases capacity) plus extra room for additional objects.
The amount of extra space allocated during each reallocation is determined by the increment that we specify
when we create the vector. If we don’t specify an increment, the vector’s size is doubled by each allocation cycle.
The Vector class constructors.
o Vector() //Create Vector with initial capacity 10
o Vector( int size ) // Create Vector with initial capacity specified by given size
o Vector ( int size , int incr ) // Create Vector with initial capacity specified by given size and The increment
specifies the number of elements to allocate each time when new elements are added in the vector.
Methods Of Vector Class (Consider v is a object of Vector Class ):
Method Prototype Description
void addElement(Object element) The object specified by element is added to the vector.
boolean removeElement(Object element) Removes element from the vector. If more than one
instance of the specifies object exists in the vector, then
it is the first one that is removed. It returns true if
successful and false if the object is not found
Object elementAt(int index) Returns the element at the location specified by index.
void insertElementAt(Object element, int Adds element to the vector at the location specified by
the index.
index)
int capacity() It returns the capacity of the vector
boolean contains(Object ele) It returns true if ‘element’ is contained by the vector, and
returns false if it is not.
void copyInto(Object array[]) The elements contained in the invoking vector are
copied into the array specified by ‘array’.
void ensureCapacity(int size) This method sets the minimum capacity of the vector to
‘size’.
Object firstElement( ) It returns the first element in the vector.
It returns the last element in the vector
Object lastElement()
int indexOf(Object element) It returns the index of the first occurrence of ‘element’. If
int indexOf(Object element, int start) the object is not found in the vector, –1 is returned.
void insertElementAt(Object element, int It adds ‘element’ to the vector at the location specified by
index) ‘index’.
WRAPPER CLASSES
The primitive data types of Java such as int, char, float are not the part of any object hierarchy. They are always passed
by value to the method not by reference. Also, there is no way for two different methods to refer same instance of a data
type.
For such purpose, it is necessary to create object representation of primitive data types. These object
representations of primitive data types are called as wrapper classes.
In essence, these classes encapsulate or wrap the primitive data types within the class.
All the wrapper classes are defined in package java.lang.
Table shows the wrapper classes of respective data types.
Data Type Boolean char Double float int long short byte
Wrapper Class Boolean Character Double Float Integer Long Short Byte
Method Description
int compareTo(Byte obj) It compares the numerical value of the invoking object with that of ‘obj’. It
Returns 0 if the values are equal. -ve value if the invoking object has a
lower value. +ve value if the invoking object has a greater value.
int compareTo(Object Byteobj) Operates identically to compareTo(Byte) if ‘obj’ is of class Byte.
throws ClassCastException Otherwise, throws a ClassCastException.
boolean equals(Object ByteObj) It returns true if the invoking Byte object is equivalent to ‘ByteObj’.
Otherwise, it returns false.
int hashCode() This method returns the hash code for the invoking object.
static byte parseByte(String str) It returns the byte equivalent of the number contained in the string
throws NumberFormatException specified by ‘str’ using radix 10. It throws NumberFormatException if
number format of ‘str’ is not matched with byte constant.
static Byte valueOf(String str) It returns the Byte object that contains the value specified by the string
throws NumberFormatException in ‘str’. Otherwise throws NumberFormatException
static String toOctalString(int num) It returns a string that contains the octal equivalent of ‘num’.
static boolean isLowerCase(char ch) It returns true if ‘ch’ is a lowercase letter. Otherwise, it returns false.
static boolean isSpaceChar(char ch) It returns true if ‘ch’ is a Unicode space character. Otherwise, it returns false.
static boolean isTitleCase(char ch) It returns true if ‘ch’ is a Unicode title-case character. Otherwise, it returns false.
static boolean isUpperCase(char ch) It returns true if ‘ch’ is an uppercase letter. Otherwise, it returns false.
static boolean isWhitespace(char ch) It returns true if ‘ch’ is whitespace. Otherwise, it returns false.
static char toLowerCase(char ch) It returns lowercase equivalent of ‘ch’.
static char toTitleCase(char ch) It returns titlecase equivalent of ‘ch’.
static char toUpperCase(char ch) It returns uppercase equivalent of ‘ch’.
Boolean
This wrapper class contains the constants TRUE & FALSE, which define true and false Boolean objects.
Methods of Boolean Wrapper Class
Method Description
boolean booleanValue( ) It returns boolean equivalent value of the invoking object.
boolean equals(Object boolObj) It returns true if the invoking object is equivalent to ‘boolObj’.
Otherwise, it returns false.
`int hashCode( ) It returns the hash code for the invoking object.
Subject: Java Programming (Vector & Wrapper) Page 5 of 7
String toString( ) This method returns the string equivalent of the invoking object.
static String toString(boolean boolVal) It returns the string equivalent of ‘boolVal’.
static Boolean valueOf(boolean It returns the Boolean equivalent of ‘boolVal’.
boolVal)
static Boolean valueOf(String It returns true if ‘boolString’ contains the string “true” (in
boolString) uppercase or lowercase). Otherwise, it returns false
Wrapper Class:
Objects like vector cannot handle primitive data types like int, float, long char and double.
Wrapper classes are used to convert primitive data types into object types.
Wrapper classes are contained in the java.lang package.
The some of the wrapper classes are: Boolean, Integer, Character, Float etc.
To convert integer number to string. Method is toString()
o String str = Integer.toString(i) –converts the integer i to string value.
To convert numeric string to integer number: Method is parseInt()
o int i = Integer.parseInt(str) – converts the string value of numeric string str to int i.
FileName : TestDay.java
class TestDay {
public static void main(String args[]) {
Day day = Day.MONDAY; //Day Refance created and assign a constant value.
System.out.println(day.getDayNo());
}
}
Here, day is referance variable of Enum Day and it contains constant value of Day.MONDAY