Program layout, naming, and comments
Program layout, naming, and comments
● The first line of the program declares a public class called "HelloWorld". The
public keyword indicates that this class can be accessed by other classes.
● The body of the class is enclosed in curly braces {}.
● The second line declares a public static method called "main". This is the entry
point of the program. The static keyword indicates that this method belongs to
the class itself, rather than to instances of the class.
● The main method takes an array of strings called "args" as a parameter. This
parameter is used to pass command-line arguments to the program.
● The body of the main method is enclosed in curly braces {}.
● The third line of the main method calls the System.out.println method to print
the string "Hello, world!" to the console.
Naming conventions
● Sticking to Java naming conventions makes your programs easy to read
● Make sure you use straightforward names for variables, methods and constants
● Names are case sensitive in Java
1
● Common naming conventions:
o Use small caps for variables and methods. If a variable or method name
has more than one word, combine the words without any concatenating
character. The first word in the compound name should begin with a small
cap and the second word should begin with a capital letter. E.g., price,
calculateTax, inflationRate
o Capitalize the first letter of each word in a class name. E.g., Mammals
o Capitalize each word in a constant e.g., MAX_VALUE
● Note: Remember to indent your code to make it clean and easy to read.
Comments
● Comments in Java are used to provide information about the code to other
programmers or to remind yourself about what the code does.
● Comments are not executed by the compiler and do not affect the program's
output. They are simply ignored by the compiler.
● Consider this example of comments in Java:
/* This is a
multi-line comment */
/**
* This is a Javadoc comment. It is used to document
* the class or method it precedes.
*/
/* This is a
2
multi-line comment */