HomepageTeaching PageResearch PageResources Page

Java Tutorial for Python Programmers

Comments

Unlike Python, Java has two different types of comments: inline (similar to Python comments) and block comments.

Inline 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
        

Block Comments

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?
        

Tutorial Table of Contents

  1. Intro
  2. Java Files and Classes
  3. Running Java Code
  4. Statements and Printing
  5. Comments
  6. Types and Variables
  7. Conditionals
  8. Static Methods
  9. Strings
  10. Loops
  11. Arrays
  12. ArrayLists
  13. Classes
  14. Subclasses
  15. Generic Types
  16. Javadoc
  17. Final Exam
  18. What's Next?