Presentation is loading. Please wait.

Presentation is loading. Please wait.

LESSON 3 - Identifiers JAVA PROGRAMMING. Identifiers All the Java components —classes, variables, and methods— need names. In Java these names are called.

Similar presentations


Presentation on theme: "LESSON 3 - Identifiers JAVA PROGRAMMING. Identifiers All the Java components —classes, variables, and methods— need names. In Java these names are called."— Presentation transcript:

1 LESSON 3 - Identifiers JAVA PROGRAMMING

2 Identifiers All the Java components —classes, variables, and methods— need names. In Java these names are called identifiers, and, there are rules for what constitutes a legal Java identifier. Beyond what's legal, Java programmers (and Sun) have created conventions for naming methods, variables, and classes.

3 Rules for naming Identifiers 1. Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number! 2. After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers. 3. Identifier can be any length. In practice, there is no limit to the number of characters an identifier can contain. 4. You can't use a Java keyword as an identifier. (See “Java Keywords,” for a list of reserved words). 5. Identifiers in Java are case ‐ sensitive; amoud and Amoud are two different identifiers. 6. Identifier cannot be true, false or null.

4 Java keywords abstract elselongsynchronizedboolean breakbytecaseextendsfinal finallynative newpackageprivate thisthrow throws transient catch char class float forGoto if protected publicreturn try void volatileconst continue default doimplements import instanceof intShort static strictfp super while double Interface switch

5 Self Test 1 (Ex 6 Ques 1 pg 29) Which of the following are valid Java identifiers? If invalid, give reason: i. Double ii. 2CS iii. Big_N iv. $67 v. Boolean vi. _number

6 Which of the following are valid Java identifiers? If invalid, give reason: i. Int ii. public iii. private iv. AMOUD v. 2*K vi. var one

7 Sun's Java Code Conventions The naming standards that Sun recommends: Classes and interfaces The first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase (a format that's sometimes called "camelCase"). – For classes, the names should typically be nouns. For example: Animal Account PrintWriter – For interfaces, the names should typically be adjectives like Runnable Serializable

8 Sun's Java Code Conventions Methods The first letter should be lowercase, and then normal camelCase rules should be used. – In addition, the names should typically be verbnoun pairs. For example: getBalance doCalculation setCustomerName calcArea

9 Sun's Java Code Conventions Variables Like methods, the camelCase format should be used, starting with a lowercase letter. Sun recommends short, meaningful names, which sounds good to us. Some examples: – nama – buttonWidth – accountBalance – myString

10 Sun's Java Code Conventions Constants Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators: MIN_HEIGHT

11 Self Test 2 (Ex 6 Ques 4 pg 30) Which of these identifiers violate the naming convention for class name? i. m2t5 ii. BigCircle iii. smallTriangle iv. CPA v. mylittleprogram

12 Self Test 2 (Ex 6 Ques 4 pg 30) Which of these identifiers violate the naming convention for class name? i. Sample Program ii. Customer iii. 3Circles iv. Lab1Ex6 v. ThisIsLongProgramName

13 Programming Style and Documentation Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles

14 Appropriate Comments 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. Include your name, class section, instructor, date, and a brief description at the beginning of the program.

15 Proper Indentation and Spacing Indentation – Indent two spaces. Spacing – Use blank line to separate segments of the code.

16 Block Styles Use next-line style for braces. public class Test { public static void main(String[] args) { System.out.println("Block Styles"); }

17 Block Styles Use end-of-line for braces. public class Test { public static void main(String[] args) { System.out.println("Block Styles"); }

18 Self Test 3 Given the following program. Reformat the program according to the programming style and documentation guidelines. 1. public class Cubaan 2. {public static void main (String[] args) 3. {double value1 = 123.56789; 4. System.out.print("Testing :"); 5. System.out.printf("%6.3f", value1);}}

19 END OF LESSON 3 THE END


Download ppt "LESSON 3 - Identifiers JAVA PROGRAMMING. Identifiers All the Java components —classes, variables, and methods— need names. In Java these names are called."

Similar presentations


Ads by Google