Header Ads

Comments in Java

  Comments in Java

Java Comments are used in Java programs to provide descriptions or annotations. They are important for code readability, documentation, and ease of use. In Java, there are three types of comments:

1. Single-Line Comments:

Single-line comments start with the // symbol. They are used to comment on the portion of code following the comment. When the compiler encounters them, it ignores them. These comments improve the readability of the code and assist the user in understanding it. The following example demonstrates the use of a single-line comment:


int num = 10; // Calculation for acceleration


2. Multi-Line Comments:

Multi-line comments are enclosed between /* and */ symbols. They are used to comment on one or more blocks of code. The compiler ignores these comments and does not consider them necessary. These comments are used for commenting larger blocks of code, functions, or classes, as well as providing licensing information and other details. The following example demonstrates the use of a multi-line comment:


/*

This is a function that adds two numbers

@param a The first number

@param b The second number

@return The sum of the numbers

*/

public int add(int a, int b) {

    return a + b;

}

*/



3. Documentation Comments:

Documentation comments are a special type of multi-line comment that is used by the JavaDoc tool. These comments are used to generate detailed documentation, including class, interface, function, and parameter descriptions. These comments can be written in HTML or Markdown format to make them useful for web or API documentation.

/**

* This class is for adding two numbers.

*/

public class Adder {

   /**

   * Adds two numbers

   * @param a The first number

   * @param b The second number

   * @return The sum of the numbers

   */

   public int add(int a, int b) {

      return a + b;

   }

}


By using Java comments, you can enhance your code's readability, documentation, and support. Make an effort to write them correctly and use them regularly as a reference for your learning experience.


हिंदी:

Java Comments जावा प्रोग्राम में विवरण या संकेत देने के लिए उपयोग होते हैं। वे कोड की पठनीयता, डॉक्यूमेंटेशन और सहजता के लिए महत्वपूर्ण होते हैं। जावा में तीन प्रकार के कमेंट्स होते हैं:

1. Single-Line Comments:

एकल-पंक्ति कमेंट्स // चिह्न के साथ शुरू होते हैं। वे कोड लाइन के आगे के भाग को टिप्पणी करने के लिए उपयोग होते हैं। जब कंपाइलर उन्हें देखता है, वे उन्हें अनदेखा करता है। ये कमेंट्स कोड की पठनीयता बढ़ाते हैं और उपयोगकर्ता को समझ में मदद करते हैं। निम्नलिखित उदाहरण एकल-पंक्ति कमेंट का उपयोग दिखाता है:

int num = 10; // एक्सेलरेशन के लिए वापसी की गणना


2. Multi-Line Comments:

बहु-पंक्ति कमेंट्स /* और */ के बीच होते हैं। वे एक या अधिक कोड ब्लॉक को टिप्पणी करने के लिए उपयोग होते हैं। कंपाइलर इन कमेंट्स को अनदेखा करता है और उन्हें आवश्यक नहीं मानता है। इन कमेंट्स का उपयोग बड़े ब्लॉक कोड, फ़ंक्शन या क्लासों के विवरण, लाइसेंस जानकारी आदि के लिए किया जाता है। निम्नलिखित उदाहरण बहु-पंक्ति कमेंट का उपयोग दिखाता है:

/*

यह एक फ़ंक्शन है जो दो संख्याओं का योग करता है

@param a पहला संख्या

@param b दूसरा संख्या

@return योग का परिणाम

*/

public int add(int a, int b) {

    return a + b;

}

*/

3. Documentation Comments:

दस्तावेज़ीकरण कमेंट्स एक विशेष प्रकार के बहु-पंक्ति कमेंट्स होते हैं जिन्हें जावाडॉक टूल उपयोग करता है। इन कमेंट्स का उपयोग विस्तृत डॉक्यूमेंटेशन उत्पन्न करने के लिए किया जाता है, जिसमें क्लास, इंटरफ़ेस, फ़ंक्शन और पैरामीटर्स के विवरण शामिल हो सकते हैं। इन कमेंट्स को HTML या मार्कडाउन फ़ॉर्मेट में लिखा जा सकता है ताकि वे वेब या एपीआई दस्तावेज़ीकरण में प्रयोगी हों।

/**

* यह क्लास दो अंकों को जोड़ने के लिए है।

*/

public class Adder {

   /**

   * दो संख्याओं का योग करता है

   * @param a पहला संख्या

   * @param b दूसरा संख्या

   * @return योग का परिणाम

   */

   public int add(int a, int b) {

      return a + b;

   }

}

अब आ प जावा कमेंट्स का उपयोग करके अपने कोड को डॉक्यूमेंट, संशोधन और समर्थन के लिए बेहतर बना सकते हैं। उन्हें सही ढंग से लिखने का प्रयास करें और अपने सीखने के अनुभव को संदर्भित करने के लिए उन्हें नियमित रूप से उपयोग करें।

No comments

Powered by Blogger.