java anatomy
DESCRIPTION
Object Oriented Programming -Java AnatomyTRANSCRIPT
1
Anatomy of a Java ProgramCommentsReserved wordsModifiersStatementsBlocksClassesMethodsThe main method
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 WordsWords that have a specific meaning to the compilerCannot 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.
•public
•class
•static
•void
•int
•double
•boolean
•continue
•return
•private
•protected
•package
c) ModifiersJava 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) StatementsA 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) BlocksA 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) ClassesThe 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) MethodSystem.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 () MethodThe 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 styleInclude 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.
Comments StyleA “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. 1.0 */
weight *= 2.2046; // Convert to kilograms
Javadoc Comments (cont’d)
/** * Returns total sales from all vendors; * sets <code>totalSales</code> * to 0. * * @return total amount of sales from all vendors */
/** indicates a javadoc comment
Can use HTML tags
Common style
b) Naming ConventionsChoose 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
15
c) Proper Indentation and SpacingIndentation
Indent two spaces.
Spacing Use blank line to separate segments of the
code.
16
d) Block StylesUse end-of-line style for braces.
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"); } }
End-of-line style
Next-line style