Home >Java >javaTutorial >What is the naming convention in Java? The use of naming conventions in Java

What is the naming convention in Java? The use of naming conventions in Java

青灯夜游
青灯夜游Original
2019-01-23 14:24:196427browse

All components in Java require names. The names used for classes, variables and methods are called identifiers; and the Java naming convention is a rule that needs to be followed when naming identifiers. The following article will introduce you to the ava naming convention. I hope it will be helpful to you.

What is the naming convention in Java? The use of naming conventions in Java

Why use naming conventions?

Because different Java programmers will use different styles and methods to program; and by using standard Java naming conventions, the readability of Java code will become higher. Make your code easier to read for yourself and other programmers. Readability of Java code is important because it means less time spent figuring out what the code does, leaving more time fixing or modifying it. [Video tutorial recommendation: Java tutorial]

All classes, interfaces, packages, methods and fields of the Java programming language are given according to the Java naming convention. If these conventions are not followed, confusing or erroneous code may result.

Standard Java Naming Convention

The following are the key rules that every identifier must follow:

● The name must not contain any spaces.

● The name should not start with special characters such as & (ampersand), $ (dollar), _ (underscore), etc.

Let's look at some other rules that different types of identifiers should follow.

1. Package naming convention

The package name must be a set of words with all lowercase letters (such as com, org, net, etc.); if the name contains multiple words , you should use dot (.) to separate (such as java.util, java.lang). The subsequent parts of the package name may differ based on the organization's own internal naming conventions. Example:

package com.howtodoinjava.webapp.controller;

package com.company.myapplication.web.controller;

package com.google.search.common;

2. Class naming convention

In Java, the class name should usually be a noun starting with a capital letter (for example: Color, Button, System , Thread, etc), need to use appropriate words, not abbreviations. For example:

public class ArrayList {}
 
public class Employee {}
 
public class Record {}
 
public class Identity {}

3. Interface naming convention

In Java, the interface name should usually be an adjective starting with a capital letter (such as: Runnable, Remote, ActionListener) ;In the same case, interfaces can also be nouns, when they present a series of categories, such as List and Map.

public interface Serializable {}
 
public interface Clonable {}
 
public interface Iterable {}
 
public interface List {}

4. Method naming convention

Methods should usually be a verb starting with a lowercase letter, such as main(), print(), println(); if the name Containing multiple words, you need to use camel case notation, using lowercase letters followed by uppercase letters, such as actionPerformed().

public Long getId() {}
 
public void remove(Object o) {}
 
public Object update(Object o) {}
 
public Report getReportById(Long id) {}
 
public Report getReportByName(String name) {}

5. Variable naming convention

Static and method parameter variable names should start with a lowercase letter; if the name contains multiple words, camel case notation is required. , using lowercase letters followed by uppercase letters. If it is a temporary variable, it can be a single character, such as x, y, z.

public Long id;

public EmployeeDao employeeDao;

private Properties properties;

for (int i = 0; i < list.size(); i++) {
}

6. Constant naming convention

Java constants should be in uppercase letters. If the name contains multiple words, they should be separated by underscores (_), for example: MAX_PRIORITY. Make sure to use the final modifier with a constant variable, which can contain numbers, but not starting letters.

public final String SECURITY_TOKEN = "...";
 
public final int INITIAL_SIZE = 16;
 
public final Integer MAX_SIZE = Integer.MAX;

Note: For ease of debugging, ANSI constants should be avoided.

7. Universal type naming convention

Universal type parameter names should be uppercase single letters. It is generally recommended to use 'T' type letters. In JDK classes, E is used for collection elements, S is used for service loaders, K and V are used for mapping keys and values.

public interface Map <K,V> {}
 
public interface List<E> extends Collection<E> {}
 
Iterator<E> iterator() {}

8. Enumeration naming convention

Similar to class names, enumeration names should be all uppercase letters.

enum Direction {NORTH, EAST, SOUTH, WEST}

9. Annotation naming convention

The annotation name follows the title case notation. They can be adjectives, verbs or nouns based on requirements.

public @interface FunctionalInterface {}
 
public @interface Deprecated {}
 
public @interface Documented {}
 
public @Asyn Documented {}
 
public @Test Documented {}

Summary: Naming conventions are very important when writing clean code in any programming language. Standard Java naming conventions make the code more readable and maintainable.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is the naming convention in Java? The use of naming conventions in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn