Java Comments

प्रोग्रामिंग करते समय, कई बार एक प्रोग्रामर को किसी प्रोजेक्ट को पूरा करने के लिए अधिक समय की आवश्यकता होती है। तो comments का उपयोग यह पहचानने के लिए किया जाता है की यह कोड क्यों लिखा गया है।

जैसे की आपने किसी एक प्रोजेक्ट के लिए 1000 lines का कोड लिखा अब आप 1 महीने के लंबे अंतराल के बाद लौटते है और फिर से उसी कोड पर दुबारा से काम करना शुरु करते है।

पहला तरीका: अब यह होगा की आपको सारे 1000 lines को एक एक करके देखना होगा की कौनसा line आपने किस काम के लिल्ये लिखा है।

दूसरा और सरल तरीका: जैसे की आपने 10 लाइन का कोड लिखा, किसी addition को परफॉर्म करने के लिए तो वहां पर आप एक comments के तौर पर लिख सकते हैं // Code for Addition. अब होगा यह कि जब आप अगली बार आएंगे तो आपको ढूंढना नहीं पड़ेगा यह कोड आपने किस लिए लिखा था देखते ही समझ जाएंगे कि यह addition का कोड है।

और इस तरह आपका कोड एक बेहतर human-readable बनेगा जिसे आप भी या कोई और भी आसानी से समझ सकेगा।

Types of Java Comments

  1. Single-line Comments.
  2. Multi-line Comments.
  3. Documentation Comments.
Java Comments

Single-line Comments

सिंगल लाइन कमेंट (Single-line Comments) सबसे आसान और सबसे ज्यादा यूज होने वाला Java comments है, यह डबल फॉरवार्ड स्लैश // के साथ शुरू होता है। कोड को compile करते समय, Java compiler डबल फॉरवार्ड स्लैश // के बाद आने वाले सारे code को इग्नोर कर देता हैं।

नोट: सिंगल लाइन कोमेंट्स (Single-line Comments) सिर्फ सिंगल लाइन में ही काम करता है।
Syntax
// Comments लिखने का तरीका
Example: Java comments before a line of code:
// यह एक comment है, Java इसे इग्नोर करता है।
System.out.println("Hello World");

Multi-line Comments

मल्टीलाइन कमेंट (Multi-line comments) का इस्तेमाल हम तब करते हैं जब कोई comments बहुत बड़ा हो और हमें किसी कोड block को विस्तार से डिस्क्राइब करना होता है।

मान लीजिए अपने 10 लाइन का java comments लिखा, अब इसके लिए आप सिंगल लाइन कमेंट (Single-line Comments) का इस्तेमाल तो करेंगे नहीं क्योंकि सिंगल लाइन कमेंट में हर लाइन में आपको डबल फॉरवार्ड स्लैश // लगाना पड़ेगा जो कि समय की बर्बादी होगी इससे बचने के लिए मल्टीलाइन कमेंट (Multi-line comments) का इस्तेमाल किया जाता है।

Multi-line comments /* से शुरू होती है और */ से खत्म होती है, /* और */ के बिच में आने वाले सारे कोड को जावा ignore कर देता है और इसे compile नहीं करता है।

Syntax
/* Java Comments line 1
Comments line 2
Comments line so on */
Example multi-line Java comments:
/* This text
will not print
this is an example of
multi-line Comments */

System.out.println("Hello World");

Documentation Comments

The documentation comment को doc comment के रूप में भी जाना जाता है। सॉफ़्टवेयर पैकेज के लिए कोड लिखते समय आमतौर पर इस प्रकार की comments का उपयोग किया जाता है। Doc comments का उपयोग JDK Javadoc टूल की मदद से एक auto-generated documentation page बनाने के लिए किया जाता हैं।

जावाडोक स्रोत कोड में दस्तावेज़ टिप्पणियों से एचटीएमएल प्रारूप में जावा कोड दस्तावेज उत्पन्न करने के लिए एक उपकरण है। जावाडोक के लिए दस्तावेज़ पूर्वनिर्धारित प्रारूप में हैं। What is JDK Javadoc tool?

Javadoc एक tool जो doc comments को HTML के प्रारूप में Java code documentation को उत्पन्न करने के लिए इस्तेमाल किया जाता है।

नोट: Javadoc के लिए Documents पूर्वनिर्धारित (predefined) फॉर्मेट में होते हैं।

Javadoc जावा के performance को प्रभावित नहीं करता है क्योंकि कोड compiling करते समय सभी comments को ignore कर दिया जाता है Java के द्वारा। Javadoc को बेहतर ढंग से समझने और लिखना सीखे

Syntax
/**Comment start
 *
 *tags are used in order to specify a parameter
 *or heading or method
 *HTML tags can also be used
 *such as <h1>...<h2> etc.
 *
 *comment ends*/
Example documentation comments:
/** Comments start
 * Simple "Hello World" will be displayed
 * <h1>, <h2> HTML Tags also
 * @author Brajlal
 * @version 1.2.34
 *
 * Comments ends*/

public class MyClass {

  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

Javadoc टूल द्वारा पहचाने जाने वाले उपलब्ध Javadoc टैग इस प्रकार हैं:

Tag Description Syntax
@author Adds the author of a class. @author name-text
{@code} Displays text in code font without interpreting the text as HTML markup or nested Javadoc tags. {@code text}
{@docRoot} Represents the relative path to the generated document's root directory from any generated page. {@docRoot}
@deprecated Adds a comment indicating that this API should no longer be used. @deprecated deprecated text
@exception Adds a Throws subheading to the generated documentation, with the classname and description text. @exception class-name description
{@inheritDoc} Inherits a comment from the nearest inheritable class or implementable interface. Inherits a comment from the immediate superclass.
{@link} Inserts an in-line link with the visible text label that points to the documentation for the specified package, class, or member name of a referenced class. {@link package.class#member label}
{@linkplain} Identical to {@link}, except the link's label is displayed in plain text than code font. {@linkplain package.class#member label}
@param Adds a parameter with the specified parameter-name followed by the specified description to the "Parameters" section. @param parameter-name description
@return Adds a "Returns" section with the description text. @return description
@see Adds a "See Also" heading with a link or text entry that points to reference. @see reference
@serial Used in the doc comment for a default serializable field. @serial field-description | include | exclude
@serialData Documents the data written by the writeObject( ) or writeExternal( ) methods. @serialData data-description
@serialField Documents an ObjectStreamField component. @serialField field-name field-type field-description
@since Adds a "Since" heading with the specified since-text to the generated documentation. @since release
@throws The @throws and @exception tags are synonyms. @throws class-name description
{@value} When {@value} is used in the doc comment of a static field, it displays the value of that constant. {@value package.class#field}
@version Adds a "Version" subheading with the specified version-text to the generated docs when the -version option is used. @version version-text
Article By: Brajlal Prasad
Created on: 17 Feb 2023  1556  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]