What is the convention for writing variables in Java?

Question:

In the case of variable names with a single word, it is normal to write them as follows:

int entero;
double doble;
String cadena;

But in the case of variables that use two words, how should they be written? I have only used two forms:

  • Option 1:

    int numero_entero;

    double numero_doble;

  • Option 2:

    int numeroEntero;

    double numeroDoble;

I've also noticed that some variables that Java libraries have are capitalized and separated by _ :

Foo.ANCHURA_MAXIMA;
Foo.ANCHURA_MINIMA;

Answer:

Naming conventions make programs more understandable by making them easier to read. They can also provide information about the function of the identifier, such as whether it is a constant, a package, or a class, which can be helpful in understanding your code.

The Java documentation summarizes its naming convention:


Paquetes

The prefix of a unique package name is always written in lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the two-letter codes in English identifying countries as specified in ISO 3166, 1981 .

Subsequent components of the package name vary according to an organization's internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

com.sun.eng
com.apple.quicktime.v2
edu.cmu.cs.bovik.cheese

Clases

Class names must be nouns . If there is more than one word, the first letter of each word is capitalized. Try to keep your class names simple and descriptive. Use whole words: avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

class Raster; 
class ImageSprite;

Interfaces

Interface names must be capitalized with every first letter, just like class names.

interface RasterDelegate; 
interface Storing;

Métodos

Method names must be verbs. The first letter is always in lowercase and if there are several words, the first letters of each internal word in uppercase.

run(); 
runFast(); 
getBackground();

Variables

Except for variables, all class instances, classes, and constants are uppercase and lowercase with a lowercase first letter. Internal words start with capital letters. Variable names must not begin with underscores _ or dollar signs $ , although both are allowed.

Variable names should be short but meaningful. The choice of a variable name should be mnemonic, that is, designed to indicate to the casual observer the intention of its use. One-character variable names should be avoided, except for "throwaway" temporary variables. Common names for temporary variables are:

i , j , k , m , and n for integers;

c , d , and e for characters.

int     i;
char    c;
float   myWidth;

Constantes

The names of variables declared class constants and ANSI constants must be all uppercase with words separated by underscores (" _ ") . (ANSI constants should be avoided, to facilitate debugging.)

static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int GET_THE_CPU = 1;

However you can use another convention. It is recommended that it be one of the standardized in the programming community and that it be uniform , that is, that in all your code you use the same naming convention.

Scroll to Top