Home > Java > javaTutorial > Learn how to name Java identifiers correctly: improve code standardization, readability, and understandability

Learn how to name Java identifiers correctly: improve code standardization, readability, and understandability

WBOY
Release: 2024-01-31 16:02:06
Original
731 people have browsed it

Learn how to name Java identifiers correctly: improve code standardization, readability, and understandability

Java Identifier Naming Rules

Java identifiers are names used to identify classes, methods, variables and constants. Identifiers can consist of letters, numbers, underscores (_), and dollar signs ($), but they cannot start with a number. Identifiers cannot yet be keywords in Java.

Naming rules for identifiers

  • Identifiers must start with a letter.
  • Identifiers cannot contain spaces.
  • Identifiers cannot contain special characters except underscore (_) and dollar sign ($).
  • Identifiers cannot be keywords in Java.
  • Identifiers cannot start with a number.
  • Identifiers cannot contain hyphens (-).
  • Identifiers cannot contain periods (.).
  • Identifiers cannot contain commas (,).
  • Identifiers cannot contain semicolons (;).
  • Identifiers cannot contain colons (:).
  • Identifiers cannot contain question marks (?).
  • Identifiers cannot contain exclamation points (!).
  • Identifiers cannot contain double quotes (").
  • Identifiers cannot contain single quotes (').

Naming convention for identifiers

  • Class names should start with a capital letter, and the first letter of each word should be capitalized. For example: public class MyClass { }.
  • Method The name should start with a lowercase letter, and the first letter of each word should be capitalized. For example: public void myMethod() { }.
  • The variable name should start with a lowercase letter, and each word should be capitalized. The first letter of should be capitalized. For example: int myVariable = 10;.
  • Constant names should start with a capital letter, and the first letter of each word should be capitalized. For example: final int MY_CONSTANT = 10;.

Naming examples of identifiers

The following are some naming examples of identifiers:

  • Class name: MyClass, YourClass, ExampleClass
  • Method name: myMethod(),yourMethod(), exampleMethod()
  • Variable name: myVariable, yourVariable, exampleVariable
  • Constant names: MY_CONSTANT, YOUR_CONSTANT, EXAMPLE_CONSTANT

Naming considerations for identifiers

  • Identifiers should be easy to read and understand.
  • Identifiers should reflect the purpose of the variable, method, or class.
  • Identifiers should be able to be used with from other identifiers.
  • Identifiers should avoid the use of abbreviations.
  • Identifiers should avoid the use of slang.
  • Identifiers should avoid the use of profanity.

Conclusion

Naming rules for Java identifiers are very important for writing readable, maintainable, and reusable code. By following these rules, you can ensure that your code Easy to read and understand, and easy to collaborate with other developers.

The above is the detailed content of Learn how to name Java identifiers correctly: improve code standardization, readability, and understandability. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template