- Coding Standards(CS) and Naming conventions(NC) are suggestions given by sun(Oracle).
- CS and NC help developers to develop projects with more readability and understandability
Why Coding Standards?
- A program is written once , but read many times
- During debugging
- When adding to the program
- When updating the program
- When trying to understand the program
- Anything that makes a program readable and understandable saves lots of time , even in the shot run.
Naming Conventions:
1.Naming a class:
- Class name should be "noun", because it represents things
- Class name should be in title case , means Every word first letter should be capital letter.
- This is also known as UpperCamelCase.
- Choose descriptive and simple names.
- Try to avoid acronyms and abbreviations.
- package com.instanceofjava
- public JavaDemo{
- }
2.Naming a Variable:
- Variable name also should be noun, because it represents values.
- Variable name should be start with small letter.
- Use lowerCamelCase.
- Example: firstName, lastName, age.
- For final variable all its letters should be capital and words must be connected with "_".
- Example: MAX_COUNT, MIN_COUNT.
- package com.instanceofjava
- public JavaDemo{
- String topicName;
- String post;
- int postNumber;
- final static int MIN_WORDS=300;
- }
3.Naming Method:
- Method name should be verb because it represents action.
- Use lowerCamelCase for naming methods
- Example: setFirstName(), getFirstName(), getLastName().
- package com.instanceofjava
- public JavaDemo{
- String topicName;
- public void setTopicName(String topicName) {
- this.topicName=topicName;
- }
- public void getTopicName() {
- return topicName;
- }
- }
4.Naming a package:
- All letters should be small
- like in java io, util, applet
- Example com.instanceofjava
- package com.instanceofjava
- public JavaDemo{
- }
- If you do not follow all above naming conventions compiler wont any Error or exception.
- But naming conventions are useful in project and need to follow.
- But we need to follow some identifier rules as follows.
Identifiers:
- Identifier is the name of the basic programming elements.
- class names, methods names , object names and variables names are identifiers
- If we define a basic program without a name , compiler throws compile time error.
- Compilation Error : <identifier> expected.
- if we take a class without name then compile time error will come : Syntax error on token "class", Identifier expected after this token.
- package com.instanceofjava
- public { // Syntax error on token "class", Identifier expected after this token.
- }
Rules in defining identifier:
- While defining identifier we must follow bellow rules, else it leads to compile time error.
1.Identifier should only contain :
- Alphabets(a -z and A-Z)
- Digits (0-9)
- Special Characters ( _ and $)
2.Identifier should not start with a digit:
- A digit can be used from second character onwards.
- Example :
- 1strank : throws error
- No1Rank : works fine
3.Identifier should not contain special characters except "-" and "$":
- Example :
- first_name : works fine
- first#name : throws error
4.Identifier is case sensitive :
- identifier is case sensitive
- For example a and A are different a!=A.
5.keyword cannot be used as user defined identifier :
- we can not use keywords as our identifiers
- For example int class; // Syntax error on token "class", invalid VariableDeclarator.
Note:
- we can use predefined class names as identifiers
- example : int String;
- There is no limit in identifier length.
Java comments:
- A description about a basic programming element is called comment.
- Comments are meant for developer, to understand purpose.
Types of comments:
- Java supports 3 types of comments
- Single line comments - //
- Multiline comments - /* */
- Document comment - /** */
No comments