Java/OO编程中常量命名前缀符号的常见用法问询
Great question! When working across multi-language codebases, it’s super common to see teams use prefix conventions to make constant types clearer at a glance—especially when the language doesn’t enforce strict type visibility or when you want to speed up code scanning. Since you already know @ and & are invalid, here are some compliant, widely used options (along with how teams often repurpose them for type differentiation):
_(Underscore): This is the most universal valid prefix. While it’s often used for private or internal constants, many teams assign type-specific meanings. For example:_INT_TIMEOUTfor integer constants_STR_DEFAULT_LABELfor string constants
It works in nearly every programming language that allows custom identifiers.
$(Dollar Sign): As you noticed, this is already used in languages like PHP or Perl for variables, but in languages where it’s allowed as an identifier start, teams repurpose it for specific types—your use case for integers is totally a valid team convention.k(Lowercase K): A staple in C++, Objective-C, and Swift for constants overall, but you can pair it with type hints. For example:kFloatPifor floating-point constantskBoolEnableLoggingfor boolean constants
It’s a letter rather than a symbol, but it’s a well-established prefix that pairs nicely with type differentiation.
g(Lowercase G): Typically used for global constants, but you can adapt it to include type context:gUIntMaxConnectionsfor unsigned integer constantsgStrAppVersionfor string constants
m(Lowercase M): Often used for member-level constants in object-oriented languages, but again, you can repurpose it for types:mLongTimestampfor long integer constantsmCharDelimiterfor character constants
A quick reminder: There’s no one-size-fits-all standard here. The most important thing is consistency across your team or project. Make sure everyone agrees on what each prefix means, and document the convention if you’re working in a shared codebase. Also, double-check your target language’s identifier rules—for example, Java allows _ and $ as leading characters, while Python allows _ but restricts $ in some contexts.
内容的提问来源于stack exchange,提问作者kay

