we understood Java by writing a couple of programs. In those
programs, you might have observed that sometimes we have used small letters and
sometimes capital letters. For example, we have written the class System
starting with a capital letter. You cannot write this class name as system or
SYSTEM. Since Java Is a case sensitive programming
language. It recognizes capital and small letters as different. So the
programmer should take care of upper and lower case while writing a program In
Java, But how to know where to use which case—upper or lower? For this purpose,
certain conventions (rules) are followed by JavuSoft people while naming the
variables, classes, methods, etc. These naming conventions should be followed
by every programmer in his programs for maintaining uniformity and also for
clarity of distinction between different elements of a program. These rules
also reduce the possibility of any spelling mistakes while writing the names of
variables, classes, etc. in the programs. For example, a Java program will not
compile, If main() method is written as:
public static void Main(string args[])
because capital M for
main(). Such errors can be eliminated if we follow the naming conventions.
Naming Conventions in
Java
Naming conventions specify the rules to be followed by a
Java programmer while writing the names of packages, classes, methods. etc.
Now, let us see some of the major naming conventions to be followed in Java.
1. A package represents a sub directory that contains a
group of classes and interfaces. Names of packages In Java are written small
letters as:
Ex: java.lang, java.io, javax.http.servlet…etc
2. A class specifies
the properties and actions of objects. An interface is also similar to a each
word of class names and interface names start with a capital letter as:
Ex: Integer, String, DataInputStream, Thread…etc
4. A class and an Interface contain methods and variables.
The first word of a method name is in small letters; then from second word
onwards, each new word starts with a capital letter as shown here:
Ex: println(), equalIgnoreCase(), toString()….etc
5. The naming convention for variables names is same as that
for methods as given here:
Ex: name, employNumber….etc
Q: if the same rule is
applied for both variables and
methods how can we distinguish between them?
A: A methods name
ends with a pair of simple braces(), it
can be distinguished easily from a
variable whose name will not have any braces.
6. Constants represent fixed values that cannot be altered.
For example, PI is a constant whose value is 22/7 or 3.1.4159. which is fixed.
Such constants should be written by using all capital letters as shown here:
MIN_VALUE,PI
7. All keywords should be written by using all small letters
as follows: public,private,this,super…etc
comments
Whenever we want to write a program, we should first think
about writing comments. What are comments? Comments are description about the
features of a program. This means that whatever we write in a program should be
described using comments. Why should we write comments? When we write comments,
we can understand what the program is doing as well as it helps others to
easily follow our code. This means readability and understandability of a
program will be more. If a program is understandable, then only can it be
usable in a software. If other members of the software development team cannot
understand our program, then they may not be able to use it in the project and
will reject it. So writing comments a compulsory in any program. Remember, it
is a good programming habit.
There are three types of comments in Java—single line, multi
line, and Java documentation.
Single Line comments: These comments are for
marking a single line as a comment. These comments start with double slash
symbol // and after this, whatever is written till the
end of the line is taken as a comment.
For example. //this represents comments.
Multi Line comments: These comments are used for representing
several lines as comments.These comments start with /* and end with */. In
between /* and * /, whatever is written is treated as a comment.
For example: /* we are learning comments concept in java.
This
is example for multi line comments in java */
Java documentation
comments: These comments start with /* * and end with */. These comments are used to provide description
for every feature in a Java program. This description proves helpful in the creation of a . httnl file called API
(Application Programming Interface) document. Java documentation comments
should be used before every feature in the program.
Ex: /** description for interfaces */
No comments:
Post a Comment