Vous êtes sur la page 1sur 16

Anatomy of a Java

Comments
Program
Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The main method
1

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: /* ... */

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:

public
class
static

void
int
double

boolean
continue
return

private
protected
package

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.

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.

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 (;).

e) Blocks
A pair of braces in a program forms a block

that groups components of a program.

public class Test {


public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

Class block
Method block

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.

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.

h) main () Method
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;
}

Programming Style and


Documentation in Java
a) 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, lectures name, date,

and a brief description of your code at the


beginning of the program.

Comments Style
A block comment is placed between /* and

*/ marks:

/* Exercise 5-2 for Java Methods


Author: Miss Brace
Date: 3/5/2010
Rev. 1.0
*/

A single-line comment goes from // to the end

of the line:
weight *= 2.2046;

// Convert to kilograms

Javadoc Comments
(contd)
/** indicates a javadoc
comment

/**
*
*
*
*
*
*/

Returns total sales from all vendors;


sets <code>totalSales</code>
to 0.
@return total amount of sales from all vendors

Common
style

Can use
HTML tags

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.

Cont..
Class names:
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

c) Proper Indentation and


Spacing
Indentation

Indent two spaces.

Spacing
Use blank line to separate segments of the

code.

15

d) Block Styles

Use end-of-line style for braces.

Next-line
style

public class Test


{
public static void main(String[] args)
{
System.out.println("Block Styles");
}
}

public class Test {


public static void main(String[] args) {
System.out.println("Block Styles");
}
}

16

End-of-line
style

Vous aimerez peut-être aussi