← Back to Core Java Course | Chapter 1: Introduction & Basics | Lesson 6 of 12

Java में Comments

Comments वे notes हैं जो आप अपने code में लोगों के पढ़ने के लिए लिखते हैं, और computer इन्हें नज़रअंदाज़ करता है। यह आपके homework के side में एक reminder लिखने जैसा है।
Syntax
java
// single-line comment

/* multi-line
   comment */

/** Javadoc comment */

Single-Line Comments

एक // comment उस point से line के आखिर तक चलता है, इसे किसी tricky logic के टुकड़े को इसके बगल में explain करने वाले छोटे, one-line notes के लिए ideal बनाते हुए।

उदाहरण: Single-Line Comments

java
public class Main {
	public static void main(String[] args) {
		int total = 100; // running total for the cart
		System.out.println(total);
	}
}

Multi-Line Comments

एक /* ... */ block comment कई lines तक span कर सकता है, जो code के एक chunk को temporarily disable करने या एक complex method के ऊपर एक लंबा explanation लिखने के लिए उपयोगी है।

उदाहरण: Multi-Line Comments

java
public class Main {
	/*
	 * This block comment can span
	 * multiple lines of explanation.
	 */
	public static void main(String[] args) {
		System.out.println("See the block comment above.");
	}
}

Javadoc Comments

किसी class या method के ऊपर रखे Javadoc comments (/** ... */) को javadoc tool browsable HTML API documentation auto-generate करने के लिए picked up करता है -- यही कैसे Java की अपनी standard library docs बनाई जाती हैं।

उदाहरण: Javadoc Comments

java
public class Main {
	/**
	 * Adds two integers together.
	 * @param a first number
	 * @param b second number
	 * @return the sum of a and b
	 */
	static int add(int a, int b) {
		return a + b;
	}
	public static void main(String[] args) {
		System.out.println(add(2, 3));
	}
}

Commenting Out Code

Debugging के दौरान code को // या /* */ में wrap करना आपको तुरंत यह rule out करने देता है कि एक specific block एक bug का कारण है या नहीं, बाद में delete करके फिर type किए बिना।

उदाहरण: Commenting Out Code

java
public class Main {
	public static void main(String[] args) {
		System.out.println("Active line");
		// System.out.println("Disabled while debugging");
	}
}

Comment Best Practices

Stale comments जो अब उनके describe किए code से मेल नहीं खाते बिना comment से भी बदतर हैं, क्योंकि वे अगले reader को actively misguide करते हैं -- जब भी आप आस-पास logic बदलें उन्हें update या delete करें।

उदाहरण: Comment Best Practices

java
public class Main {
	public static void main(String[] args) {
		int price = 20; // price in dollars (kept in sync with the code beside it)
		System.out.println(price);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक /* ... */ comment किसी दूसरे /* ... */ के अंदर लिखना, क्योंकि block comments nest नहीं होते और पहला */ इसे खत्म कर देता है।
  2. /* बंद करना भूल जाना इसलिए file का बाकी हिस्सा एक comment बन जाता है।
  3. दूसरी languages की तरह comments के लिए # या -- उपयोग करना, जब Java // और /* */ उपयोग करती है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.