Enumerations, Autoboxing and Annotations
- Each expands the power of the language by offering a streamlined approach to handling common programming tasks.
Enumerations:
An enumeration is a list of named constants.
Java offered other features that provide somewhat similar functionality, such as final variables.
Java enumerations appear similar to enumerations in other languages.
Enumeration are also known as enums.
By making enumerations into classes, the capabilities of the enumeration are greatly expanded.
Enumeration Fundamentals:
- It can be identified by,
An enumeration of apple varieties.
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
- The identifiers Jonathan, GoldenDel, and so on, are called enumeration constants.
Each is implicitly declared as a public, static final member of Apple.
Once you have defined an enumeration, you can create a variable of that type.
Even though enumerations define a class type, you do not instantiate an enum using new.
You declare and use an enumeration variable in much the same way as you do one of the primitive types.
The sequence following are,
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.
Winesap, not Apple.Winesap, is used. This is because the type of the enumeration in the switch expression has already implicitly specified the enum type of the case constants.
When an enumeration constant is displayed, such as in a println( ) statement, its name is output.
System.out.println(Apple.Winesap);
- In these Symbol ReDel is preced by Apple.
The Values() And Valuesof() methods:
- All enumerations automatically contained two predefined methods,
1.Value() public staic enum-type value()
2.valueof() public static enum-type valuesOf(String str)
value() It returns a array linked with the enumeration of constants.
valueOf() It returns a string str linked with the enumeration of constants.
Example:
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);
System.out.println();
// use valueOf()
ap = Apple.valueOf("Winesap");
System.out.println("ap contains " + ap);
}
}
In these ap equals to apple and contains winesap.
for(Apple a : Apple.values()) System.out.println(a);
Notice how the value corresponding to the name Winesap was obtained by calling valueOf( ).
ap = Apple.valueOf("Winesap");
As valueOf( ) returns the enumeration value associated with the name of the constant represented as a string.
Java Enumerations class type:
Its Java enumeration are class type.
That available all are used by all enumerations.
You can obtain a value that indicates an enumeration constant and list of constants are called ordinal.And these reterived by calling the ordinal method().
General form:
final int compareTo(enum-type e)
And e is the constant being revoked by compared to invoking constant.
You can compare for equality an enumeration constant with any other object by using equals( ), which overrides the equals() method defined by Object.
Although equals( ) can compare an enumeration constant to any other object, those two objects will be equal only if they both refer to the same constant,within the same enumeration.
Objects can be Equals in enumeration constant if both are in same constant and same enumeration.
Incase they are different in ordinal values will not be equal.
Enumeration Example:
import java.util.Random;
// An enumeration of the possible answers.
enum Answers {
NO, YES, MAYBE, LATER, SOON, NEVER
}
class Question {
Random rand = new Random();
Answers ask() {
int prob = (int) (100 * rand.nextDouble());
if (prob < 15)
return Answers.MAYBE; // 15%
else if (prob < 30)
return Answers.NO; // 15%
else if (prob < 60)
return Answers.YES; // 30%
else if (prob < 75)
return Answers.LATER; // 15%
else if (prob < 98)
return Answers.SOON; // 13%
else
return Answers.NEVER; // 2%
}
}
class AskMe {
static void answer(Answers result) {
switch(result) {
case NO:
System.out.println("No");
break;
case YES:
System.out.println("Yes");
break;
case MAYBE:
System.out.println("Maybe");
break;
case LATER:
System.out.println("Later");
break;
case SOON:
System.out.println("Soon");
break;
case NEVER:
System.out.println("Never");
break;
}
}
public static void main(String args[]) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}
Type wrappers:
The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean.These all the numbers are subclasses of the abstract classes.
These classes provide you a wide array of methods that allow you to fully integrate the primitive types within Java object.
character(ch):
Its constructor is
character(char ch)
character is wrapped under char.
ch determines by character will removed by the character object.
To count the char in the character object use call charvalue().
Then it returns on the encapsulation character.
Boolean(Boolean)
Its constructor is
boolean(boolvalue) boolean(boolString)
First version, boolValue must be either true or false
Second version boolString contains the string "true" (in uppercase or lowercase), then the new Boolean object will be true.
Otherwise, it will be false.
It returns the boolean equivalent of the invoking object.
Numeric Type Wrappers:
- All numeric types inherit the abstract class Number.
- And number declares return value of an object in each different number formats. Methods shown as,
Byte ByteValue()
Int IntValue() Double DoubleValue() Float FloatValue()
Numeric type wrapper define constructor that return the value of an object.
Integer (int num)
Integer (String str)
If str does not contain numeric value and then numberformatexception is thrown.
Then all of the type wrapper override tostring().It returns the human readable form of value contained in the wrapper.
Then the output the value by wrapper into object.
Example:
class Wrap {
public static void main(String args[]) {
Integer iOb = new Integer(100); // boxing
int i = iOb.intValue(); // unboxing
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.
The process of extracting a value from a type wrapper is called unboxing.
Autoboxing:
- Autoboxing is the process by which the primitive type is automatically encapsulated(boxing) into its equivalent type wrapper whenever an object of that type is needed.
- Autounboxing is the process by which the value of boxed object is automatically unboxed(extracted) from a type wrapper whenever an value of that type is needed.
- Both will help in coding the algorithms and helps in removing the tedium manually of boxing and unboxing values.
With autoboxing we can wrap the primitive type automatically with the assign value of type wrappers.
By above program reference
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
Autoboxing and Methods:
- Autoboxing is process of converting the primtive data type into object.
- Autounboxing is process of converting the object into primitive type.
- In these Boxing and unboxing might occur when arguments is passed into methods.
Example:
class autobox{
Static int m(integer v);
return v; //autounbox to int
}
public static void main(String args[]){
integer iob=m(100); //argument is passed to m as100 & return the value
System.out.println(iob);
}
}
Output:
100
Autoboxing and UnAutoboxing In Expressions:
- In this boxing and unboxing occur in conversion into an object or from an object.
- In these expression the numeric value is unboxed from an object and then boxed if necessary.
Example:
class AutoBox3 {
public static void main(String args[]) {
Integer iOb, iOb2;
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);
// The same expression is evaluated, but the
// result is not reboxed.
i = iOb + (iOb / 3);
System.out.println("i after expression: " + i);
}
}
- Auto-unboxing also allows you to mix different types of numeric objects in an expression.
- The standard type of promotions and conversions are applied.
- When the switch expression is evaluated, iOb is unboxed and its int value is obtained.
Autoboxing/Unboxing boolean and Character value:
- Because of auto-unboxing, the boolean value contained within b is automatically unboxed when the conditional expression is evaluated
- When a Boolean is used as the conditional expression of a while, for, do/while, it is automatically unboxed into its boolean equivalent.
boolean b=true; //setting boolean value
character ch=x; //box a char char ch2=ch; //unbox a char if(b) System.out.println() //setting upon the b value by conditional statement
Autoboxing/Unboxing Helps Prevent Errors:
Example:
int i=iob.bytevalue (boxing and unboxing are created with same type of numeric objects)
In these int acts on the byte leads to mismatch leads to error.
In these value inside the iob is manually unboxed by calling the bytevalue().
Rare instances where you want a type different than that produced
by the automated process, you can still manually box and unbox values.It reduces the boxing/unboxing benefits.
A word of warning:
Example:
Double a, b, c;
a = 10.0;
b = 4.0;
c = Math.sqrt(a*a + b*b);
System.out.println("Hypotenuse is " + c);
In these case boxing and unbox creates the user to use double,integer frequently avoid primitive types.
In this user created the code correctly but result in bad use of code in boxing/unboxing.
It will result in less efficient in equivalent code in the primitive type double.
You should restrict your use of the type wrappers to only those cases in which an object representation of a primitive type is required
Annotation:
- Java has a supported feature that enables to implement supplemental information into source file.
- It is called annotation and does not change in the action of program.
Annotations Basics:
- An annotation created by a mechanism is called interface.In that declaration of the annotation is called by MyAnno.
interface MyAnno{
String str();
int value();
}
Two members are val() and str() declare the method annotation and then java implements these method.
All Annotation types are combined with annotation interface.
It is declared within java.lang.annotation package and it overrides on equals(), hascode() and tostring() which are defined by object.
It also specify the annotation type which returns the class object that represents the invoking mechanism.
Any type of declaration can be annotated.Even form,parameter,classes,methods and enum can be annotated.
And incase annotation can be annotated.
Specifying the retention policy:
- A retention policy decide what point the annotation is discarded.
- Java define three policies as Source,class and Runtime.
- Annotation within retention policy of Source is retained only on the source file and discarded during the compilation.
- Annotation within retention policy of class is stored as .class file during compilation(however it is not available in JVM)
- Annotation within retention policy of Runtime is stored as .class file during compilation and available during JVM at runtime.
- The runtime retention is the one of the greatest persistence.
The examples are,
Retention(retention-policy)
Here, retention-policy must be one of the previously discussed enumeration constants.
If no retention policy is specified for an annotation, then the default policy of CLASS is used.
Obtaining Annontations at runtime by use of reflection:
If they specify a retention policy of runtime they can be queried at runtime by any java program through use of reflection.
To obtain annotation:
Class object that represents the class that whose annotations you want to join & obtain.Class is the one of the java builtin classes defined in java.lang
In these they are invoked by using method call syntax.This same approach is used whenever the value of annotation of member is required.
A second Reflection Example:
Obtaining All annotations:
- All annotation that have Runtime retention that associated to an item by calling getAnnotations().
Example:
import java.lang.annotation.*;
import java.lang.reflect.*;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
String str();
int val();
}
@Retention(RetentionPolicy.RUNTIME)
@interface What {
String description();
}
@What(description = "An annotation test class")
@MyAnno(str = "Meta2", val = 99)
class Meta2 {
@What(description = "An annotation test method")
@MyAnno(str = "Testing", val = 100)
public static void myMeth() {
Meta2 ob = new Meta2();
try {Annotation annos[] = ob.getClass().getAnnotations();
// Display all annotations for Meta2.
System.out.println("All annotations for Meta2:");
for(Annotation a : annos)
System.out.println(a);
System.out.println();
// Display all annotations for myMeth.
Method m = ob.getClass( ).getMethod("myMeth");
annos = m.getAnnotations();
System.out.println("All annotations for myMeth:");
for(Annotation a : annos)
System.out.println(a);
} catch (NoSuchMethodException exc) {
System.out.println("Method Not Found.");
}
}
public static void main(String args[]) {
myMeth();
}
}
- In these getannontations() get returns an array of annonation objects.
- Recall that Annotation is a super-interface of all annotation interfaces and that it overrides toString( ) in Object.
- Thus, when a reference to an Annotation is output, its toString( ) method is called to generate a string that describes the annotation, as the preceding output shows.
The Annotated Element Interface:
- the method getAnnotation() and getannotations() used by preceding example defined by the annotated element interface. which defined by java.lang.reflect.
- It returns all the non-inherited annotations present in invoking object.The second is IsAnnotation is present().
General form ,
boolean isAnnotationPresent(class <? extends Annotation> annoType)
It returns true if annotation is specified by annotype associated with the invoking object.
It return false otherwise.
Using default values:
- default values will be assigned in case no values is used in annotation specified.
Example:
// An annotation type declaration that includes defaults.
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno {
String str() default "Testing";
int val() default 9000;
}
This declaration gives a default value of "Testing" to str and 9000 to val.
This means that neither value needs to be specified when @MyAnno is used.
Either or both can be given values if desired.
Marker Annotations:
- A marker annotation is a special kind of annotation that contains no members. Its sole purpose is to mark an item.
Example:
import java.lang.annotation.*;
import java.lang.reflect.*;
// A marker annotation.
@Retention(RetentionPolicy.RUNTIME)
@interface MyMarker { }
class Marker {
// Annotate a method using a marker.
// Notice that no ( ) is needed.
@MyMarker
public static void myMeth() {
Marker ob = new Marker();
try {
Method m = ob.getClass().getMethod("myMeth");
// Determine if the annotation is present.
if(m.isAnnotationPresent(MyMarker.class))
System.out.println("MyMarker is present.");
} catch (NoSuchMethodException exc) {
System.out.println("Method Not Found.");
}
}public static void main(String args[]) {
myMeth();
}
}
Output:
MyMarker is present
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.
- In these single class is used for further referred above program.
Built in Annotations:
java define built in annotations.but these 9 are specialized in it.
These are imported from Java.lang.annontation
@Retention,@Documented,@Targeted and @Inherited
And these five are imported from java.lang
@Override,@Deprecated,@Functional Interface,@Safe Wings and@suppress Wings.
@Retention:
- @Retention is designed to be used only as an annotation to another annotation.
- Annotation within retention policy of Source is retained only on the source file and discarded during the compilation.
- Annotation within retention policy of class is stored as .class file during compilation(however it is not available in JVM)
Annotation within retention policy of Runtime is stored as .class file during compilation and available during JVM at runtime.
The runtime retention is the one of the greatest persistence.
@Documented:
- It is a marker interface that tells a tool that an annotation is to be documented.
- It is designed to be used only as an annotation to an annotation declaration.
@Targeted:
- The @Target annotation specifies the types of items to which an annotation can be applied.It can be used only as an annotation of another annotation.
- @Target takes one argument, which is an array of constants of the ElementType enumeration.
- This argument specifies the types of declarations to which the annotation can be applied.
Example:
TYPE ---> Class, interface, or enumeration
ANNOTATION_TYPE ---> Another annotation
CONSTRUCTOR ---> Constructor
@Inherited:
- @Inherited is a marker annotation that can be used only on another annotation declaration.
- It affects only annotations that will be used on class declarations.
Example:
@Documented
@Retention(value=RUNTIME)
@Targeted(value=Annotation_Type)
public@Interface inherited
@Override:
- @Override is a marker annotation that can be used only on methods
- It is used to ensure that a superclass method is actually overridden, and not simply overloaded.
- A method with @override overload the method with superclass.
@Deprecated:
- It is an marker annotation and it is in obsolete declaration is no longer used and changed into new form.
@Functional Interface:
- It is an marker annotation added by JDK8 and used as an interface.
- Hence functional interface can have one and only one abstract class. And it is used by lambada expressions.
- If its @functional interface is not in the functional interface in annotation its result in compilation error.
- Any interface with one abstract class is known as functional interface.
@SafeVarags:
- Safevarargs are marker annotations that can be applied to methods and constructors.
- It used to suppress the unchecked warnings and safecode relates to non-refifiable vararg type are parameterized array instantiation
@SuppressWarnings:
- The one or more warnings are suppressed in compiler.
- It suppress are int and String form.
Annotation --> Target
@TypeAnno --> ElementType.TYPE_USE
@MaxLen --> ElementType.TYPE_USE
@NotZeroLen --> ElementType.TYPE_USE
@Unique --> ElementType.TYPE_USE
@What --> ElementType.TYPE_PARAMETER
@EmptyOK --> ElementType.FIELD
@Recommended --> ElementType.METHOD
Repeating Annotation:
- The annotation are repeated on the same element type.It uses the @Repeatable annotation defined in java.lang.annotation.
- Its value field determine the container type for the repeatable annotation.To create an container annotation type specify an argument in @Repeatable Annotation.
- In these repeated annotations by calling getAnnotation() passing in the class of container annotation not in nonrepeatable Annotation.
- <T extends Annotation> T[ ] getAnnotationsByType(Class<T> annoType)
- It return the array of annotations of annotype with the invoking object .
- Incase if there is no annotations are present then the array will be in zero length.
Some Restrictions:
There are a number of restrictions that apply to annotation declarations.
First no annotation can inherit another.
Second all methods declared by an annotation must be without parameters
Hereafter it return following,
A primitive type, such as int or double.
An object of type String or Class
An enum type.
Another annotation type.
An array of one of the preceding types.
Annotations method cannot specify throws clause.