JAVATM- The Beginnings

Comments and Indentation

In the Hello World lesson, we wrote the following program:

public class HelloWorld

{

            public static void main(String[] args)

            {

                System.out.println("Hello World");

            }

        }

Programming languages can often be difficult to read, and it is very helpful to include information within a source file which tells the reader what the program does. This is done using comments.

Comments are plain text inserted into your source files which help document your programs. Comments are ignored by the Java compiler. You are highly encouraged to comment your work.

Section 1 - Defining Java-style Comments

Java-style comments begin with two consecutive forward slash characters. All characters on the current line before the forward slashes are processed by the compiler. All characters on the current line that follow the forward slashes are ignored by the compiler.

Let’s add a Java-style comment to the class definition.

public class HelloWorld // This program prints Hello World

{

            public static void main(String[] args)

            {

                System.out.println("Hello World");

            }

        }

The characters “public class HelloWorld” are processed by the compiler, beginning the class definition. The forward slashes inform the compiler that all following characters are comments, so the characters “This program prints Hello World” are ignored by the compiler.

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.



Back to ralphlecessi.com