Introduction
Java mein code likhna important hai, lekin use samajhna aur samjhana usse bhi zyada important hai. Isiliye comments ka use hota hai. Comments ka kaam hota hai developer ke thoughts ko explain karna — bina code ko affect kiye.
Java mein 3 tarah ke comments hote hain:
- Single-line comment
- Multi-line comment
- Documentation comment
Chaliye har ek ko detail mein samajhte hain with examples
1. Single-line Comments (//
)
Ye comment ek hi line mein likha jata hai. Java ise ignore kar deti hai during compilation.
Syntax:
// This is a single-line comment
int x = 10; // Variable x assign to 10
Use Kab Karein?
- Jab aapko chhoti explanation deni ho.
- Variable, method, condition ya logic ko briefly explain karna ho.
Multi-line Comments (/* ... */
)
Agar aapko zyada lines mein explanation likhna hai, to aap multi-line comment use kar sakte ho.
Syntax:
/*
This is multi-line comment
Yaha hum multiple lines mein kuch bhi likh sakte hain
Compiler ise ignore karega
*/
int y = 20;
Use Kab Karein?
- Jab aapko koi bada logic explain karna ho.
- Code ka part temporarily disable karna ho (debugging ke time helpful).
3. Documentation Comments (/** ... */
)
Ye comment kaafi special hota hai. Java ke Javadoc tool ke saath use hota hai documentation generate karne ke liye.
Syntax:
/**
* Ye method do numbers ko add karta hai
* @param a Pehla number
* @param b Dusra number
* @return Do numbers ka sum
*/
public int add(int a, int b) {
return a + b;
}
Use Kab Karein?
- Jab aap API ya public library bana rahe ho.
- Methods, classes, constructors ki proper documentation chahiye.
Bonus: Javadoc ka use kaise karein?
Command line mein likho:
javadoc MyClass.java
Isse HTML format mein documentation ban jata hai.
Final Tips
- Comments ka use code samjhane ke liye karo, code chhupane ke liye nahi.
- Clear aur concise likho — unnecessary comment se confusion hota hai.
- Good commenting = Good coding practice...
Comments