Java Anatomy
-
Upload
yeshwini-ramasamy -
Category
Documents
-
view
215 -
download
0
description
Transcript of Java Anatomy
![Page 1: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/1.jpg)
1
Anatomy of a Java ProgramCommentsReserved wordsModifiersStatementsBlocksClassesMethodsThe main method
![Page 2: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/2.jpg)
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: /* ... */
![Page 3: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/3.jpg)
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
![Page 4: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/4.jpg)
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.
![Page 5: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/5.jpg)
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 (;).
![Page 6: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/6.jpg)
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
![Page 7: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/7.jpg)
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.
![Page 8: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/8.jpg)
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.
![Page 9: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/9.jpg)
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;}
![Page 10: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/10.jpg)
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.
![Page 11: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/11.jpg)
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
![Page 12: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/12.jpg)
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
![Page 13: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/13.jpg)
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.
![Page 14: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/14.jpg)
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
![Page 15: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/15.jpg)
15
c) Proper Indentation and SpacingIndentation
Indent two spaces.
Spacing Use blank line to separate segments of the
code.
![Page 16: Java Anatomy](https://reader036.fdocuments.in/reader036/viewer/2022081515/5695d00e1a28ab9b0290c331/html5/thumbnails/16.jpg)
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