Advanced Java
Content in the Notes
1) Enumeration
2) Annotations
3) Serialization
4) Multithreading
5) Synchronisation
6) Autoboxing
7) Input?/Output
8) Database Connections
9) Generics
10) String Handling
11) Java.Lang and Java.Util
12) Networking
13) Images in Java
14) Concurrency Utilities
15) Regular Expression
16) Non-Blocking Input/Output
17) Java Beans
18) Spring Framework
19) Spring MVC
20) Spring and REST API
21) Additional Points
, 2
Enumeration in Java (Enums)
Why Use Enums?
Enums are used to define constants that remain unchanged, such as:
o Months of the year (e.g., January to December).
o Days of the week (e.g., Monday to Sunday).
o Time zones (e.g., GMT, PST).
o Seasons (e.g., Summer, Winter).
These values are type-safe and ensure consistency across applications.
Properties of Enums:
Defined as a new data type using the enum keyword.
Enums are immutable (cannot be changed after creation).
Values are treated as constants.
Syntax to Define Enums:
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY,
SUNDAY;
}
Values are in uppercase by convention.
Advanced Enum Example:
Enums can also hold associated values:
public enum Color {
RED("red"), GREEN("green"), BLUE("blue");
private String value;
, 3
private Color(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
Using Enums:
Accessing Enum:
Color c1 = Color.RED;
System.out.println(c1.name()); // Outputs: RED
System.out.println(c1.getValue()); // Outputs: red
Iterating Through Enums:
for (Color color : Color.values()) {
System.out.println(color.name()); // Enum name
System.out.println(color.getValue()); // Enum value
}
Annotations in Java
Annotations in Java are like shortcuts or markers that provide additional instructions to the
compiler or runtime. They start with the @ symbol and serve as metadata for your code. For
, 4
example, when you use @Override, you’re telling the compiler that a method overrides one in
the parent class.
Why Use Annotations?
They simplify coding by reducing boilerplate.
They provide instructions for:
o Compile-time (e.g., checking if a method is overridden).
o Runtime (e.g., configuring how methods or classes behave during execution).
o Deployment-time processing (e.g., setting up metadata for deployment).
Common Predefined Annotations
1. @Override: This tells the compiler that a method is overriding a parent class method.
Without this annotation, the compiler might need extra processing to figure it out. Using
@Override ensures better clarity and reduces errors.
@Override
public void someMethod() {
// Overrides a method from the superclass
}
2. @SuppressWarnings: This suppresses warnings in the code, such as unused variables.
While it’s not always recommended, it can be useful in specific scenarios.
3. @Author: Used to add metadata about the author of the code. When someone reads your
code or its documentation, they can see who wrote it.
Creating Your Own Annotations
Sometimes, you may want to define your own annotations to include custom metadata. Let’s
break this into steps.