The basic Annotation

  • @Override
  • @Deprecated
  • @SuppressWarnings
  • @SafeVarargs

Override is used to specify method overloading, which forces a child class to Override the parent class’s methods.

  • For example, if the parent class has a run() method, the subclass’s method name is accidentally written runn(), and the program is compiled with an error message.
  • Acts only on methods, not other program elements.

@deprecated is used to indicate that an element (class, method, etc.) is obsolete, and the compiler will issue a warning when other programs use the obsolete class or method.

  • Although there is a warning, but does not affect the use, use can still be used
// superclass, where the method marked test() is obsolete. Public class supperClass {@deprecated public void test(){System.out.println(); }} // If the subclass is called, the compiler will warn you that the method is obsolete. public class SonClass extends SupperClass{ public static void main(String[] args) { SonClass sonClass = new SonClass();

sonClass. test() ; // The prompt method is obsolete

}}

@SuppressWarnings instructs the program element (and all child elements of the program) decorated with this Annotation to cancel the display of the specified compiler warning.

  • If you use a method annotated with @Deprecated, the compiler will display a warning message where the method is called. Put @SuppressWarnings(“deprecation”) on the class that called the outdated method and the warning is removed.
Public class supperClass {@deprecated public void test(){System.out.println(); }} @suppressWarnings ("deprecation")// Add this annotation, Public class extends SupperClass{public static void main(String[] args) {sonClass sonClass = new SonClass(); sonClass.test(); }}

The Java compiler gives an unchecked warning when it declares a constructor or method with a mutable argument of an ambiguous type, such as a generic type.

  • In such cases, if it is determined that the declared constructor and method body will not perform potentially unsafe operations on its varargs arguments, you can flag it with @safevargs so that the Java compiler will not report an unchecked warning.
  • For methods that are not static or final, this does not apply and will not compile. To suppress unchecked warnings, you can use @SuppressWarnings annotation: @SuppressWarnings(“unchecked”)