Java Programming Applets, Type Wrappers, Generic Classes
Java Programming Applets, Type Wrappers, Generic Classes
1
TOPICS
Applet fundamentals
Enumerations
Type wrappers
Auto boxing
Annotations
Generics interfaces.
2
APPLET FUNDAMENTALS
There are two types of java programs:
1. Applets 2. Applications
Applications are similar to C/ C++ programs used to solve
problems.
Applets are small applications that are accessed on an
Internet server, transported over the Internet,
automatically installed, and run as part of a web document.
An applet is a GUI based program.
Applets are event –driven programs.
Applets do not contain main() method.
3
APPLET- A SIMPLE PROGRAM
6
RUNNING OF APPLETS IN BROWSER
Write a short HTML text file that contains a tag that loads the applet. At
the time of this writing, Oracle recommends using the APPLET tag for this
purpose.
Here is the Runapp.html file that executes:
<html>
<body>
<applet code="SimpleApplet" width=200 height=60>
</applet>
</body>
</html>
The width and height statements specify the dimensions of the display
area used by the applet.
Three steps are needed to run with browser:
1. Edit a Java source file.
2. Compile your program. 7
3. Open the html file where it is stored.
RUNNING STEPS WITH APPLET VIEWER:
1. Edit a Java source file.
2. Compile your program.
3. Execute the applet viewer, specifying the name of your
applet’s source file. The applet viewer will encounter the
APPLET tag within the comment and execute your applet.
8
LIFE CYCLE OF APPLETS
There are 5 methods in Applets life cycle:
1. public void init( )- to initialize or pass input to an applet.
2. public void start( )- It is called after init( ) –starts an applet.
3. public void stop( )- stops a running applet.
4. public void paint(Graphics g)- To draw some thing with in a applet.
5. public void destroy( )- to remove an applet from memory completely.
9
METHODS USED IN JAVA. AWT. GRAPHICS CLASS
10
11
12
ENUMERATIONS
An enumeration is a list of named constants.
An enumeration is created using the enum keyword.
In Java, an enumeration defines a class type.
Each enumeration constant is an object of its enumeration type.
Each enumeration constant has its own copy of any instance
variables defined by the enumeration.
All enumerations automatically inherit one: java.lang.Enum
Example:
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.
To declare a variable of Apple type:
Apple ap; 13
ap=Apple.RedDel;
14
2 BUILT IN METHODS IN ENUMERATIONS
15
16
ENUMERATIONS WITH CONSTRUCTORS, INSTANCE
VARIABLES & METHODS:
17
18
19
20
AUTO BOXING:
Auto boxing is the process by which a primitive type is
automatically encapsulated (boxed) into its equivalent type
wrapper whenever an object of that type is needed.
Auto unboxing is the process by which the value of a boxed
object is automatically extracted (unboxed) from a type wrapper
when its value is needed.
Where it works:
1. In assignments.
2. In Method arguments and return types.
3. In Expressions.
4. In switch statement and any loop statements.
21
22
23
ANNOTATIONS(META DATA)
Java Annotation is a tag that represents the metadata i.e.
attached with class, interface, methods or fields to indicate
some additional information which can be used by java
compiler and JVM.
2 types of Annotations:
1. Built-in annotations
2.Custom annotations / user-defined annotations.
24
BUILT IN ANNOTATIONS
25
META ANNOTATIONS:
Built-In Java Annotations used in other annotations are
called as meta-annotations.
There are several meta-annotation types defined in
java.lang.annotation.
Meta annotations are 4 types:
1. @target: @target annotation marks another annotation to
restrict what kind of Java elements the annotation can be
applied to.
2. @Retention: @Retention annotation specifies how the
marked annotation is stored.
3. @Inherited: @Inherited annotation indicates that the
annotation type can be inherited from the super class.
4. @Documented: @Documented annotation indicates that
whenever the specified annotation is used those elements
should be documented using the Javadoc tool. 26
USER DEFINED ANNOTATIONS
Java Custom annotations or Java User-defined annotations
are easy to create and use.
The @interface element is used to declare an annotation:
30
GENERIC CLASS- EXAMPLE
31
GENERIC METHODS-EXAMPLE
32
GENERICS CONSTRUCTORS - EXAMPLE
33
GENERIC INTERFACES - EXAMPLE
34
35