Java में Comments
In this page:
// single-line comment
/* multi-line
comment */
/** Javadoc comment */
Single-Line Comments
एक // comment उस point से line के आखिर तक चलता है, इसे किसी tricky logic के टुकड़े को इसके बगल में explain करने वाले छोटे, one-line notes के लिए ideal बनाते हुए।
उदाहरण: Single-Line Comments
public class Main {
public static void main(String[] args) {
int total = 100; // running total for the cart
System.out.println(total);
}
}
Login to try C/C++/Java/PHP code in the editor
Multi-Line Comments
एक /* ... */ block comment कई lines तक span कर सकता है, जो code के एक chunk को temporarily disable करने या एक complex method के ऊपर एक लंबा explanation लिखने के लिए उपयोगी है।
उदाहरण: Multi-Line Comments
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.");
}
}
Login to try C/C++/Java/PHP code in the editor
Javadoc Comments
किसी class या method के ऊपर रखे Javadoc comments (/** ... */) को javadoc tool browsable HTML API documentation auto-generate करने के लिए picked up करता है -- यही कैसे Java की अपनी standard library docs बनाई जाती हैं।
उदाहरण: Javadoc Comments
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));
}
}
Login to try C/C++/Java/PHP code in the editor
Commenting Out Code
Debugging के दौरान code को // या /* */ में wrap करना आपको तुरंत यह rule out करने देता है कि एक specific block एक bug का कारण है या नहीं, बाद में delete करके फिर type किए बिना।
उदाहरण: Commenting Out Code
public class Main {
public static void main(String[] args) {
System.out.println("Active line");
// System.out.println("Disabled while debugging");
}
}
Login to try C/C++/Java/PHP code in the editor
Comment Best Practices
Stale comments जो अब उनके describe किए code से मेल नहीं खाते बिना comment से भी बदतर हैं, क्योंकि वे अगले reader को actively misguide करते हैं -- जब भी आप आस-पास logic बदलें उन्हें update या delete करें।
उदाहरण: Comment Best Practices
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक
/* ... */comment किसी दूसरे/* ... */के अंदर लिखना, क्योंकि block comments nest नहीं होते और पहला*/इसे खत्म कर देता है। /*बंद करना भूल जाना इसलिए file का बाकी हिस्सा एक comment बन जाता है।- दूसरी languages की तरह comments के लिए
#या--उपयोग करना, जब Java//और/* */उपयोग करती है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: