Presentation is loading. Please wait.

Presentation is loading. Please wait.

Identifiers.

Similar presentations


Presentation on theme: "Identifiers."— Presentation transcript:

1 Identifiers

2 Introduction An identifier is a collection of certain characters that could mean a variety of things There are some existing Java identifiers sqrt String Integer System in out We can make up new identifiers test1 x1 aNumber MAXIMUM A_1

3 Identifiers

4 Valid Identifiers Identifiers have from 1 to many characters:
'a'. .'z', 'A'..'Z', '0'..'9', '_', $ Identifiers start with letter a1 is legal, 1a is not can also start with underscore or dollar sign: _ $ Java is case sensitive. A and a are different.

5 Reserved Identifier Reserved identifier: An identifier with a pre-defined meaning that can not be changed Java reserved identifiers not a complete list boolean default for new break do if private case double import public catch else instanceOf return char extends int void class float long while

6 Identifiers Names given to variables, objects, methods
Must not be a Java keyword See Appendix B for list of keywords May begin with a letter or the underline character _ Followed by any number of characters, digits, or _ (note, no blanks) Identifiers should be well chosen use complete words (even phrases) this helps program documentation

7 Conventions for Identifiers
Classes Names given in lowercase except for first letter of each word in the name Variables Same as classes, except first letter is lowercase Constants All caps with _ between words Methods like variable names but followed by parentheses

8 Identifiers Keywords Lexical elements (or identifiers) that have a special, predefined meaning in the language Cannot be redefined or used in any other way in a program Ex: public, private, if, class, throws See p. 32 in LL for complete list

9 Identifiers Other Identifiers Defined by programmer
Java API defines quite a few for us e.g. System, Scanner, String, out are used to represent names of variables, methods and classes Cannot be keywords We could redefine those defined in Java API if we wanted to, but this is generally not a good idea Java IDs must begin with a letter, followed by any number of letters, digits, _ (underscore) or $ characters Similar to identifier rules in most programming languages

10 Example for Java Identifiers
package javaidentifiers; /** * paul */ public class Main { /** args the command line arguments */ public static void main(String[] args)

11 Program { // TODO code application logic here System.out.println(“Must start with a letter, ($), or connecting character (_)”); System.out.println(“Can not start with a number”); System.out.println(“No limits to the amount of characters for a identifier”);

12 Program System.out.println(“You can not use a Java Keyword”); System.out.println(“Case sensitive – IDENTIFER is different from identifier”); System.out.println(“Legal – see code”); int _$a; int x; int $j; int _____iiii;

13 Program System.out.println(“Illegal”); System.out.println(“int :6 – breaks rule 1″); System.out.println(“int ;J – breaks rule 1″); System.out.println(“int 6pp – breaks rule 2″); System.out.println(“int else – breaks rule 4″); }

14 The End ….. Thank You …..


Download ppt "Identifiers."

Similar presentations


Ads by Google