3. Disclaimer
•Disclaimer: This is not the promotion of
Beer. Just used for an educational purpose.
The author of image contains the full right of
the image. Uses of such product is injurious
to Health.
4. How to represent all Beers in Java?
• { ARNA, GORKHA, TUBOURG, MUSTANG,}
• - These all values are constant type.
• They are of same category
5. ENUM SHORTCUT FOR
ENUMERATION
When we want to represent a group of named
constants.
Ex: Represent month name with single variable.
enum Month {
JAN, FEB, MAR, …DEC;
}
[ ; is optional inside enum]
6. Main Objective | Use
• What is range of byte ?
[ - 128 to + 127 ]
Its Max Values : 256
Byte is a data type with max value : 256
7. Main Obective of enum:
• In month is our data type with 12 values allowed.
• Beer is my own data type with … values allowed
• Its Main Objective:
• To Define our own data types [ which is also known enumerated
data types]
8. Defining an Enum
• Use enum instead of the class keyword and name of enum.
• The constant values are listed in the enum’s body
• It is common to write the constants in all uppercase.
9. While compared to old languages enum it is
more powerful in java, that came in 1.5 V.
• It can also include normal variables, methods and constructors.
• Every enum constant is public static final.
10. the values() and valueOf() method
value and valueOf both are static methods of enum type
and can be used to access enum elements.
Here we are using both the methods to access the enum
elements.
12. • The values() method returns an array of enum-type variables
containing all of the enumeration constants.
• Here is an example:
13. • The valueOf(String) method returns an enumeration constant whose
value corresponds to the string passed to it or
• throws an IllegalArgumentException if no constant with the specified
name was found.
• System.out.println(ProgramLanguages.valueOf("JAVA"));
14. Points to remember about Enumerations
Enumerations are of class type, and have all the capabilities that
a Java class has.
Enumerations can have Constructors, instance Variables,
methods and can even implement Interfaces.
Enumerations are not instantiated using new keyword.
15. enum with Inheritance
• The Java enum class is a special class that allows you to
create a group of constant values (also known as
enumerations)
• The enum class is compiled as a final class in Java, so you
can’t extend an enum class in your code:
16. Type Wrappers: character, Boolean, the numeric type
wrappers
• int a = 5 + 10;
• Primitive types, rather than objects, are used for these
quantities for the sake of performance.
Using objects for these values would add an unacceptable
overhead to even the simplest of calculations.
17. Type Wrappers: character, Boolean, the numeric type
wrappers
Thus, the primitive types are not part of the object hierarchy, and
they do not inherit the Object.
Despite the performance benefit offered by the primitive
types, :
- there are times when you will need an object representation.
18. wrappers
• many of the standard data structures implemented by Java
operate on objects,
• which means that you can't use these data structures to store the primitive types.
• To handle these and other situations, Java provides type
wrappers, which are classes that encapsulate a primitive type
within an object.
19. The type wrappers
• The type wrappers are Integer, Short, Double, Long, Float,
Byte, Character, and Boolean.
• These classes offer a wide array of methods that allow you to
fully integrate the primitive types into the Java's object
hierarchy.
• Let's examine each.
20. Character
• Character is a wrapper around a char. The constructor for
the Character is
•
• char charValue() returns char value.
22. Autoboxing
Autoboxing is the automatic conversion that the Java compiler
makes between the primitive types and their corresponding
object wrapper classes.
For example, converting an int to an Integer, a double to a
Double, and so on.
If the conversion goes the other way, this is called unboxing.
23. Example 1: For Autoboxing and Unboxing
• https://www.geeksforgeeks.org/autoboxing-unboxing-java/
24. Autoboxing/Unboxing in Expressions
• Java 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.
•
25. Ex: Program below the Note
• https://www.demo2s.com/java/java-primitive-data-type-autoboxing-
unboxing-in-expressions.html
Both programs are valid in above link but you can go for 2nd program
and explain it a little.
26. Autoboxing/unboxing Boolean and character values
• http://www.java2s.com/Tutorial/Java/0040__Data-
Type/AutoboxingUnboxingBooleanandCharacterValues.htm
27. Benefits of Autoboxing / Unboxing
1.Autoboxing / Unboxing lets us use primitive types and Wrapper class
objects interchangeably.
2.It helps prevent errors, but may lead to unexpected results sometimes.
Hence must be used with care.
3.We don't have to perform Explicit typecasting.
4.Auto-unboxing also allows you to mix different types of numeric objects in
an expression. When the values are unboxed, the standard type
conversions can be applied.
29. Annotations
• Java annotations are metadata (data about data) for our
program source code.
They provide additional information about the program to the
compiler but are not part of the program itself.
These annotations do not affect the execution of the compiled
program.
30. • Its syntax is:
• @AnnotationName
• Ex:
Annotations start with @.
31. Ex : @Override
• The @Override annotation specifies that the method with this
annotation overrides the superclass method with the same method
name, return type, and parameter list.
• It is not mandatory to use @Override when overriding a method.
• However, if we use it, the compiler gives an error if something is
wrong (such as wrong parameter type) while overriding the method.
32. Example 1: @Override Annotation
Example
• https://www.programiz.com/java-
programming/annotations#:~:text=Java%20annotations%20are%20m
etadata%20(data,execution%20of%20the%20compiled%20program.
• Check example 1
33. Java Annotations Retention Policy
• A retention policy determines at what point an annotation is
discarded.
•
• Java defines three such policies,
• They are SOURCE, CLASS, and RUNTIME.
35. SOURCE
• An annotation with a retention policy of SOURCE is retained
[engaged | activated] only in the source file and is discarded
during compilation.
36. CLASS
• An annotation with a retention policy of CLASS is stored in
the .class file during compilation.
• However, it is not available through the JVM during run time.
37. RUNTIME
• An annotation with a retention policy of RUNTIME is stored in
the .class file during compilation and is available through the
JVM during run time.
Thus, RUNTIME retention offers the greatest annotation
persistence.
38. • 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.
39.
40. Obtaining Annotations at Run Time via Refle
ction
• If annotations specify a retention policy of RUNTIME, then they
can be queried at run time by any Java program through the
use of reflection.
•
• Reflection is the feature that enables information about a
class to be obtained at run time.
41. • Reflection is a feature in the Java programming language. It
allows an executing Java program to examine or "introspect"
upon itself, and manipulate internal properties of the program.
• The ability to examine and manipulate a Java class from within
itself may not sound like very much, but in other programming
languages this feature simply doesn't exist