0% found this document useful (0 votes)
10 views

Module5opp Part2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module5opp Part2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

OBJECT ORIENTED

PROGRAMMING WITH JAVA


Enumeration , Type
wrappers and Autoboxing
Department of Computer Science & Engineering

www.cambridge.edu
.in
Enumerations
• an enumeration is a list of named constants.
• Java enumerations appear similar to enumerations in other
languages. However, this similarity may be only skin deep
because, in Java, an enumeration defines a class type.
• By making enumerations into classes, the capabilities of the
enumeration are greatly expanded. For example, in Java, an
enumeration can have constructors, methods, and instance
variables. Therefore, although enumerations were several years
in the making, Java’s rich implementation made them well
worth the wait.
Enumerations…….
• An enumeration is created using the enum keyword

• For example, here is a simple enumeration that lists various


apple varieties:
// An enumeration of apple varieties.
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
• The identifiers Jonathan, GoldenDel, and so on, are
called enumeration constants.
• implicitly declared as a public, static final member of Apple.
• their type is the type of the enumeration in which they are declared,
which is Apple in this case.
• Thus, in the language of Java, these constants are called self-typed, in
which “self” refers to the enclosing enumeration.
• If you have defined an enumeration, you can create a variable of that
type.
• you do not instantiate an enum using new.
• Instead, you declare and use an enumeration variable in much the
same way as you do one of the primitive types. For example, this
declares ap as a variable of enumeration type Apple:
Apple ap;
• Because ap is of type Apple, the only values that it can be assigned
(or can contain) are those defined by the enumeration.
• For example, this assigns ap the value RedDel:
ap = Apple.RedDel;
*******Notice that the symbol RedDel is preceded by Apple.
• Two enumeration constants can be compared for equality by using the
= = relational operator. For example, this statement compares the
value in ap with the GoldenDel constant:
if(ap == Apple.GoldenDel)
• An enumeration value can also be used to control a switch statement.
• Of course, all of the case statements must use constants from the
same enum as that used by the switch expression.
• For example, this switch is perfectly valid:
// Use an enum to control a switch statement.
switch(ap) {
case Jonathan:
...
case Winesap:
...
//Use an enum to control a switch statement.
switch(ap) {
// An enumeration of apple varieties. case Jonathan:
enum Apple { System.out.println("Jonathan is red.");
Jonathan, GoldenDel, RedDel, Winesap, Cortland break;
} case GoldenDel:
class EnumDemo { System.out.println("Golden Delicious is yellow.");
public static void main(String args[]) break;
{ case RedDel:
Apple ap; System.out.println("Red Delicious is red.");
ap = Apple.RedDel; break;
// Output an enum value. case Winesap: System.out.println("Winesap is red.");
System.out.println("Value of ap: " + ap); break;
System.out.println(); case Cortland: System.out.println("Cortland is red.");
ap = Apple.GoldenDel; break;
//Compare two enum values. }
if(ap == Apple.GoldenDel) } Value of ap: RedDel
System.out.println("ap contains GoldenDel.\n"); }
ap contains GoldenDel.
Golden Delicious is yellow.
The values( ) and valueOf( ) Methods
public static enum-type [ ] values( )
public static enum-type valueOf(String str )
• The values( ) method returns an array that contains a list of the
enumeration constants.
• The valueOf( ) method returns the enumeration constant whose value
corresponds to the string passed in str.
• In both cases, enum-type is the type of the enumeration.
• For example, in the case of the Apple enumeration shown earlier, the
return type of Apple.valueOf("Winesap") is Winesap.
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
class EnumDemo2 {
public static void main(String args[])
{
Apple ap;
System.out.println("Here are all Apple constants:");
// use values()
Apple allapples[] = Apple.values();
for(Apple a : allapples)
System.out.println(a); Here are all Apple constants:
System.out.println(); Jonathan
// use valueOf()
ap = Apple.valueOf("Winesap");
GoldenDel
System.out.println("ap contains " + ap); RedDel
} Winesap
}
Cortland
ap contains Winesap
Type Wrappers
• Primitive types, rather than objects, are used for these quantities for the
sake of performance.
• Using objects for these values would add an unacceptable overhead to
even the simplest of calculations.
• Thus, the primitive types are not part of the object hierarchy, and they
do not inherit Object.
• For example, you can’t pass a primitive type by reference to a method.
• standard data structures implemented by Java operate on objects, which
means that you can’t use these data structures to store primitive types.
• The type wrappers
are Double, Float, Long, Integer, Short, Byte, Character, and
Boolean.
Character
• Character is a wrapper around a char. The constructor for Character
• 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.
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.
• To obtain a boolean value from a Boolean object, use booleanValue( ), shown
here: boolean booleanValue( )
The Numeric Type Wrappers
• These are Byte, Short, Integer, Long, Float, and Double.
• byte byteValue( )
• double doubleValue( )
• float floatValue( )
• int intValue( )
• long longValue( )
• short shortValue( )
class Wrap {
public static void main(String args[]) {
Integer iOb = new Integer(100);
int i = iOb.intValue();
System.out.println(i + " " + iOb); // displays 100 100
}
}
This program wraps the integer value 100 inside an Integer object called iOb.
The program then obtains this value by calling intValue( ) and stores the result
in i.
• The process of encapsulating a value within an
object is called boxing. Thus, in the program,
this line boxes the value 100 into an Integer:
Integer iOb = new Integer(100);
•The process of extracting a value from a type
wrapper is called unboxing. For example, the
program unboxes the value in iOb with this
statement:
int i = iOb.intValue();
public static void main(String args[]) {
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
With autoboxing, it is no longer necessary to manually construct an object in
order to wrap a primitive type. You need only assign that value to a
type-wrapper reference. Java automatically constructs the object for you. For
example, here is the modern way to construct an Integer object that has the
value 100:
To unbox an object, simply assign that object reference to a primitive-type
variable. For example, to unbox iOb, you can use this line:
Autoboxing and Methods

Autoboxing/unboxing takes place with method parameters and return values.


class AutoBox2 {
Take an Integer parameter and return an int value;
static int m(Integer v) {
return v ; // auto-unbox to int
}
public static void main(String args[]) {
Pass an int to m() and assign the return value to an Integer. Here, the argument 100 is autoboxed into an
Integer.
The return value is also autoboxed into an Integer.
Integer iOb = m(100);
System.out.println(iOb);
}
}
In the program, notice that m( ) specifies an Integer parameter and returns
an int result. Inside main( ), m( ) is passed the value 100. Because m( ) is expecting
an Integer, this value is automatically boxed. Then, m( ) returns the int equivalent of
its argument. This causes v to be auto-unboxed. Next, this int value is assigned
to iOb in main( ), which causes the int return value to be autoboxed.
Autoboxing/Unboxing Occurs in Expressions

// Autoboxing/unboxing occurs inside expressions. This causes the value in iOb to be


class AutoBox3 { incremented. It works like this: iOb is
public static void main(String args[]) { unboxed, the value is incremented, and
Integer iOb, iOb2; the result is reboxed.
int i;
iOb = 100;
System.out.println("Original value of iOb: " + iOb);
//The following automatically unboxes iOb, performs the increment, and then reboxes the result back into iOb.
++iOb;
System.out.println("After ++iOb: " + iOb);
Here, iOb is unboxed, the expression is evaluated, and the result is reboxed and assigned to iOb2.
iOb2 = iOb + (iOb / 3);
System.out.println("iOb2 after expression: " + iOb2);
he same expression is evaluated, but the result is not reboxed.
i = iOb + (iOb / 3); Original value of iOb: 100 After ++iOb: 101
System.out.println("i after expression: " + i); iOb2 after expression: 134 i after
} expression: 134
}

You might also like