350 likes | 557 Views
Identifiers. Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. Identifiers should help to document what a classes, variables, or methods are used for. Upper and lower case letters are different in Java.
E N D
Identifiers • Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. • Identifiers should help to document what a classes, variables, or methods are used for. • Upper and lower case letters are different in Java. • ThisOne is not the same as thisOne. • Identifiers should not include punctuation and can not include spaces.
Identifiers • A good programming standard for identifiers composed of multiple words is to capitalize the first character of the additional words. • The case of the first letter of the first word will depend upon the use of the identifier. • For Example, myCar, or bobAndSusieAreFriends
Identifiers • Class names begin with a capital letter. • This standard makes it easier to understand which identifiers represent classes. • In the MyFirstApplication we have two class names: • MyFirstApplication and MainWindow
Identifiers • Variable (object names) and method names begin with a lower case letter. • In MyFirstApplication we have one object: • mainWindow • Note that mainWindow is different from MainWindow. • In MyFirstApplication we have one method: • main(…)
Creating a Java Program • The first step to creating a Java program is to define a class containing the main method. • The MyFirstApplication program on page 39 of Wu is an example of a Java class containing a main method. • This Java class definition can be thought of as the driver class or the ring leader class.
MyFirstApplication MyFirstApplication main(String[] args)
Declaring Objects • In order to create objects to work within a Java program they must first be declared. • The object declaration includes the class name that the object belongs to and the variable name for the object. • The format for declaring an object is: <class name> <object(variable) name> ; • The class must be defined before you can declare the object.
Declaring Objects • Think about our cars. • Cars was the class name and we had three instances of the this class. • To declare each of the cars we had created we would type: Cars clintsPorsche; Cars jessicasBMW; Cars johnsLamborghini;
Declaring Objects • The MyFirstApplication program in Wu defines one object called mainWindow of the class type MainWindow. MainWindow mainWindow; • Note the differences in the text above.
Declaring Objects • Declaring an object only creates a name to refer to the object, it does not create the instance of that object. • In order to use the named object instance, the object must be instantiated (or created).
Object Instantiation • Object instantiation (creation) actually creates a copy of the class and stores it in memory with the name defined in the object declaration. • The format for object instantiation is: <object name> = new <class name> (<arguments>);
Object Instantiation <object name> = new <class name> (<arguments>); • The <object name> is the name for the object as you declared it in your program. • The <class name> is the name of the class that this object has been declared to belong to. • The <arguments> represent possible data that may be required to create the object instance.
Object Instantiation • We declared three types of Cars: Cars clintsPorsche; Cars jessicasBMW; Cars johnsLamborghini; • To instantiate the instances of the Cars: clintsPorsche = new Car(); jessicasBMW = new Car(); johnsLamborghini = new Car();
Object Instantiation • The MyFirstApplication instantiates the object mainWindow on line two of the main method: mainWindow = new MainWindow();
MyFirstApplication MyFirstApplication mainWindow MainWindow Main(String[] args)
Message Passing • Once an object exists, the program can begin to send messages to the object. • The message must include the object to which the message is addressed, the name of the task to be completed, and any data required to complete the task. • In Java speak: <object name>.<method name>(<arguments>);
Message Passing • Let’s create messages for our cars. • We need to know how much gas Clint has. clintsPorsche.getGasLevel(); • We want to turn on John’s hazards. johnsLamborghini.turnOnHazards();
Message Passing • Now lets look at the last line of the main method in MyFirstApplication. mainWindow.setVisible(true);
Method Declarations • The method declaration provides the function implementation in the program. • The basic format of a method declaration is: <modifiers> <return type> <method name> (<parameters>) { <method body> }
Method Declarations <modifiers> <return type> <method name> (<parameters>) { <method body> } • Modifiers represent terms that determine what kind of method is declared. (Chap 4) • The Return Type is the data type of the value returned by the method. • If the method does not return a value this value is void.
Method Declarations <modifiers> <return type> <method name> (<parameters>) { <method body> } • The method name should identify the task to be completed by the method • The parameters are the data that the method requires to complete the task. • The method body includes the code statements that implement the task.
Method Declarations • Let’s look at the main method in MyFirstApplication.
Class Declarations • Java programs are composed of one or more classes. • Some classes are already defined in Java but we also need to be able to define our own classes. class <class name> { <class member declarations> }
Class Declarations class <class name> { <class member declarations> } • Every class must have a unique name. • The class member declarations include method definitions and variable (data value).
Class Declarations • One class in every program must include the main method. • This class is the driver class. • The main method is the first method that is called when a program is executed. • Let’s look at the MyFirstApplication program.
Import Statement • The import statement is used to allow a class definition to access predefined Java classes. • Related predefined Java classes are stored in packages. • We will use the package called javabook provide by Wu. • In order to import predefined classes: import <package name>.<class name>;
Import Statement import <package name>.<class name>; • The package name represents the the particular package to look into for the class name. • java.* or javabook.* • The class name can be a specific class or every class in the package. import javabook.MainWindow; import javabook.*;
Comments • Providing a description of what the program does is called commenting the code. • Commenting is used to provide an overall description of the class and what it does, who the author is, when the class was created and modified, etc. – Header comments • Commenting is also used throughout your code to provide a description of a method, as well as to provide descriptions of complicated code. • The use of meaningful variable and method names helps “comment” the code.
Comments • Java provides three ways to comment code. • MyFirstApplication includes two types. • The /* …. */ is used for multiple line comments. • The // is used for a single line comment. • The final type is javadoc.
Comments • The javadoccomment appears before the class declaration. • The javadoc comment begins with /** rather than /*. • The javadoc comments end with */ • The benefit of using a javadoc comment is that the programmer can use a tool that will automatically create a web page from the comment. • This is only true for javadoc comments.
Comments • Comments can not be put inside of another comment. • Comments are for the programmer only, they do not represent executable code. • The computer ignores comments.
Commenting Rules of Thumb • Do not state the obvious for self-evident information. For example if the code is: • x = x + y; // it is not necessary that you are adding x and y and storing the result in x. • Provide a brief statement about each variable and a brief description for each method.
Commenting Rules of Thumb • Do not comment code that is bad, if you are writing a long paragraph to describe what is going on, then re-write the code to make it simpler. • Make sure your comments agree with the code. • Use comments to Clarify not confuse the unsuspecting reader. Use them to help the reader.
Creating a Java Program • The filename.class file is interpreted by the Java interpreter every time it is run. Only Once Every Time Java Interpreter Java Compiler Editor Filename.java Filename.class