1.what Is Enum? Explain The Use of Values and Valueof Methods? Enumerations
1.what Is Enum? Explain The Use of Values and Valueof Methods? Enumerations
Enumerations
An enum is a list of named constants. In Java, enumerations define class types. That is, in Java,
enumerations can have constructors, methods and variables. An enumeration is created using the
keyword enum. Following is an example:
enum Person
{
Married, Unmarried, Divorced, Widowed
}
The identifiers like Married, Unmarried etc. are called as enumeration Constants. Each such
constant is implicitly considered as a public static final member of Person.
}
Class EnumDemo{
Person p1;
p1=Person.Unmarried;
Else
case Married:
System.out.println("p1 is Married");
break;
case Unmarried:
System.out.println("p1 is Unmarried");
break;
case Divorced:
System.out.println("p1 is Divorced");
break;
case Widowed:
System.out.println("p1 is Widowed");
break;
}
enum Apple{
janarthan,goldendel,redel,winesap,cortland
}
Apple allapples[]=Apple.values();
for(Apple a : allapples)
System.out.println(a);
System.out.println();
ap = Apple.valueOf("Winesap");
System.out.println("ap contains"+ap);
}
}
package sem3;
enum Apple{
janarthan,goldendel,redel,winesap,cortland
}
public class EnumDemo1 {
}
3. What is autoboxing? Explain its methods?
class AutoBox2 {
Take an Integer parameter and return an int value;
//
into an Integer.
}
This program displays the following result:
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.
4. Explain how autoboxing and unboxing occurs in Boolean and
Character values?
Java also supplies wrappers for boolean and char. These
are Boolean and Character. Autoboxing/unboxing applies to these wrappers,
too. For example, consider the following program:
package sem3;
public class p6 {
}
The output is shown here:
b is true
ch2 is x
The most important thing to notice about this program is the auto-unboxing
of b inside the if conditional expression. As you should recall, the conditional
expression that controls an if must evaluate to type boolean. Because of auto-
unboxing, the boolean value contained within b is automatically unboxed when
the conditional expression is evaluated. Thus, with the advent of
autoboxing/unboxing, a Boolean object can be used to control an if statement.
Because of auto-unboxing, a Boolean object can now also be used to control any
of Java’s loop statements. When a Boolean is used as the conditional expression
of a while, for, or do/while, it is automatically unboxed into
its boolean equivalent. For example, this is now perfectly valid code:
Boolean b;
// ...
while(b) { // ...
• Are different from comments since they can affect how the program is
treated by the compiler.
how can we create our own java annotations, for that refer to simple
steps sequentially as follows:
String str();
int val();
First, notice the @ that precedes the keyword interface. This tells the compiler
that an annotation type is being declared. Next, notice the two members str(
) and val( ).
Example:
import java.io.*;
@interface books_data
// Main class
class books {
// Print statement
@Override
@Override is a marker 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. It is used to ensure
that a superclass method is actually overridden, and not simply overloaded.
1. class Animal{
2. void eatSomething(){System.out.println("eating something");}
3. }
4.
5. class Dog extends Animal{
6. @Override
7. void eatsomething(){System.out.println("eating foods");}//should be eatSomet
hing
8. }
9.
10. class TestAnnotation1{
11. public static void main(String args[]){
12. Animal a=new Dog();
13. a.eatSomething();
14. }}
@SuppressWarnings
package sem3;
import java.util.*;
class Addition
{
public static int sum(int n1,int n2)
{
return n1+n2;
}
public static int sum(int...nums)
{
int sum=0;
for(int i:nums)
{
sum+=i;
}
return sum;
}
}
public class Annotation2 {
@SuppressWarnings("unchecked")
public static void main(String args[])
{
Addition add=new Addition();
int sum=Addition.sum(10, 20);
System.out.println("sum of 10 and 20:"+sum);
@SuppressWarnings("rawtypes")
List list=new ArrayList();
list.add(12);
list.add(120);
System.out.println("list items:"+list);
}
}
If you remove the @SuppressWarnings("unchecked") annotation, it will show warning at
compile time because we are using non-generic collection.
@Deprecated
Deprecated annotation is used for informing the compiler that the
particular method, class or field is unimportant and it indicates that
a declaration is outdated.
1. class A{
2. void m(){System.out.println("hello m");}
3.
4. @Deprecated
5. void n(){System.out.println("hello n");}
6. }
7.
8. class TestAnnotation3{
9. public static void main(String args[]){
10.
11. A a=new A();
12. a.n();
13. }}
@Target
Target Tags are used to specify the type of Annotation used. The
Annotation Library declares many constants to specify the type of
element where the annotation is needed to be applied, such as TYPE,
METHOD, FIELD etc. We can access the Target tags
from java.lang.annotation. ElementType
@Documented
// Retention Annotation 1
@Retention(RetentionPolicy.SOURCE)
// Interface
@interface SourceRetention
{
String value() default "Source Retention";
}
// Retention Annotation 2
@Retention(RetentionPolicy.CLASS)
// Interface
@interface ClassRetention
{
String value() default "Class Retention";
}
// Retention Annotation 3
@Retention(RetentionPolicy.RUNTIME)
// Interface
@interface RuntimeRetention
{
String value() default "Runtime Retention";
}
@ClassRetention
class B {
}
@RuntimeRetention
class C {
};
// Main class
public class RetentionPolicyDemo {
System.out.println(
"Number of annotations attached to "
+ "class B at Runtime: " + b.length);
System.out.println(
"Number of annotations attached to "
+ "class C at Runtime: " + c.length);
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:
c
har charValue( ) It returns the encapsulated
character.
Boolean Wrappers: 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
boolean
booleanValue( ) It returns the boolean equivalent of
the invoking object.
The Numeric Type Wrappers: The most commonly used type wrappers are those 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. These
methods are shown here:
byte byteValue( )
double doubleValue( )
float floatValue( )
int intValue()
long longValue()
short shortValue()
For example, doubleValue( ) returns the value of an object as a double, floatValue( ) returns the
value as a float, and so on. These methods are implemented by each of the numeric type wrappers.
All of the numeric type wrappers define constructors that allow an object to be constructed from a
given value, or a string representation of that value. For example, here are the constructors defined
for Integer:
Integer(int num)
Integer(String str)
If str does not contain a valid numeric value, then a NumberFormatException is thrown. 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.
Example:
class TypeWrap
System.out.println("Boolean is " +
b1.booleanValue()); Integer iOb=new
Integer(12); //boxing int
i=iOb.intValue(); //unboxing
System.out.println(i + " is
same as " + iOb); Integer
a=new Integer("21"); int
x=a.intValue();
String s=Integer.toString(25);
Output:
Character is #
Boolean is true
Boolean is False
12 is same as 12
X is 21 s is 25