Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 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

3 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

4 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(…)

5 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.

6 MyFirstApplication main(String[] args)

7 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: ; –The class must be defined before you can declare the object.

8 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;

9 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.

10 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).

11 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: = new ( );

12 Object Instantiation = new ( ); The is the name for the object as you declared it in your program. The is the name of the class that this object has been declared to belong to. The represent possible data that may be required to create the object instance.

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

14 Object Instantiation The MyFirstApplication instantiates the object mainWindow on line two of the main method: mainWindow = new MainWindow();

15 MyFirstApplication Main(String[] args) mainWindow MainWindow

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

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

18 Message Passing Now lets look at the last line of the main method in MyFirstApplication. mainWindow.setVisible(true);

19 Method Declarations The method declaration provides the function implementation in the program. –The basic format of a method declaration is: ( ) { }

20 Method Declarations ( ) { } –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.

21 Method Declarations ( ) { } –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.

22 Method Declarations Let’s look at the main method in MyFirstApplication.

23 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 { }

24 Class Declarations class { } –Every class must have a unique name. –The class member declarations include method definitions and variable (data value).

25 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.

26 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. ;

27 Import Statement import. ; –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.*;

28 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.

29 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.

30 Comments The javadoc comment 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.

31 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.

32 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.

33 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.

34 Creating a Java Program The filename.class file is interpreted by the Java interpreter every time it is run. Editor Filename.javaFilename.class Java Compiler Java Interpreter Only Once Every Time


Download ppt "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."

Similar presentations


Ads by Google