Java Tutorial

Java Comments

Comments are for people. The compiler skips // lines and /* blocks */.

The compiler ignores comments

A comment is text you leave in the source so a human can follow the program. javac does not compile it. It does not change output. Use comments to record why a line exists, not to repeat what the code already shows.

Example

public class Main {
  public static void main(String[] args) {
    // This program greets the user once.
    System.out.println("Hello");
  }
}

Line comments with //

Two slashes start a comment that runs to the end of that line. You can put // on its own line or after a statement.

Example

public class Main {
  public static void main(String[] args) {
    int n = 10;  // starting inventory
    System.out.println(n);
    // System.out.println("skip this line while testing");
    System.out.println("still running");
  }
}

Block comments with /* */

/* starts a comment that can span several lines. */ ends it. Blocks do not nest. Prefer // on each line when you disable a chunk of code.

Example

public class Main {
  public static void main(String[] args) {
    /*
      Print a short header, then a number.
    */
    System.out.println("total");
    System.out.println(42);
  }
}

What to write

WeakUseful
count = count + 1; // increment countcount = count + 1; // skip the header row
price = price * 0.9; // multiplyprice = price * 0.9; // 10 percent loyalty discount

Javadoc: /** */

A comment that starts with /** just above a class or method is a Javadoc comment. Tools turn these into browsable documentation, and editors show them as pop-up help when you hover a method. You will see them everywhere in real Java.

Example

public class Main {
  /** Returns 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));
  }
}

Key Takeaways

  • // comments to the end of the line; /* ... */ spans lines.
  • The compiler ignores every comment — they are for humans.
  • Explain why, not what the code obviously already says.
  • /** ... */ Javadoc above a method becomes documentation and editor hover-help.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Chemistry

Annotate a reagent

A comment is not executed. It tells the next person which bottle and concentration the number came from.

In a lab notebook the same habit stops someone treating 0.100 as volume when it was molarity.

Example

public class Main {
  public static void main(String[] args) {
    // HCl stock: 0.100 mol/L
    double conc = 0.100;
    System.out.println(conc);
  }
}

FAQ: Java Comments

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java comments examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java comments in this Java Java lesson (Java Comments).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.