Ii-I R21 Oopj Module-2
Ii-I R21 Oopj Module-2
Ii-I R21 Oopj Module-2
MODULE-2
SYLLABUS
To create an array, you first must create an array variable of the
desired type.
That is, to use new to allocate an array, you must specify the type
and number of elements to allocate.
Here, the square brackets follow the type specifier, and not the
name of the array variable.
For example,
ALTERNATIVE ARRAY DECLARATION
The following declarations are also equivalent:
VARIABLE-LENGTH ARGUMENTS
Java has included a feature that simplifies the creation of methods
that need to take a variable number of arguments.
Therefore, just as you can overload methods by using different types of array
parameters, you can overload vararg methods by using different types of
varargs.
In this case, Java uses the type difference to determine which overloaded
method to call.
VARIABLE-LENGTH ARGUMENTS
OVERLOADING VARARG METHODS
The second way to overload a varargs method is to add one or
more normal parameters.
In this case, Java uses both the number of arguments and the
type of the arguments to determine which method to call.
VARIABLE-LENGTH ARGUMENTS
VARARG AND AMBIGUITY
Somewhat unexpected errors can result when overloading a
method that takes a variable-length argument.
The type wrappers are Double, Float, Long, Integer, Short, Byte,
Character, and Boolean.
These classes offer a wide array of methods that allow you to fully
integrate the primitive types into Java’s object hierarchy.
WRAPPER CLASSES
CHARACTER
Character is a wrapper around a char. The constructor for
Character is Character(char ch)
Here, ch specifies the character that will be wrapped by the
Character object being created.
To obtain the char value contained in a Character object, call
charValue( ), shown here: char charValue( )
It returns the encapsulated character.
WRAPPER CLASSES
BOOLEAN
Boolean is a wrapper around boolean values. It defines these
constructors:
Boolean(boolean boolValue)
Boolean(String boolString)
In the first version, boolValue must be either true or false.
In the second version, if boolString contains the string "true" (in
uppercase or lowercase), then the new Boolean object will be true.
Otherwise, it will be false.
WRAPPER CLASSES
BOOLEAN
To obtain a boolean value from a Boolean object, use
booleanValue( ), shown here:
boolean booleanValue( )
For example, the program unboxes the value in iOb with this
statement:
int i=iOB.intValue();
STRINGS
STRING CLASS
String is probably the most commonly used class in Java’s class
library.
The first thing to understand about strings is that every string you
create is actually an object of type String.
Even string constants are actually String objects.
For example, in the statement System.out.println(“This is a
String,too”);
The String “ This is a String,too” is a String Object.
STRING CLASS
The second thing to understand about strings is that objects of type
String are immutable; once a String object is created, its contents
cannot be altered.
An important point to note here is that, while the String object is
immutable, its reference variable is not.
Once you have created a String object, you can use it anywhere that a
string is allowed.
STRING CLASS
Java defines one operator for String objects: +. It is used to
concatenate two strings.
Both hold strings that can be modified after they are created.