Presentation is loading. Please wait.

Presentation is loading. Please wait.

Anatomy of a Java Program

Similar presentations


Presentation on theme: "Anatomy of a Java Program"— Presentation transcript:

1 Anatomy of a Java Program
Comments Reserved words Modifiers Statements Blocks Classes Methods The main method

2 a) Comments Comments ignored during program execution
Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Comments start with: // Traditional comments: /* ... */

3 b) Reserved Words Words that have a specific meaning to the compiler
Cannot be used for other purposes in the program. Key words examples are: Key words are lower case (Java is a case sensitive language). goto and const are C++ keywords, but not currently used in Java. If they appear in Java, Java compilers will produce error messages. boolean continue return public class static void int double private protected package

4 c) Modifiers Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public datum, method, or class can be accessed by other programs. A private datum or method cannot be accessed by other programs.

5 d) Statements A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;).

6 e) Blocks A pair of braces in a program forms a block that groups components of a program.

7 f) Classes The class is the essential Java construct.
A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. A program is defined by using one or more classes.

8 g) Method System.out.println is a method.
Method is a collection of statements that performs a sequence of operations to display a message on the console. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message.

9 h) main () Method public static void main(String[] args) {
The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements; }

10 Programming Style and Documentation in Java
Appropriate comments and comments style Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class, lecture’s name, date, and a brief description of your code at the beginning of the program.

11 Comments Style A “block” comment is placed between /* and */ marks:
A single-line comment goes from // to the end of the line: /* Exercise 5-2 for Java Methods Author: Miss Brace Date: 3/5/2010 Rev */ weight *= ; // Convert to kilograms

12 Javadoc Comments (cont’d)
/** indicates a javadoc comment /** * Returns total sales from all vendors; * sets <code>totalSales</code> * to 0. * * @return total amount of sales from all vendors */ Can use HTML tags Common style

13 b) Naming Conventions Choose meaningful and descriptive names.
Variables and method names: Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea.

14 Cont.. Class names: Constants:
Capitalize the first letter of each word in the name. For example, the class name ComputeArea. Constants: Capitalize all letters in constants, and use underscores to connect words. For example, the constant PI and MAX_VALUE

15 c) Proper Indentation and Spacing
Indent two spaces. Spacing Use blank line to separate segments of the code.

16 d) Block Styles Use end-of-line style for braces.


Download ppt "Anatomy of a Java Program"

Similar presentations


Ads by Google