Home  >  Article  >  Java  >  What are the naming conventions that Java needs to follow when developing international projects?

What are the naming conventions that Java needs to follow when developing international projects?

WBOY
WBOYforward
2023-05-13 15:16:06610browse

1. Package naming convention

The package name of a software project should be the reverse order of your company's domain name, plus the name of your project, all using lowercase letters and cannot contain other symbols. You can add the package version at the end, as shown in the example below:

org.example.aprojectname
org.example.aprojectname.innerpkg
org.example.aprojectname.innerpkg.anotherpkg
org.example.aprojectname.innerpkg2
org.example.anotherproject
org.example.anotherproject.v2

2. Class naming convention

The class name must be a noun, use camel case naming, and the first letter should be capitalized, for example HashMap. It should be possible to infer from the class name what functions and variables it will probably contain. For example:

class Student{}
class ArrayList{}
class HashMap{}
class ComputerEngineer{}

3. Interface interface

Interface names should use adjectives as much as possible. Sometimes it can also be a noun, such as List or Map. Like the class naming convention, use camel case and the first letter should be capitalized. As follows:

interface Cloneable{}
interface AutoCloseable{}
interface PreparedStatement{}
interface Engineer{}

Interface usually represents the general name of a class of objects. The adjective "can run" can be an interface name, and the implementation class can be a person, a dog, or a cat.

The use of nouns requires a certain generality. "Claw" is more like the name of an interface than "cat claw".

4. Variables

Variables should be short and meaningful nouns, follow camel case naming, and the first letter should be lowercase. And don't use the dash ('_') or dollar sign '$' characters.

  • The counting variables used in the for loop can be i, j, k, m and n. They are also commonly used for integers

  • For Boolean values, it usually starts with is or has.

  • You can use Of to connect multiple nouns in series

  • You can use adjectives to modify nouns

int countOfCustomer;   //Of串联
float averageInterest;   //形容词修饰名词
long timeInMillisecond, daysInYear;    //时间
boolean isEngineer, isCompleted, hasSubmitted;   //布尔类型

五, Constant

Constant is also an important part of the program. Also use nouns, or nouns modified by adjectives. Use all capital letters and use underscores to separate words.

public static final float PI = 3.14;
static int CREATED_ON_YEAR = 2019;

6. Methods

Method names should use verbs, and the function of the method should be inferred from the name, and should be as short as possible. Use camelCase for naming. Usually, get and set are used to get data and set data respectively. For boolean values, the method name should be the same as the variable name.

void print(Object obj);
void remove(Obejct obj);
Object update();
int getCountOfCustomer();//getter
void setCountOfCustomer(int countOfCustomers);//setter
boolean isUserAdmin(User user);  //布尔类型

7. Generics

Use a single uppercase letter, T is widely used. E is usually used for set elements, and K and V are usually used for mapping relationships. Do not use multiple characters.

public <T> void print(T t);
interface List<E>{}
class HashMap<K, V> {};

The above is the detailed content of What are the naming conventions that Java needs to follow when developing international projects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete