Advanced Java J2EE Vtu Notes 17CS553
Advanced Java J2EE Vtu Notes 17CS553
MODULE 1
Enumerations:
Versions of Java prior to JDK 5 lacked few features.
Three relatively recent additions to the Java language after JDK 5: enumerations,
autoboxing, and annotations (also referred to as metadata)
• An enumeration is a list of named constants
• Java enumerations appear similar to enumerations in other languages
• 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
Some examples:
– dayOfWeek: SUNDAY, MONDAY, TUESDAY, …
– month: JAN, FEB, MAR, APR, …
– gender: MALE, FEMALE
– title: MR, MRS, MS, DR
– appletState: READY, RUNNING, BLOCKED, DEAD
In the past, enumerations were usually represented as integer values:
public final int SPRING = 0;
public final int SUMMER = 1;
public final int FALL = 2;
public final int WINTER = 3;
Now
enum Season { WINTER, SPRING, SUMMER, FALL }
1
However, even though enumerations define a class type, you do not instantiate an enum
using new.
• Declare and use an enumeration variable
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;
Usage:
Two enumeration constants can be compared for equality by using the = = relational
operator.
if(ap == Apple.GoldenDel)
Use an enum to control a switch statement.
switch(ap)
{
case Jonathan:
case Winesap:
}
The values( ) and valueOf( ) Methods:
All enumerations automatically contain two predefined methods:
values( ) and valueOf( ).
public static enum-type[ ] values( )
The values( ) method returns an array that contains a list of the enumeration constants
public static enum-type valueOf(String str)
The valueOf( ) method returns the enumeration constant whose value corresponds to the
string passed in str.
Sample Program:
2
3
Java Enumerations Are Class Types:
• Don’t instantiate an enum using new,
• Has much the same capabilities as other classes.
• The fact that enum defines a class gives the Java enumeration extraordinary
power.
• For example, you can give them constructors, add instance variables and
methods, and even implement interfaces.
• It is important to understand that each enumeration constant is an object of its
enumeration type.
• Thus, when you define a constructor for an enum, the constructor is called when
each enumeration constant is created.
• Also, each enumeration constant has its own copy of any instance variables
defined by the enumeration.
An Example –
4
• The arguments to the constructor are specified, by putting them inside parentheses
after each constant, as shown here:
Jonathan(10), GoldenDel(9), RedDel(12), Winesap(15), Cortland(8);
• These values are passed to the p parameter of Apple( ), which then assigns this
value to price.
• Because each enumeration constant has its own copy of price, you can obtain the
price of a specified type of apple by calling getPrice( )
Apple.Winesap.getPrice( )
- The preceding example contains only one constructor.
• An enum can offer two or more overloaded forms, just as can any other class.
5
Enumerations Inherit Enum:
• All enumerations automatically inherit one class: java.lang.Enum.
• String toString() returns the name of this enum constant, as contained in the
declaration
• boolean equals(Object other) returns true if the specified object is equal to this
enum constant
• int compareTo(E o) compares this enum with the specified object for order;
returns a negative integer, zero, or a positive integer as this object is less than,
equal to, or greater than the specified object
• static enum-type valueOf(String s) returns the enumerated object whose name is s
• static enum-type[] values() returns an array of the enumeration objects
• final int ordinal( ) obtain a value that indicates an enumeration constant’s position
in the list of constants. This is called its ordinal value.
Using ordinal method:
6
num1 38
• Note that a primitive variable contains the value itself, but an object variable
contains the address of the object, that is, a ‘reference’ to the object.
• An object reference can be thought of as a pointer to the location of the object.
Rather than dealing with arbitrary addresses, we often depict a reference
graphically.
• Java uses primitive types (also called simple types), to hold the basic data types
supported by the language.
• 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.
• The primitive types are not part of the object hierarchy, and they do not inherit
Object.
Wrapper Classes:
• Java provides type wrappers, which are classes that encapsulate a primitive type
within an object
• The java.lang package contains wrapper classes that correspond to each primitive
type.
7
• Wrapper classes also contain static methods that help manage the associated type.
For example, the Integer class contains a method to convert an integer stored in a String
to an int value:
int num;
num = Integer.parseInt(str);
Or
float fltnum;
fltnum = Float.parseFloat(str);
Character:
Character(char ch)
Here, ch specifies the character that will be wrapped by the Character object being
created.
• Beginning with JDK 9, the Character constructor has been deprecated and
recommended to use the static method valueOf()to obtain a Character object.
static Character valueOf(char ch)
Boolean:
8
Here, boolValue must be either true or false
Boolean(String boolString)
Here, if boolString contains the string "true" (in uppercase or lowercase), then the new
Boolean object will be true. Otherwise, it will be false.
• Beginning with JDK 9, the Boolean constructors have been deprecated and
recommended to use the static method valueOf()to obtain a Boolean object.
boolean booleanValue()
• The most commonly used type wrappers that represent numeric values.
• All of the numeric type wrappers inherit the abstract class Number.
• Number declares methods that return the value of an object in each of the
different number formats.
9
Example: constructors defined for Integer:
Integer(int num)
Integer(String str)
Note: If str does not contain a valid numeric value, then a NumberFormatException is
thrown.
• The valueOf() method is a static member of all of the numeric wrapper classes.
• All numeric classes support forms that convert a numeric value or a string into an
object.
• All of the type wrappers override toString(),It returns the human-readable form of
the value contained within the wrapper.
• This allows you to output the value by passing a type wrapper object to println( ),
for example, without having to convert it into its primitive type.
10
Program demonstrates how to use a numeric type wrapper to encapsulate a value and then
extract that value.
Output:
100 100
Summary:
• The process of encapsulating a value within an object is called boxing
• The process of extracting a value from a type wrapper is called unboxing
• The same general procedure used by the preceding program to box and unbox
values has been available for use since the original version of Java.
• However, today, Java provides a more streamlined approach – Auto boxing &
Auto unboxing
11
• For example – conversion of int to Integer, long to Long, double to Double etc.
Advantages:
• Autoboxing and auto-unboxing greatly streamline the coding of several
algorithms, removing the tedium of manually boxing and unboxing values.
• They also help prevent errors.
• Moreover, they are very important to generics, which operate only on objects.
• Finally, autoboxing makes working with the Collections Framework much
easier.
Example:
Integer iOb = 100; // autobox an int
• With autoboxing, it is not 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.
iob 100
Notice that the object is not explicitly boxed. Java handles this for you, automatically
i 100
12
Output:
Displays 100 100
13
Autoboxing/Unboxing in Expressions:
• Within an expression, a numeric object is automatically unboxed.
• The outcome of the expression is reboxed, if necessary.
Integer iob1, iob2;
int i;
iob1=100;
++iob1;
iob2= iob1 + (iob1 /3);
i= iob1 + (iob1 / 3);
Output:
14
• Once the values are unboxed, the standard type promotions and conversions are
applied.
Integer iob = 100;
Double dob = 12.6;
dob = iob + dob;
Output:
15
Output:
16
A Word of Warning:
• Restrict use of the type wrappers to only those cases in which an object
representation of a primitive type is required.
• Each autobox and auto-unbox adds overhead that is not present if the primitive
type is used.
Summary:
• Java provides a more streamlined approach – Auto boxing & Auto unboxing -
Beginning with JDK 5.
• Autoboxing and auto-unboxing greatly streamline the coding of several
algorithms, removing the tedium of manually boxing and unboxing values.
• Makes easier to work with Generics, Collection Framework.
Quiz:
Is it valid?
int sum(int a, int b)
{
return a + b;
}
Integer a = sum(new Integer(1), 2);
Answer: YES
Object Identity(Object x)
{
return x;
}
double c = Identity(new Double(1.0));
17
Annotations (Metadata):
• Since JDK 5, Java has supported a feature that enables you to embed
supplemental information into a source file.
• This information, called an annotation, does not change the actions of a program.
• This information can be used by various tools during both development and
deployment.
• The term metadata is also used to refer to this feature, but the term annotation is
the most descriptive and more commonly used.
– @ that precedes the keyword interface - This tells the compiler that an annotation
type is being declared.
– There are two members str( ) and val( ).
– All annotations consist solely of method declarations.
– Does not contain method implementation.
– Java implements these methods.
– The methods act much like fields
– An annotation cannot include an extends clause
Usage – Foreword:
• Any type of declaration can have an annotation associated with it.
• For example, classes, methods, fields, parameters, and enum constants can be
annotated.
• Even an annotation can be annotated.
• In all cases, the annotation precedes the rest of the declaration.
• When you apply an annotation, you give values to its members
Example- Usage
MyAnno being applied to a method declaration.
// Annotate a method.
18
@MyAnno(str = "Annotation Example", val = 100)
public static void myMeth()
{
…. }
Retention Policy:
• SOURCE is retained only in the source file and is discarded during compilation.
• CLASS is stored in the .class file during compilation. It is not available through
the JVM during run time.
• RUNTIME is stored in the .class file during compilation and is available through
the JVM during run time.
19
• Class is one of Java’s built-in classes and is defined in java.lang.
• One of the easiest is to call getClass( ), which is a method defined by Object final
Class<?> getClass( )
It returns the Class object that represents the invoking object
<?> - This is related to Java’s generics feature
• If you want to obtain the annotations associated with a specific item declared
within a class,
• First obtain an object that represents that item.
• For example, Class supplies (among others) the getMethod( ) - return objects of
type Method
• getField( ) - return objects of type Field
• getConstructor( ) – return objects of type Constructor
Example:
An example that obtains the annotations associated with a method
• First obtain a Class object that represents the class
• Then call getMethod( ) on that Class object, specifying the name of the method
• getMethod( ) has this general form:
Method getMethod(String methName, Class<?> ... paramTypes)
1. The name of the method is passed in methName.
2. If the method has arguments, then Class objects representing those types must
also be specified by paramTypes
3. If the method can’t be found, NoSuchMethodException is thrown.
getAnnotation( )
• From a Class, Method, Field, or Constructor object, you can obtain a specific
annotation associated with that object by calling getAnnotation( ).
General form :
<A extends Annotation> getAnnotation(Class<A> annoType)
annoType is a Class object that represents the annotation in which you are interested.
The method returns a reference to the annotation.
Using this reference, you can obtain the values associated with the annotation’s members.
The method returns null if the annotation is not found, which will be the case if the
annotation does not have RUNTIME retention.
20
• In the preceding example, myMeth( ) has no parameters. Thus, when getMethod(
) was called, only the name myMeth was passed.
• To obtain a method that has parameters, you must specify class objects
representing the types of those parameters as arguments to getMethod( ).
21
Output:
The output from this version is shown here:
Two Parameters 19
myMeth( ) takes a String and an int parameter.
To obtain information about this method,
getMethod( ) must be called as shown here:
Method m = c.getMethod("myMeth", String.class, int.class);
Here, the Class objects representing String and int are passed as additional arguments.
22
• Class is one of Java’s built-in classes and is defined in java.lang.
• One of the easiest is to call getClass( ), which is a method defined by Object
final Class<?> getClass( )
It returns the Class object that represents the invoking object
<?> - This is related to Java’s generics feature
• If you want to obtain the annotations associated with a specific item declared
within a class,
• First obtain an object that represents that item.
• For example, Class supplies (among others) the getMethod( ) - return objects of
type Method
• getField( ) - return objects of type Field
• getConstructor( ) – return objects of type Constructor
Example:
An example that obtains the annotations associated with a method
• First obtain a Class object that represents the class
• Then call getMethod( ) on that Class object, specifying the name of the method
• getMethod( ) has this general form:
Method getMethod(String methName, Class<?> ... paramTypes)
1. The name of the method is passed in methName.
2. If the method has arguments, then Class objects representing those types must
also be specified by paramTypes
If the method can’t be found, NoSuchMethodException is thrown.
getAnnotation( )
• From a Class, Method, Field, or Constructor object, you can obtain a specific
annotation associated with that object by calling getAnnotation( ).
General form :
<A extends Annotation> getAnnotation(Class<A> annoType)
annoType is a Class object that represents the annotation in which you are interested.
The method returns a reference to the annotation.
Using this reference, you can obtain the values associated with the annotation’s members.
The method returns null if the annotation is not found, which will be the case if the
annotation does not have RUNTIME retention.
23
Output:
The output from this version is shown here:
Two Parameters 19
myMeth( ) takes a String and an int parameter.
To obtain information about this method,
getMethod( ) must be called as shown here:
Method m = c.getMethod("myMeth", String.class, int.class);
Here, the Class objects representing String and int are passed as additional arguments.
24
Obtaining All Annotations:
• You can obtain all annotations that have RUNTIME retention that are associated
with an item by calling getAnnotations( ) on that item.
Annotation[ ] getAnnotations( )
• It returns an array of the annotations.
• getAnnotations( ) can be called on objects of type Class, Method, Constructor,
and Field, among others
Example:
25
The AnnotatedElement Interface:
• The methods getAnnotation( ) and getAnnotations( ) are defined by the
AnnotatedElement interface, which is defined in java.lang.reflect
• AnnotatedElement defines several other methods.
• Two have been available since JDK 5. The first is getDeclaredAnnotations( ),
which has this general form:
Annotation[ ] getDeclaredAnnotations( )
It returns all non-inherited annotations present in the invoking object.
• The second is isAnnotationPresent( ), which has this general form:
boolean isAnnotationPresent(Class<? extends Annotation> annoType)
• It returns true if the annotation specified by annoType is associated with the
invoking object. It returns false otherwise.
26
int val() default 9000;
}
Marker Annotations:
• A marker annotation is a special kind of annotation that contains no members.
• Its sole purpose is to mark an item.
• Thus, its presence as an annotation is sufficient.
• The best way to determine if a marker annotation is present is to use the method
isAnnotationPresent( ), which is defined by the AnnotatedElement interface.
• Because a marker interface contains no members, simply determining whether it
is present or absent is sufficient.
27
Single-Member Annotations:
• A single-member annotation contains only one member.
• It works like a normal annotation except that it allows a shorthand form of
specifying the value of the member.
• When only one member is present, you can simply specify the value for that
member when the annotation is applied—you don’t need to specify the name of
the member.
• However, in order to use this shorthand, the name of the member must be value.
Output:
• As expected, this program displays the value 100. In the program, @MySingle is
used to annotate myMeth( ), as shown here:
@MySingle(100)
• Notice that value = need not be specified.
• You can use the single-value syntax when applying an annotation that has other
members, but those other members must all have default values.
For example, here the value xyz is added, with a default value of zero:
@interface SomeAnno
28
{
int value();
int xyz() default 0;
}
Single-Member Annotations:
• In cases in which you want to use the default for xyz, you can apply
@SomeAnno, as shown next, by simply specifying the value of value by using the
single-member syntax.
• @SomeAnno(88)
• In this case, xyz defaults to zero, and value gets the value 88. Of course, to
specify a different value for xyz requires that both members be explicitly named,
as shown here:
• @SomeAnno(value = 88, xyz = 99)
• Remember, whenever you are using a single-member annotation, the name of that
member must be value.
29
• You can specify one or more of these values in a @Target annotation. To specify
multiple values, you must specify them within a braces-delimited list. For
example, to specify that an annotation applies only to fields and local variables,
you can use this @Target annotation:
@Target( { ElementType.FIELD,ElementType.LOCAL_VARIABLE } )
• @Inherited - causes the annotation for a superclass to be inherited by a subclass
• @Override - annotation that can be used only on methods. A method annotated
with @Override must override a method from a superclass. If it doesn’t, a
compile-time error will result.
• @Deprecated - It indicates that a declaration is obsolete and has been replaced by
a newer form.
• @FunctionalInterface - marker annotation added by JDK 8 and designed for use
on interfaces. It indicates that the annotated interface is a functional interface. A
functional interface is an interface that contains one and only one abstract method.
Functional interfaces are used by lambda expressions.
• @SafeVarargs -is a marker annotation that can be applied to methods and
constructors. It indicates that no unsafe actions related to a varargs parameter
occur
• @SuppressWarnings @SuppressWarnings specifies that one or more warnings
that might be issued by the compiler are to be suppressed. The warnings to
suppress are specified by name, in string form.
Acknowledgment:
References:
Herbert Schildt: JAVA the Complete Reference, 7th/9th Edition, Tata McGraw
Hill, 2007.
Web References:
https://docs.oracle.com/
https://www.geeksforgeeks.org/
-For both content creation and delivery.
30