Home Java javaTutorial Master Java identifier naming rules and follow the specifications with ease

Master Java identifier naming rules and follow the specifications with ease

Feb 01, 2024 am 08:47 AM
specification Naming rules java identifier

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
Copy after login

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
Copy after login

3. Identifiers cannot begin with numbers

Identifiers cannot begin with numbers. For example, the following identifiers are invalid:

1age
2name
3_age
Copy after login

4. The identifier cannot contain spaces

The identifier cannot contain spaces. For example, the following identifiers are invalid:

name age
age_name
Copy after login

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
Copy after login

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
Copy after login
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login
Copy after login

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 age
    • sName 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 customerName
    • ProductName 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 customerName
    • productName 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;
    }

}
Copy after login
In this code example, we use camel case to name the variables and methods. This naming style makes the code easier to read and understand.

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!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the rules for using underscores and hyphens in PHP function names? What are the rules for using underscores and hyphens in PHP function names? Apr 20, 2024 am 09:33 AM

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.

What are the naming rules for PHP functions? What are the naming rules for PHP functions? Apr 18, 2024 pm 06:12 PM

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 required in Python learning Variable naming conventions required in Python learning Jan 20, 2024 am 09:03 AM

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? How can you understand the design principles and goals behind the latest PHP code specification by reading its source code? Sep 05, 2023 pm 02:46 PM

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

What is the standard for API interface? What is the standard for API interface? Feb 23, 2024 pm 08:15 PM

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

How to solve the problem of irregular use of indented spaces in Python code? How to solve the problem of irregular use of indented spaces in Python code? Jun 24, 2023 pm 09:03 PM

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

PyCharm formatting shortcut key analysis: how to quickly unify code style PyCharm formatting shortcut key analysis: how to quickly unify code style Jan 27, 2024 am 10:38 AM

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? How to use code specifications and naming rules in Java to unify the team's coding style? Aug 02, 2023 am 09:17 AM

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.

See all articles