Unlike Python, Java has two different types of comments: inline (similar to Python comments) and block comments.
Java uses //
to indicate the start of an inline comment, which works exactly the same as #
in Python. Everything after the two slashes is ignored by Java:
age = 5; //age of the gorilla in years
Sometimes it's convenient to have a comment that spans more than one line. Instead of adding //
to the beginning of each line, add /*
before the first line and */
after the end of the last line:
/* The next five lines sort all the apes, then either: - removes the biggest ape if there are more than seven, or - adds two apes if there are enough bananas. */
Block comments can be very useful to temporarily "comment out" code that you are considering deleting. They can also be used to temporarily comment out a piece of a line of code:
age = 2 * otherAge /* + 1 */; //TODO: need to offset by 1?