0% found this document useful (0 votes)
2 views

Program layout, naming, and comments

The document outlines the structure of a Java program, emphasizing that it consists of one or more classes stored in .java files, with specific naming conventions for variables, methods, and constants. It also explains the importance of comments in Java, detailing three types: single-line, multi-line, and Javadoc comments, which help document the code without affecting its execution. Adhering to these conventions and practices enhances code readability and maintainability.

Uploaded by

alfredjoso847
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program layout, naming, and comments

The document outlines the structure of a Java program, emphasizing that it consists of one or more classes stored in .java files, with specific naming conventions for variables, methods, and constants. It also explains the importance of comments in Java, detailing three types: single-line, multi-line, and Javadoc comments, which help document the code without affecting its execution. Adhering to these conventions and practices enhances code readability and maintainability.

Uploaded by

alfredjoso847
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Program Layout

● A Java program typically consists of one or more Java classes.


● Each Java class is stored in a separate file with a .java extension, and the file
name must match the name of the public class in the file.
● Here's an example Java program that contains one class:

public class HelloWorld


{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}

● 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:

public class Example {


// This is a single-line comment

/* This is a
multi-line comment */

/**
* This is a Javadoc comment. It is used to document
* the class or method it precedes.
*/

public static void main(String[] args) {


// This is a single-line comment

/* This is a

2
multi-line comment */

System.out.println("Hello, world!"); // This is an inline comment


}
}

● In this example, there are three types of comments:


o Single-line comments: Single-line comments start with two forward
slashes (//) and continue until the end of the line. They are used to
provide a short explanation of the code on the same line.
o Multi-line comments: Multi-line comments start with /* and end with */.
They can span multiple lines and are used to provide a more detailed
explanation of the code.
o Javadoc comments: Javadoc comments start with /** and end with */.
They are used to provide documentation for the class or method that
they precede. Javadoc comments are parsed by the javadoc tool, which
generates documentation in HTML format.

You might also like