Master Java identifier naming rules and follow the specifications with ease
Java identifier naming rules: clear and easy to read, standardized and unified
In Java programming, identifiers are used to name variables and methods , class and package symbols. The choice of identifiers is critical to the readability and maintainability of your code. Following Java identifier naming rules can help you write clear, readable, standardized and unified code.
1. Identifiers consist of letters, numbers, underscores, and dollar signs
Java identifiers can consist of letters, numbers, underscores, and dollar signs. Among them, letters can be uppercase or lowercase, and numbers cannot be used as the first character of the identifier. For example, the following are valid identifiers:
name age _age $age
2. The identifier cannot be a Java keyword
Java keywords are words with special meanings in the Java language , cannot be used as an identifier. For example, the following are Java keywords:
abstract boolean byte case catch char class const continue default do double else extends final finally float for if implements import instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient try void volatile while
3. Identifiers cannot begin with numbers
Identifiers cannot begin with numbers. For example, the following identifiers are invalid:
1age 2name 3_age
4. The identifier cannot contain spaces
The identifier cannot contain spaces. For example, the following identifiers are invalid:
name age age_name
5. The identifier cannot be the same as a Java reserved word
Java reserved words are words reserved in the Java language , cannot be used as an identifier. For example, the following are Java reserved words:
abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while
6. Identifiers should be descriptive
Identifiers should be descriptive so that other programmers can easily understand them its meaning. For example, the following identifiers are descriptive:
customerName productName orderDate
7. Avoid abbreviations for identifiers
Abbreviations for identifiers should be avoided because abbreviations may Making the code difficult to understand. For example, the following identifiers are difficult to understand:
custName prodName ordDate
8. Identifiers should avoid using underscores
Identifiers should avoid using underscores because underscores may cause The code is difficult to read. For example, the following identifiers are difficult to read:
_customer_name _product_name _order_date
9. Identifiers should follow camelCase
Identifiers should follow camelCase, i.e. each The first letter of a word is capitalized and the remaining letters are lowercase. For example, the following identifiers follow camel case:
customerName productName orderDate
10. Identifiers should follow a consistent naming style
Identifiers should follow a consistent naming style , so that other programmers can easily understand the code. For example, you can use the following naming style:
##Hungarian nomenclature: The variable name prefix indicates the type of the variable, for example:
- iAge represents the integer variable agesName represents the string variable name
Pascal nomenclature: The variable name consists of words, each Capitalize the first letter of each word, for example:
- CustomerName represents the variable customerNameProductName represents the variable productName
CamelCase nomenclature: The variable name is composed of words, the first letter of the first word is lowercase, and the first letter of the remaining words is uppercase, for example:
- customerName represents the variable customerNameproductName represents the variable productName
Code example:
public class Customer { private String customerName; private String customerAddress; private String customerPhoneNumber; public Customer(String customerName, String customerAddress, String customerPhoneNumber) { this.customerName = customerName; this.customerAddress = customerAddress; this.customerPhoneNumber = customerPhoneNumber; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getCustomerAddress() { return customerAddress; } public void setCustomerAddress(String customerAddress) { this.customerAddress = customerAddress; } public String getCustomerPhoneNumber() { return customerPhoneNumber; } public void setCustomerPhoneNumber(String customerPhoneNumber) { this.customerPhoneNumber = customerPhoneNumber; } }
The above is the detailed content of Master Java identifier naming rules and follow the specifications with ease. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP function naming rules: Underscore: used to separate words and improve readability; suitable for private methods or variables. Hyphens: should not be used in function names. Best practices: Avoid hyphens; use underscores to separate words; precede private or protected method or variable names with an underscore.

PHP function naming rules: 1. Camel case naming method, the first letter is lowercase, and the first letter of subsequent words is capitalized; 2. Use verbs or adjectives as function names to clearly explain the function; 3. Avoid using underscores or hyphens; 4. Use descriptive function name. Practical example: formatPhoneNumber function follows the above rules.

Variable naming conventions you need to know when learning Python An important aspect when learning the Python programming language is learning how to name and use variables correctly. Variables are identifiers used to store and represent data. Good variable naming conventions not only improve the readability of your code, but also reduce the possibility of errors. This article will introduce some commonly used variable naming conventions and give corresponding code examples. Use Meaningful Names Variable names should have a clear meaning and be able to describe the data stored in the variable. Using meaningful names allows it to

How can you understand the design principles and goals behind the latest PHP code specification by reading its source code? Introduction: When writing high-quality PHP code, it is very important to follow certain coding standards. Through code specifications, the readability, maintainability and scalability of the code can be improved. For the PHP language, there is a widely adopted code specification, namely PSR (PHPStandardsRecommendations). This article will introduce how to read the source code of the latest PHP code specification

API (Application Programming Interface) interface specification refers to a series of guidelines and specifications that define and specify API interfaces in software development. The purpose of the API interface specification is to ensure interoperability and consistency between different software components. This article will introduce several important aspects of API interface specifications. Interface naming convention The name of an API interface should be clear, concise, and consistent, and can accurately express its function and purpose. Naming conventions should follow industry practices and internal conventions of the development team, and avoid using vague and confusing terms. this

Python is a very popular programming language. It is favored by more and more people because of its simplicity, ease of understanding, and ease of learning. In Python, indentation and code format are very important. If used irregularly, it will greatly affect the readability and maintainability of the code. This article aims to introduce several methods to solve the problem of irregular indentation spaces in Python code. Using automated tools In Python programming, it is very important to adhere to coding standards. Each indentation in the code should use the same number of spaces. If you manually modify line by line

Quickly standardize code style: The readability and consistency of PyCharm formatted shortcut key parsing code is very important for programmers. Under the premise of following certain coding style specifications, writing clean code can make the project easier to maintain and understand. As a powerful integrated development environment, PyCharm provides shortcut keys to help us quickly format code. This article will introduce several commonly used shortcut keys in PyCharm, as well as their specific usage and effects. 1. Code automatic indentation (Ctrl

How to use code specifications and naming rules in Java to unify the team's coding style? In a team, it is very important for developers to have a unified coding style. It can improve code readability and maintainability, reduce communication costs between developers, and improve code quality and stability. This article will introduce how to use code specifications and naming rules in Java to unify the team's coding style, and give some code examples. 1. Code specifications for indentation and line breaks: use four spaces for indentation and line breaks after each statement.
