Java Coding Conventions

Naming Conventions, Data Types, Comments and Structures

© Ana Mills

Dec 4, 2008
Commenting, Ana Mills
Write better code by using the right data types, naming conventions, and comments.

Like many programming languages, Java has coding conventions to encourage uniform code. They may seem arbitrary at first, but using naming conventions, proper data types, and commenting on blocks rather than statements will prevent errors and explain program steps later on.

Naming Conventions

Java has several naming conventions that are strongly suggested to create orderly and readable code. Naming conventions are guidelines to write the unique identifiers of each element type:

  • Packages, collections of common classes, must begin with a prefix of lowercase ASCII letters from the top-level domain names: com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981. Example: edu.mit.cs
  • Class and interface names should be descriptive nouns in mixed case with the first letter of each word capitalized. Use acronyms only when they are more common than their alternatives. Example: class PrintName.
  • Method names must be verbs with the first letter in lowercase and the first letter of subsequent words in uppercase. Example getAge()
  • Variable names must be short and descriptive, and capitalized as are method identifiers. Use of names that begin with an underscore or dollar sign as well as single character names is discouraged.
  • Constants are declared with reserved words final and static and cannot be changed afterward. Example: final static Pi.

Data Types

Java elements store a variety of data, but a new element must declare its data type before it can be used. Java has two groups of data types: primitive data types and object reference data types. Primitive data types are the most common and consist of:

  • int or integer elements store any whole number between -2,147,483,648 and 2,147,483,648.
  • char or character elements store a single alphanumberic Unicode character. This includes all standard punctuation marks.
  • boolean stores only two values: true or false. These values are constants and not the same as 1 or 0, True or False, TRUE or FALSE, etc.
  • byte stores 1 byte or 8 bits of data, specifically integers from -128 to 127.
  • short stores 2 bytes or 16 bits of integer data from -32,768 to 32,767.
  • long stores 64 bits, any integer from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The int data type is normally sufficient for large numeric elements, but use the long data type when needed.
  • float is the first of the floating-point data types, which are used for numbers with decimal points. The float data type stores 4 bytes, from 1.40129846432481707e-45 to 3.40282346638528860e+38.
  • double is the second floating-point data type, used for larger decimals from 4.94065645841246544e-324d to 1.79769313486231570e+308d. Sun Microsystems recommends the java.math.BigDecimal class for precise numbers like currency rather than float or double.

Object reference data types are elements that store references to data rather than the data itself. A web page that contains a link to text rather than the text itself is a loose analogy for an object reference element.

Commenting on Code

Commenting provides clarity for both the author and future readers. Comments must concisely describe a block of code rather than individual statements. Longer programs gather more comments, but the interpreter ignores comments so as not to affect file size.

Insert a single line of comment by beginning the statement with two forward slashes:

//This is a comment. Every line after this will not be commented.

Insert a comment block with /* and */ :

/*

This is a block of comment.

Anything inside the symbols is commented.

*/

Statements & Blocks

Statements and blocks are common concepts in C-based languages and are the equivalents of sentences and paragraphs. A statement ends with a semicolon (;), which also begins the next statement. A block is a group of one or more statements enclosed in curly brackets {}.

Summary

As with human languages, Java recommends coding conventions to prevent sloppy programming. Using comments or researching the proper data type is annoying at first, but will save time later with troubleshooting and documenting.


The copyright of the article Java Coding Conventions in Javascript/Java Programming is owned by Ana Mills. Permission to republish Java Coding Conventions in print or online must be granted by the author in writing.


Commenting, Ana Mills
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo