Tuesday, 4 August 2015

Java Tutorial : Source Code Comments

Want to write clean, professional code?

Subscribe to Ram N Java for beginner-friendly lessons, clean coding best practices, and complete Java tutorials.

Subscribe Now

Why Source Code Comments Matter

Writing functional code is only half the battle. Making your code readable and easy to maintain by others (and yourself in six months) is equally essential. In Java, source code comments let you document logic, describe functionality, and leave explanatory notes directly in your files without affecting how your program runs.

Does the Java Compiler Read Comments?

No. When you compile your source code using javac, the compiler strips out every single comment. They are completely ignored during compilation and never appear inside the resulting .class bytecode file. This means adding helpful comments will never slow down your application.

The Three Types of Java Comments

Java provides three distinct comment formats depending on your documentation needs:

  • 1. Single-Line Comments (//):
    Used for brief, one-line remarks. Anything from the double slashes to the end of that specific line is treated as a comment.
  • 2. Multi-Line Comments (/* ... */):
    Ideal for longer descriptions spanning multiple lines or temporarily disabling blocks of test code. Everything between /* and */ is ignored.
  • 3. Documentation Comments (/** ... */):
    Special comments placed before classes, interfaces, or method declarations. The javadoc tool reads these blocks to automatically generate structured HTML API documentation.

Code Example: All Three Comments in Action

Take a look at how each type of comment is structured inside a Java class:

/**
 * The Calculator class provides basic mathematical operations.
 * This is a Javadoc documentation comment.
 * 
 * @author Ram
 */
public class Calculator {

    /*
     * This is a multi-line comment.
     * It explains that the add method takes two integer
     * parameters and returns their combined total.
     */
    public int add(int firstNumber, int secondNumber) {
        // Calculate the sum of two inputs (Single-line comment)
        int sum = firstNumber + secondNumber;
        return sum;
    }
}

Best Practices for Commenting Code

  • Explain the "Why", Not Just the "What": Don't explain obvious syntax like int x = 5; // sets x to 5. Instead, explain the intent behind non-obvious business logic.
  • Keep Comments Updated: An outdated comment that contradicts the code is worse than no comment at all. Update your documentation whenever you update your implementation.
  • Use Javadoc for Public APIs: Whenever you build libraries, reusable helper methods, or public interfaces, write clean documentation tags like @param and @return.

Recommended Java Tutorials From Our Channel

Explore these related tutorials from our channel to keep improving your Java programming skills:


Click here to watch in Youtube :
https://www.youtube.com/watch?v=GgaMMPeO91o&list=UUhwKlOVR041tngjerWxVccw

HelloWorldApp.java
/**
 * The HelloWorldApp class implements an application that simply prints
 * "Hello World!" to standard output.
 */
class HelloWorldApp
{
    public static void main(String[] args)
    {
        /* Print Hello World! */
        System.out.println("Hello World!"); // Display the string.
    }
}
Output
Hello World!

Click the below Image to Enlarge

To Download HelloWorldAppComments Project Click the below link
https://sites.google.com/site/javaee4321/java/HelloWorldAppComments.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Tutorials