Comments 

Fundamental Programming Standards
  • Just as we saw in Perl, Java uses the standard syntax for defining the parts of a program. For example, all commands are ended with a semicolon(;) 
  •         int i = 5;
  • Similarly, all blocks of code are defined by curly braces such as in the following example:
  • if (i <10)
      {
      System.out.println("i is less than 5);
      }
  • This type of programming should be very familiar for you from yesterday.
Comments
  • Just as you could use the number sign (#) to let the Perl interpreter know that a line in the code was a comment, you can specify comments in Java. 
  • Actually, there are several ways to denote comments in Java. 
  • For single line comments, you can use the "//" notation. For example, consider the following code: 
  •                 // Initialize variables...
    int age = 28;   // Declare and initialize age
  • Single line comments can be defined on the same line as your code or on their own line 
  • To specify multiline comments, however, you will use the "/*" and "*/" combination such as in the following example: 
  • /*
    This is a sample class which is used to demonstrate
    the use of multi-line comments.  it was written by
    Selena Sol for the Web Programming Tutorial
    */
    
            // Define an Example class with a single
            // main() method which will print out
            // "Hello Cyberspace!" to standard output.
    
    public class Example
      {
      public static void main(String[] args)
        {
        System.out.println("Hello Cyberspace!");
        }
      }
    
    
  • As you can see, if you use the "//" notation and your comment spans multiple lines, you must begin each new line with a "//" combination. 
  • Finally, if you are writing comments which should be included as part of a javadoc document, you should use the "/**" "*/" combination such as in the following example: 
  • /*
    This is a sample class which is used to demonstrate
    the use of multi-line comments.  it was written by
    Selena Sol for the Web Programming Tutorial.  This comment
    does not appear in the java documentation
    */
    
            /** 
            Define an Example class with a single
            main() method which will print out
            "Hello Cyberspace!" to standard output.
            This comment appears in the javadoc.
            */
    
    public class Example
      {
      public static void main(String[] args)
        {
        System.out.println("Hello Cyberspace!");
        }
      }

Additional Resources:

Setting Your Development Environment
Table of Contents
Printing to Standard Output


Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory