In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about what the Java naming convention is. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Package naming convention
The role of Package is to group and manage classes or interfaces with similar functions or related functions, so as to facilitate class positioning and lookup. At the same time, packages can also be used to avoid class name conflicts and access control, making the code easier to maintain. Usually, the package is ordered to be named in lowercase letters and use "." For segmentation, each divided unit can contain only one noun. In general, package naming is often prefixed with a top-level domain name, such as com,net,org,edu,gov,cn,io, followed by the company / organization / individual name and the function module name. Here are some examples of package naming:
Package org.springframework.boot.autoconfigure.cloudpackage org.springframework.boot.utilpackage org.hibernate.actionpackage org.hibernate.cfgpackage com.alibaba.druidpackage com.alibaba.druid.filterpackage com.alibaba.nacos.client.configpackage com.ramostear.blog.web
Here are some common examples of package naming for Oracle Java:
Package java.beanspackage java.iopackage java.langpackage java.netpackage java.utilpackage javax.annotation2. Class naming convention
Class (Class) is usually named by nouns, and the first letter is capitalized. If a class name contains more than two nouns, it is recommended to use hump naming (Camel-Case) to write the class name, and the first letter of each noun should also be capitalized. In general, class names are written as simple and descriptive as possible, so abbreviations are not recommended when writing class names (except for some conventional names, such as Internationalization and Localization abbreviated to i18n uniform Resource Identifier abbreviated to URI,Data Access Object to DAO,JSON Web Token abbreviated to JWT,HyperText Markup Language abbreviated to HTML, etc.). Here are some common examples of class naming:
Public class UserDTO {/ / TODO...} class EmployeeService {/ / TODO...} class StudentDAO {/ / TODO...} class OrderItemEntity {/ / TODO...} public class UserServiceImpl {/ / TODO...} public class OrderItemController {/ / TODO...}
Here are some examples of standard naming in Oracle Java:
Public class HTMLEditorKit {/ /...} public abstract class HttpContext {/ /...} public interface ImageObserver {/ /...} public class ArrayIndexOutOfBoundsException {/ /...} public class enum Thread.State {/ /...} interface naming specification
First of all, the Interface is a special class that describes the actions of a certain type of object; simply put, the interface is also a class (not very rigorous), so the writing of the interface name should also comply with the class name writing specification, the first letter should be capitalized, different from the common class name, the interface naming usually uses adjectives or verbs to describe the action of the interface. The following are examples of adjective naming for the interfaces of some standard libraries in Oracle Java:
Public interface Closeable {/ /...} public interface Cloneable {/ /...} public interface Runnable {/ /...} public interface Comparable {/ /...} public interface CompletionService {/ /...} public interface Iterable {/ /...} public interface EventListener {/ /...}
In the Spring Framework standard library, interfaces are usually named in the combination of nouns, verbs / adjectives. Here are some examples of interface naming in Spring Framework:
Public interface AfterAdvice {/ /...} public interface TargetClassAware {/ /...} public interface ApplicationContextAware {/ /...} public interface MessageSourceResolvable {/ /...} 2.2 abstract class naming convention
Abstract class (Abstract Class) is a special class whose naming is equivalent to the naming convention of ordinary classes. In general, in order to distinguish abstract classes from ordinary classes and interfaces and improve the readability of abstract classes, abstract classes are prefixed with "Abstract" / "Base" when naming abstract classes. Here are some general naming examples in programming:
Public abstract class AbstractRepository {/ /...} public abstract class AbstractController {/ /...} public abstract class BaseDao {/ /...} public abstract class AbstractCommonService {/ /...}
Here are examples of abstract classes that are common in Spring Framework:
Public abstract class AbstractAspectJAdvice {/ /...} public abstract class AbstractSingletonProxyFactoryBean {/ /...} public abstract class AbstractBeanFactoryPointcutAdvisor {/ /...} public abstract class AbstractCachingConfiguration {/ /...} public abstract class AbstractContextLoaderInitializer {/ /...} 2.3 exception class naming convention
An exception class (Exception Class) is also a class, but unlike a normal class, an exception class needs to be named with the suffix "Exception". Here are examples of common exception class naming:
Public class FileNotFoundException {/ /...} public class UserAlreadyExistException {/ /...} public class TransactionException {/ /...} public class ClassNotFoundException {/ /...} public class IllegalArgumentException {/ /...} public class IndexOutOfBoundsException {/ /...}
In addition, there is another class of exceptions in Java, which belong to system exceptions, which are named with the suffix "Error" to distinguish between Exception (coding, environment, operation, etc.). The following is a named example of a system exception (non-checked exception):
Public abstract class VirtualMachineError {/ /...} public class StackOverflowError {/ /...} public class OutOfMemoryError {/ /...} public class IllegalAccessError {/ /...} public class NoClassDefFoundError {/ /...} public class NoSuchFieldError {/ /...} public class NoSuchMethodError {/ /...} 3. Method naming convention
When a method (Method) is named, its first letter should be lowercase, and if the method signature consists of multiple words, the hump naming method is used to write from the second word. In general, when naming a method, it is usually a combination of verbs / verbs + nouns. Here are some common examples of method naming.
3.1 representation acquisition
If a method is used to get a value, it is usually prefixed with "get", for example:
Public String getUserName () {/ /...} public List getUserIds () {/ /...} public User getOne () {/ /...} 3.2 express query
If a method needs to get some data by query or filter, it usually uses "find" / "query" as its prefix, for example:
Public List findOne (Integer id) {/ /...} public List findAll () {/ /...} public List queryOrders () {/ /...} 3.3.Exposition conditions
If a method requires some conditional parameters, you can use characters such as "by" / "with" as connectors for the condition in the method name, for example:
Public User findByUsername (String username) {/ /...} public List getUserIdsWithState (boolean state) {/ /...} public List findAllByUsernameOrderByIdDesc (String username) {/ /...} 3.4 express settings
If a method is to set, insert, modify, delete, etc., you should prefix its noun with the corresponding verb (set,insert,update,delete), for example:
Public void setName (String name) {/ /...} public User insert (User user) {/ /...} public void update (User user) {/ /...} public void clearAll () {/ /...} 3.5 other specifications
If a method is used to get the length or quantity of a set of data, the method should be named using length or size; if the return value of the method is a Boolean, the method should be prefixed with "is" or "has"; if the method is used to convert one type of data into another data type, you can use "to" as the prefix. The following is a comprehensive example:
Public long length () {/ /...} public int size () {/ /...} public boolean isOpen () {/ /...} public boolean isNotEmpty () {/ /...} public boolean hasLength () {/ /...} public Set mapToSet (Map map) {/ /...} public UserDto convertTo (User user) {/ /...} public String toString (Object obj) {/ /...} 4. Variable naming convention
Variable naming includes parameter names, member variables, and local variables. Variable naming usually begins with a lowercase letter, and if the variable name consists of multiple words, the first letter from the second word needs to be capitalized. In the variable naming process, it is not recommended to use "_" as a prefix or a split symbol between words. Here are some common examples of variable naming:
Private String nickName;private String mobileNumber;private Long id;private String username;private Long orderId;private Long orderItemId;5. Constant naming convention
Generally speaking, constant names are written in all uppercase English words. If a constant name consists of multiple words, the words are divided by "_". The following is an example of constant naming:
Public static final String LOGIN_USER_SESSION_KEY = "current_login_user", public static final int MAX_AGE_VALUE = 120th public static final int DEFAULT_PAGE_NO = 1x public static final long MAX_PAGE_SIZE = 1000x public static final boolean HAS_LICENSE = false;public static final boolean IS_CHECKED = false;6. Enumeration naming convention
The Enum class is a special class, and its naming convention follows the naming constraints of ordinary classes. The first letter is capitalized, and the hump naming method is adopted. The names of the values defined in the enumeration class follow the naming conventions of constants, and the names of enumerated values need to be related to the class name. Here are some examples of enumerations:
Public enum Color {RED,YELLOW,BLUE,GREEN,WHITE;} public enum PhysicalSize {TINY,SMALL,MEDIUM,LARGE,HUGE,GIGANTIC;}
The following is an example from the Oracle Java standard library:
Public enum ElementType {TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE, TYPE_PARAMETER, TYPE_USE;} 7. Other naming conventions 7.1 arrays
When defining an array, try to maintain the following writing rules for ease of reading:
Int [] array = new int [10]; int [] idArray = {1 First, "Yellow", "Big"} public List getNameById (Integer [] ids) {/ /...} / or public List getNameById (Integer...ids) {/ /...} 7.2 denotes plural or collection
If a variable is used to describe multiple data, try to write in the plural of words, for example:
Collection orders;int [] values;List items
In addition, if you are expressing an Map data, you should use "map" as its suffix, for example:
Map userMap;Map listMap;7.3 generic class
When writing generic classes, you usually make the following conventions:
E stands for Element, which is usually used in collections
The type of unique identifier used by ID to represent an object
T stands for Type (type), usually referring to class
K stands for Key (key) and is usually used in Map
V stands for Value (value), which is usually used in Map and occurs in pairs with K
N stands for Number, which is usually used to represent numeric types
? Represents an indeterminate Java type
X is used to indicate an exception
Uther S stands for any type.
The following is a writing example of a generic class:
Public class HashSet extends AbstractSet {/ /...} public class HashMap extends AbstractMap {/ /...} public class ThreadLocal {/ /...} public interface Functor {T val () throws X;} public class Container {private K key; private V value; Container (K key,V value) {this.key = key; this.value = value;} / / getter and setter...} public interface BaseRepository {T findById (ID id) Void update (T t); List findByIds (ID...ids);} public static List methodName (Class clz) {List dataList = getByClz (clz); return dataList;} 7.4 interface implementation class
For ease of reading, in general, it is recommended that interface implementation classes use "Impl as a suffix". Uppercase "I" is not recommended as an interface prefix. The following is a writing example of an interface and an interface implementation class.
Recommended writing method:
Public interface OrderService {/ /...} public class OrderServiceImpl implements OrderService {/ /...}
Not recommended writing:
Public interface IOrderService {/ /...} public class OrderService implements IOrderService {/ /...} 7.5 test classes and test methods
In the project, the test class is written by the method of business module name / interface to be tested / class under test + "Test", and the test functions in the test class are written in the combination of "test" + use case operation _ state, for example:
Public class UserServiceTest {public void testFindByUsernameAndPassword () {/ /...} public void testUsernameExist_notExist () {/ /...} public void testDeleteById_isOk () {/ /...} 8 extension: various O in shorthand Java development
Finally, a table and diagram are used to quickly sort out the meanings, differences and connections of BO,DTO,DAO,PO,POJO,VO in Java.
The name uses scope to explain that BO is used to name Business Object business processing objects of business-related classes such as Service,Manager,Business, and its main function is to encapsulate business logic into an object. The internal attributes of DTO processed PO objects may increase or decrease Data Transfer Object data transfer objects, which are mainly used in places where a large amount of data needs to be transferred, such as remote calls. For example, some or all of the attributes of one or more PO classes can be encapsulated as DTO for transmission DAO classes used to read and write to the database are named Data Access Object data access objects, which are mainly used to encapsulate database access. Through DAO, POJO can be persisted to PO, and PO can also be used to encapsulate named Persistant Object persistent objects of classes such as VO and DTOPOBean,Entity. The mapping state of the data in the database table in the Java object can be simply understood as a PO object, that is, a record in the database table. POJOPOJO is a Plain Ordinary Java Object simple Java object, which is a simple ordinary Java object. It is forbidden to name the class XxxxPOJOVO, which is usually the data object Value Object value object transferred between the view control layer and the template engine, which is mainly used in the view layer. The view controller encapsulates the attributes needed by the view layer into an object, and then uses a VO object to transfer data between the view controller and the view. The AO application layer object Application Object, a reuse object model abstracted between the Web layer and the Service layer, is rarely used. Thank you for reading! This is the end of this article on "what is the naming convention of Java?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.