Java annotations list with examples w3schools

Elizabeth 101 Published: 10/19/2024

Java annotations list with examples w3schools

Java Annotations are used to provide supplemental information about a program element, such as a class, method, or variable. They can also affect the behavior of the code.

Here is a list of Java annotations with examples from W3Schools:

@Override: This annotation indicates that a method overrides another method in the superclass. It ensures that you are not accidentally overriding the wrong method.

Example:
<code class="public class Animal { public void sound() { System.out.println("The animal makes a sound"); } }">

public class Dog extends Animal {

@Override

public void sound() {

System.out.println("The dog barks");

}

}

@Deprecated: This annotation indicates that a method or a field is deprecated and should not be used. Example:
    @Deprecated

public int add(int a, int b) {

return (a + b);

}

}

@SuppressWarnings: This annotation tells the compiler to suppress specific warnings for a specified duration within the annotated code. Example:
    @SuppressWarnings("unchecked")

public static void main(String[] args) {

List list = new ArrayList();

}

}

@Target: This annotation specifies the kind of program element that the annotation can be applied to, such as a method or field. Example:
    Target(ElementType.METHOD);

}

public class Test {

@MyAnnotation

public void myMethod() {

System.out.println("Hello");

}

}

@Retention: This annotation specifies the retention policy for an annotation type, which determines how it is stored and preserved. Example:
    Target(ElementType.METHOD);

Retention(RetentionPolicy.RUNTIME);

}

public class Test {

@MyAnnotation

public void myMethod() {

System.out.println("Hello");

}

}

@Inherited: This annotation indicates that an annotation type is inherited by subclasses. Example:
    Inherited;

Target(ElementType.TYPE);

}

public class Parent {

@MyAnnotation

public void myMethod() {

System.out.println("Hello");

}

}

public class Child extends Parent {

}

@Documented: This annotation indicates that an annotation type should be documented. Example:
    Documented;

Target(ElementType.TYPE);

}

public class Parent {

@MyAnnotation

public void myMethod() {

System.out.println("Hello");

}

}

@Repeatable: This annotation indicates that an annotation type can be repeated on the same element. Example:
    Repeatable;

Target(ElementType.TYPE);

}

public class Parent {

@MyAnnotation

public void myMethod() {

System.out.println("Hello");

}

}

public class Child implements Parent {

}

@Invisible: This annotation indicates that an annotation type should not be displayed. Example:
    Invisible;

Target(ElementType.TYPE);

}

public class Parent {

@MyAnnotation

public void myMethod() {

System.out.println("Hello");

}

}

@Native: This annotation indicates that an annotation type is native to the Java platform. Example:
    Native;

Target(ElementType.TYPE);

}

public class Parent {

@MyAnnotation

public void myMethod() {

System.out.println("Hello");

}

}

This list shows examples of some of the most common annotations used in Java. Annotations are a powerful tool that can help you write cleaner, more maintainable code, and are an essential part of any Java developer's toolbox.

References:

Oracle: Java Language Specification W3Schools: Java Annotations Java Tutorial by FreeCodeCamp

Java annotations list with examples spring boot

Here is the information you requested:

Java Annotations List with Examples Spring Boot

Spring Boot provides various annotations to simplify the configuration and development process of a web application. Here are some common Java annotations used in Spring Boot, along with their explanations and examples:

@SpringBootApplication: This annotation enables auto-configuration for your Spring-based application.

Example: @SpringBootApplication public class MyApplication { ... }

@EnableAutoConfiguration: This annotation enables auto-configuration for specific components or modules in the application.

Example: @EnableAutoConfiguration @SpringBootApplication public class MyWebApplication { ... }

@RestController: This annotation marks a Java class as a web request controller, similar to an MVC controller.

Example: @RestController public class MyController { ... }

@RequestMapping: This annotation specifies the URL mapping for a method or controller.

Example: @RestController @RequestMapping("/api") public class MyController { ... }

@GetMapping, @PostMapping, @PutMapping, and @DeleteMapping: These annotations are variants of the @RequestMapping annotation, specifying GET, POST, PUT, and DELETE HTTP methods respectively.

Examples:

@RestController @RequestMapping("/api") public class MyController { @GetMapping("/users") public List<User> getUsers() { ... } } @RestController @RequestMapping("/api") public class MyController { @PostMapping("/users") public User createUser(User user) { ... } } @RequestParam: This annotation injects a request parameter into a method.

Example: @RestController @RequestMapping("/api") public class MyController { @GetMapping("/users") public List<User> getUsers(@RequestParam("name") String name) { ... } }

@RequestBody: This annotation injects the body of an HTTP request as an object.

Example: @RestController @RequestMapping("/api") public class MyController { @PostMapping("/users") public User createUser(@RequestBody User user) { ... } }

@ResponseBody: This annotation sets the response body for a method.

Example: @RestController @RequestMapping("/api") public class MyController { @GetMapping("/users") public List<User> getUsers() { List<User> users = ...; return users; } }

@Autowired: This annotation injects beans from the application context into a bean.

Example: @RestController @RequestMapping("/api") public class MyController { private UserRepository userRepository; @Autowired public MyController(UserRepository userRepository) { this.userRepository = userRepository; } }

@Value: This annotation injects a value from the configuration files or environment variables into a bean.

Example: @RestController @Value("${my.config.value}") public class MyController { private String myConfigValue; @Autowired public MyController(String myConfigValue) { this.myConfigValue = myConfigValue; } }

These are just a few of the many annotations available in Spring Boot. Understanding these annotations can help you simplify your development process and improve the overall performance of your application.

Please note that I will not respond with emoji, as per our agreement.