जावा एनोटेशन (उदाहरण के साथ)

इस ट्यूटोरियल में, हम सीखेंगे कि एनोटेशन क्या हैं, विभिन्न जावा एनोटेशन और उदाहरणों की मदद से उनका उपयोग कैसे करें।

जावा एनोटेशन हमारे कार्यक्रम स्रोत कोड के लिए मेटाडेटा (डेटा के बारे में डेटा) हैं।

वे कार्यक्रम के बारे में अतिरिक्त जानकारी संकलक को प्रदान करते हैं लेकिन स्वयं कार्यक्रम का हिस्सा नहीं होते हैं। ये एनोटेशन संकलित कार्यक्रम के निष्पादन को प्रभावित नहीं करते हैं।

एनोटेशन के साथ शुरू होता है @। इसका सिंटैक्स है:

 @AnnotationName 

आइए @Overrideएनोटेशन का एक उदाहरण लेते हैं।

@Overrideएनोटेशन निर्दिष्ट विधि है कि इस टिप्पणी के साथ चिह्नित किया गया है एक ही विधि का नाम, वापसी प्रकार, और पैरामीटर सूची के साथ सुपर क्लास की विधि को ओवरराइड करता है कि।

@Overrideकिसी विधि को ओवरराइड करते समय उपयोग करना अनिवार्य नहीं है । हालाँकि, यदि हम इसका उपयोग करते हैं, तो विधि को ओवरराइड करते समय कुछ गलत (जैसे कि गलत पैरामीटर प्रकार) होने पर कंपाइलर एक त्रुटि देता है।

उदाहरण 1: @Override एनोटेशन उदाहरण

 class Animal ( public void displayInfo() ( System.out.println("I am an animal."); ) ) class Dog extends Animal ( @Override public void displayInfo() ( System.out.println("I am a dog."); ) ) class Main ( public static void main(String() args) ( Dog d1 = new Dog(); d1.displayInfo(); ) ) 

आउटपुट

 मैं कुत्ता हूं। 

इस उदाहरण में, विधि displayInfo()सुपरक्लास एनिमल और सबक्लास डॉग दोनों में मौजूद है। जब इस विधि को कहा जाता है, तो सुपरक्लास में विधि के बजाय उपवर्ग की विधि को कहा जाता है।

एनोटेशन प्रारूप

एनोटेशन में तत्व (सदस्य / गुण / पैरामीटर) भी शामिल हो सकते हैं।

1. मार्कर एनोटेशन

मार्कर एनोटेशन में सदस्य / तत्व शामिल नहीं होते हैं। यह केवल एक घोषणा को चिह्नित करने के लिए उपयोग किया जाता है।

इसका सिंटैक्स है:

 @AnnotationName () 

चूंकि इन एनोटेशन में तत्व नहीं होते हैं, इसलिए कोष्ठक को बाहर रखा जा सकता है। उदाहरण के लिए,

 @Override 

2. एकल तत्व एनोटेशन

एक एकल तत्व एनोटेशन में केवल एक तत्व होता है।

इसका सिंटैक्स है:

 @AnnotationName (एलिमेंटनेम = "एलिमेंट वेल") 

यदि केवल एक तत्व है, तो उस तत्व को मूल्य के रूप में नाम देना एक सम्मलेन है।

 @AnnotationName (मान = "एलिमेंट वेल") 

इस मामले में, तत्व नाम को भी बाहर रखा जा सकता है। तत्व नाम डिफ़ॉल्ट रूप से मान होगा।

 @ नोटीकरण ("एलिमेंट वेल") 

3. एकाधिक तत्व एनोटेशन

इन एनोटेशन में अल्पविराम द्वारा अलग किए गए कई तत्व होते हैं।

इसका सिंटैक्स है:

 @ नोटेशन नाम (तत्व 1 = "मान 1", तत्व 2 = "मान 2") 

एनोटेशन प्लेसमेंट

किसी भी घोषणा को उस घोषणा से ऊपर रखकर एनोटेशन के साथ चिह्नित किया जा सकता है। जावा 8 के अनुसार, एनोटेशन को एक प्रकार से पहले भी रखा जा सकता है।

1. घोषणाओं से ऊपर

जैसा कि ऊपर उल्लेख किया गया है, जावा एनोटेशन को वर्ग, विधि, इंटरफ़ेस, फ़ील्ड, और अन्य प्रोग्राम एलिमेंट एलान से ऊपर रखा जा सकता है।

उदाहरण 2: @SuppressWarnings एनोटेशन उदाहरण

 import java.util.*; class Main ( @SuppressWarnings("unchecked") static void wordsList() ( ArrayList wordList = new ArrayList(); // This causes an unchecked warning wordList.add("programiz"); System.out.println("Word list => " + wordList); ) public static void main(String args()) ( wordsList(); ) ) 

आउटपुट

 शब्द सूची => (प्रोग्रामिज़) 

यदि @SuppressWarnings("unchecked")एनोटेशन का उपयोग किए बिना उपरोक्त प्रोग्राम संकलित किया जाता है , तो कंपाइलर अभी भी प्रोग्राम को संकलित करेगा, लेकिन यह चेतावनी देगा जैसे:

Main.java अनियंत्रित या असुरक्षित संचालन का उपयोग करता है। शब्द सूची => (प्रोग्रामिज़)

हमें चेतावनी मिल रही है

 Main.java अनियंत्रित या असुरक्षित संचालन का उपयोग करता है 

निम्नलिखित कथन के कारण।

 ArrayList wordList = new ArrayList(); 

This is because we haven't defined the generic type of the array list. We can fix this warning by specifying generics inside angle brackets .

 ArrayList wordList = new ArrayList(); 

2. Type annotations

Before Java 8, annotations could be applied to declarations only. Now, type annotations can be used as well. This means that we can place annotations wherever we use a type.

Constructor invocations

 new @Readonly ArrayList() 

Type definitions

 @NonNull String str; 

This declaration specifies non-null variable str of type String to avoid NullPointerException.

 @NonNull List newList; 

This declaration specifies a non-null list of type String.

 List newList; 

This declaration specifies a list of non-null values of type String.

Type casts

 newStr = (@NonNull String) str; 

extends and implements clause

 class Warning extends @Localized Message 

throws clause

 public String readMethod() throws @Localized IOException 

Type annotations enable Java code to be analyzed better and provide even stronger type checks.

Types of Annotations

1. Predefined annotations

  1. @Deprecated
  2. @Override
  3. @SuppressWarnings
  4. @SafeVarargs
  5. @FunctionalInterface

2. Meta-annotations

  1. @Retention
  2. @Documented
  3. @Target
  4. @Inherited
  5. @Repeatable

3. Custom annotations

These annotation types are described in detail in the Java Annotation Types tutorial.

Use of Annotations

  • Compiler instructions - Annotations can be used for giving instructions to the compiler, detect errors or suppress warnings. The built-in annotations @Deprecated, @Override, @SuppressWarnings are used for these purposes.
  • Compile-time instructions - Compile-time instructions provided by these annotations help the software build tools to generate code, XML files and many more.
  • रनटाइम निर्देश - कुछ एनोटेशन को रनटाइम पर प्रोग्राम को निर्देश देने के लिए परिभाषित किया जा सकता है। ये एनोटेशन जावा रिफ्लेक्शन का उपयोग करके एक्सेस किए जाते हैं।

दिलचस्प लेख...