Javaunit1 New
Javaunit1 New
Example:
// A simple annotation type
@ interface FirstAnno{
String str();
int val();
}
The @ symbol preceding the keyword interface tells
the compiler that an annotation type is being declared.
There are also two members str() and val(). All
annotation solely consists of method declaration and
these methods act much like fields.
It is to be noted that annotation
cannot include extends clause.
However, it is automatic that all
annotation programs extend
the Annotation interface.
Thus Annotation is the superclass
of all annotations.
An enumeration is created using the enum keyword.
For example, here is a simple enumeration that lists
various apple varieties:
Enums
An enum is a special "class" that represents a group
of constants (unchangeable variables,
like final variables).
To create an enum, use the enum keyword (instead of
class or interface), and separate the constants with a
comma. Note that they should be in uppercase letters:
Example
// Valid
ArrayList<Integer> myNumbers =
new ArrayList<Integer>();
To create a wrapper object, use the wrapper class instead of the
primitive type.
To get the value, you can just print the object:
Example
public class Main
{
public static void main(String[] args)
{
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
Need of Wrapper Classes
There are certain needs for using the
Wrapper class in Java as mentioned
below:
They convert primitive data types into
objects.
Objects are needed if we wish to
modify the arguments passed into a
method (because primitive types are
passed by value).
The classes in java.util package
handle only objects and hence wrapper
classes help in this case.
Data structures in the Collection
framework, such as ArrayList and
Vector, store only objects (reference
types) and not primitive types.
An object is needed to support
synchronization in multithreading.
Advantages of Wrapper Classes
Collections allow only object data.
On object data we can call multiple
methods compareTo(), equals(),
toString()
The cloning process only works on
objects
Object data allows null values.
Serialization allows only object data.
Since you're now working with objects, you
can use certain methods to get information
about the specific object.
For example, the following methods are
used to get the value associated with the
corresponding wrapper
object: intValue(), byteValue(), shortValue(),
longValue(), floatValue(), doubleValue(), ch
arValue(), booleanValue().
This example will output the same result as the
example above:
Example
public class Main {
public static void main(String[] args) { Integer
myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt.intValue());
System.out.println(myDouble.doubleValue());
System.out.println(myChar.charValue());
Autoboxing/Unboxing Occurs
in Expressions
In general, autoboxing and unboxing
take place whenever a conversion into
an object or from an object is required.
This applies to expressions.
Within an expression, a numeric object
is automatically unboxed.
The outcome of the expression is
reboxed, if necessary.
AutoBoxing and Auto-UnBoxing occurs in
expressions. Lets understand it in the below
example -
Example -
class TestAnnotation1{
public static void main(String args[]){
Animal a=new Dog();
a.eatSomething();
}}
Test it NowOutput:
Specifying Retention Policy
In Java, annotations are used to attach meta-data
to a program element such as a class, method,
instances, etc.
Some annotations are used to annotate other
annotations. These types of annotations are
known as meta-annotations.
@Retention is also a meta-annotation that
comes with some retention policies.
These retention policies determine at which
point an annotation is discarded.
There are three types of retention policies: SOURCE, CLASS,
and RUNTIME.
RetentionPolicy.SOURCE: The
annotations annotated using the SOURCE
retention policy are discarded at runtime.
RetentionPolicy.CLASS: The annotations
annotated using the CLASS retention policy
are recorded in the .class file but are
discarded during runtime.
CLASS is the default retention policy in
Java.
RetentionPolicy.RUNTIME: The
annotations annotated using the
RUNTIME retention policy are
retained during runtime and can be
accessed in our program during
runtime.
What
is JavaBeans?
JavaBeans is a portable, platform-
independent model written in Java
Programming Language.
Its components are referred to as beans.
In simple terms, JavaBeans are classes
which encapsulate several objects into a
single object.
It helps in accessing these object from
multiple places.
JavaBeans contains several elements like
Constructors, Getter/Setter Methods and
much more.
What is Jsp?
JavaServer Pages (JSP) is a server-side
technology that creates dynamic web
applications.
It allows developers to embed Java code
directly into HTML or XML pages and it
makes web development more efficient.
JSP is an advanced version of Servlets.
It provides enhanced capabilities for
building scalable and platform-independent
web pages.
JSP simplifies web development by
combining the strengths of Java with
the flexibility of HTML. Some of the
advantages of JSP over Servlets are
listed below:
JSP code is easier to manage than
Servlets as it separates UI and business
logic.
JSP minimizes the amount of code
required for web applications.
Easily generates content dynamically
in response to user interactions.
It provides access to the complete
range of Java APIs for robust
application development.
JSP is suitable for applications with
growing user bases.
It is a software application on a
Java platform that is platform-
independent.
It is portable, and its components
are called beans.
You can convert data structures or
objects into a particular format that
can be stored, transmitted, and
reconstructed later by the user,
which can be termed serialization
Uses of java beans
You can create and design any component.
As it is reusable software, one can use it in any
environment.
It contains different functions like spelling
correction and forecasting the stock market one
can perform several functions.
It also comes with a property of visible or
invisible.
The end-user can see it or maybe can’t see it. It
can work autonomously.
It is also capable of showing a pie chart.
JavaBeans has interactive capabilities that
can provide support to a web-based
application.
It is user-friendly and system-friendly also.
One can run JavaBeans on any operating
system.
One can run JavaBeans in several
applications like word processors,
browsers, and many more.
Instead of writing custom components, you
can use UI elements like graphical buttons,
trees, and many more.
While considering JavaBeans, you
must follow some conventions:
1. Several Conventions
A default constructor (no arguments) is a
must for JavaBeans.
Serialization(converting data into
bits/bytes)data can be transferred and
stored.
getter and setter methods(all variables
must be either private or protected)
2. Components
Components are the class that stores the
definition of beans.
Classes contain design conventions. It
includes persistence, events, properties, and
methods.
It consists of two types of components i.e.,
Non-GUI and GUI.
3. Classes
Persistence: With the help of this class, you
can store the state of JavaBean.
Events: It is pretty alike to Swing event
handling.
Properties: It includes color, display, font,
etc. It will also show the appearance and
behavior of the bean and many more.
Methods: You will find it exactly like the
normal java methods.
4. Components
These components can be in the
following mode
write-only mode
read or write mode
read-only mode
Characteristics of Java Beans
Java Beans possess several key characteristics
that set them apart from regular Java classes.
These characteristics include:
Public Default Constructor: Java Beans must
have a public default constructor to allow the
framework or application to instantiate them.
Private Fields with Public
Accessors: Encapsulated fields within a Java
Bean should be declared as private, with
public getter and setter methods to access and
modify the data.
Serializable: Java Beans should
implement the Serializable interface,
enabling their state to be saved and
restored.
Event Support: Java Beans can
support events by implementing
appropriate listener interfaces, allowing
other components to be notified of state
changes.
Advantages of java beans
The best thing about Java beans is
that you have to write code only
once and can run anywhere.
You can work on several local
platforms.
The developer can control
properties, methods, and events.
At design time, a user can configure
JavaBeans without any disturbance.
It can easily be created. It is easy to
write JavaBean.
JavaBeans are light in weight. You don’t
have to make the extra effort to create a
suitable environment to support
JavaBean.
JavaBeans are compatible in nature.
JavaBeans are classes that encapsulate
many objects into a single object (the
bean).
It is a Java class that should follow the
following conventions:
Must implement Serializable.
It should have a public no-arg
constructor.
All properties in java bean must be
private with public getters and setter
methods.
Below is the implementation of the
JavaBean Class:
// Java Program of JavaBean class // Getter for Id
package geeks; public int getId() { return id; }
}
public void setDepth(double
d) { }
depth = d; public double getWidth( ) {
return width;
} }