You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java/OO编程中常量命名前缀符号的常见用法问询

Valid Symbol Prefixes for Type-Distinguished Constants

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_TIMEOUT for integer constants
    • _STR_DEFAULT_LABEL for 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:
    • kFloatPi for floating-point constants
    • kBoolEnableLogging for 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:
    • gUIntMaxConnections for unsigned integer constants
    • gStrAppVersion for string constants
  • m (Lowercase M): Often used for member-level constants in object-oriented languages, but again, you can repurpose it for types:
    • mLongTimestamp for long integer constants
    • mCharDelimiter for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:39:41