0% found this document useful (0 votes)
111 views35 pages

Java Programming Applets, Type Wrappers, Generic Classes

The document discusses several topics related to Java programming including: [1] Applet fundamentals and creating a simple applet program. [2] Enumerations, type wrappers, auto boxing, annotations, and generics. [3] The general form of a generics class allows the class to work with various data types through type parameters.

Uploaded by

Pavan Pulicherla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views35 pages

Java Programming Applets, Type Wrappers, Generic Classes

The document discusses several topics related to Java programming including: [1] Applet fundamentals and creating a simple applet program. [2] Enumerations, type wrappers, auto boxing, annotations, and generics. [3] The general form of a generics class allows the class to work with various data types through type parameters.

Uploaded by

Pavan Pulicherla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

JAVA PROGRAMMING

APPLETS, TYPE WRAPPERS,


GENERIC CLASSES

1
TOPICS
 Applet fundamentals
 Enumerations

 Type wrappers

 Auto boxing

 Annotations

 Generics: The general form of a generics class

 Creating a generic method

 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

 The first imports the Abstract Window Toolkit (AWT) classes.


Applets interact with the user through a GUI framework.
 The second import statement imports the applet package, which
contains the class Applet.
 Every AWT-based applet that you create must be a subclass
(either directly or indirectly) of Applet.
 SimpleApplet class must be declared as public, because it will be
accessed by code that is outside the program.
 paint ( ) is defined by AWT & must be overridden by the applet.4
APPLET- A SIMPLE PROGRAM

 The paint( ) is also called when the applet begins execution.


 The paint( ) method has one parameter of type Graphics.
 This Graphics parameter contains the graphics context, which
describes graphics environment in which the applet is running.
 This context is used whenever output to the applet is required.
 Inside paint( ) is a call to drawString( ), which is a member of the
Graphics class.
 This drawString( ) method outputs a string beginning at the
specified X,Y location.
 Syntax: void drawString(String message, int x, int y)
5
 Here, message is the string to be output beginning at x,y.
RUNNING OF APPLETS
 In fact, there are two ways in which you can run an applet:
1. Executing the applet within a Java-compatible web browser.
2. Using an applet viewer, such as the standard tool, applet
viewer.
An applet viewer executes your applet in a window. This is
generally the fastest and easiest way to test your applet.

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

Here, The constructor is called when each enumeration constant is created.


TYPE WRAPPERS
 Java uses primitive types (also called simple types), such as int or
double, to hold the basic data types supported by the language.
 Java provides type wrappers, which are classes that encapsulate a
primitive type within an object.
 The type wrappers are Double, Float, Long, Integer, Short,
Byte, Character, and Boolean.
 These classes offer a wide array of methods that allow you to fully
integrate the primitive types into Java’s object hierarchy.

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:

 3 types of User-defined annotations are:


1. Marker Annotation
2. Single-Value Annotation
3. Multi-Value Annotation
27
USER DEFINED ANNOTATIONS
1. Marker Annotation: It is a special kind of annotation that contains
no members. Its sole purpose is to mark an item. For example:
@interface MyMarker { }

2. Single-Value Annotation: It is an annotation that has one method,


is called single-value annotation. We can provide the default value
also. For example:
@interface MyAnnotation{
int value() default 0;
}
3. Multi-Value Annotation: It is an annotation that has more than
one method, is called Multi-Value annotation. For example:
@interface MyAnnotation{
int value1();
String value2();
String value3();
}
28
}
GENERICS

 Generics are introduced by JDK5.


 Generics adds a new syntactical element to the language.
 Generics causes changes to many of the classes and methods in
the core API.
 The term generics means parameterized types.
 Parameterized types are important because they enable you to
create classes, interfaces, and methods in which the type of data
upon which they operate is specified as a parameter.
 A class, interface, or method that operates on a parameterized
type is called generic, as in generic class or generic method.
 Advantages:
1. It is possible to create classes, interfaces, and methods that
will work in a type-safe manner with various kinds of data.
2. Compile-Time Checking.
29
3. Enabling programmers to implement generic algorithms
THE GENERAL FORM OF A GENERICS CLASS

The syntax for a generic method:

<type-param-list > ret-type meth-name (param-list) { // …

type-param-list is a comma-separated list of type parameters

30
GENERIC CLASS- EXAMPLE

31
GENERIC METHODS-EXAMPLE

32
GENERICS CONSTRUCTORS - EXAMPLE

33
GENERIC INTERFACES - EXAMPLE

34
35

You might also like