如何在字符串变量名中包含反斜杠(在 Java 中)

How to include backslash in String variable name (in java)

我想在字符串变量名中包含反斜杠怎么办。

例如:

String Cd_St_SSLC/PUC;

这是一个正斜杠,在 Java 变量名中是不合法的,因为它是除法运算符。

int a = b/c;

/(正斜杠)不鼓励使用,因为它们是保留字符。 / 的存在将 抛出 一个 编译时错误 如果您不进行分割、注释(///** *//* */),或将其包含在字符串中("//")或对其进行处理作为字符文字 ('//')。运算符不能出现在变量名中。

The Java™ Tutorials

Variables

Naming

Every programming language has its own set of rules and conventions for the kinds of names that you're allowed to use, and the Java programming language is no different. The rules and conventions for naming your variables can be summarized as follows:

  • Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "". The convention, however, is to always begin your variable names with a letter, not "$" or "". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.

  • Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word.

  • If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

另见 1/2

Java 标识符的语言规范。

The Java® Language Specification: Java SE 7 Edition

Chapter 3. Lexical Structure

3.8. Identifiers

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

Identifier:

IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral

IdentifierChars:

JavaLetter
IdentifierChars JavaLetterOrDigit

Java字母:

any Unicode character that is a Java letter (see below)

Java字母或数字:

any Unicode character that is a Java letter-or-digit (see below)

3.12. Operators

37 个标记是 运算符,由 ASCII 字符组成。

运营商:

之一
=   >   <   !   ~   ?   :
==  <=  >=  !=  &&  ||  ++  --
+   -   *   /   &   |   ^   %   <<   >>   >>>
+=  -=  *=  /=  &=  |=  ^=  %=  <<=  >>=  >>>=

另见 2/2

下面的方法Character.isUnicodeIdentifierPart可以判断"if the character may be part of a Unicode identifier".

Method: Java.lang.Character.isUnicodeIdentifierPart()

Description

The java.lang.Character.isUnicodeIdentifierPart(char ch) [method] determines if the specified character may be part of a Unicode identifier as other than the first character.

A character may be part of a Unicode identifier if and only if one of the following statements is true:

  • it is a letter
  • it is a connecting punctuation character (such as '_')
  • it is a digit
  • it is a numeric letter (such as a Roman numeral character)
  • it is a combining mark
  • it is a non-spacing mark
  • isIdentifierIgnorable returns true for this character.

我建议您考虑 Java 命名约定!您可以在 "Thinking in java" 中阅读更多相关信息,来自 http://java.about.com/od/javasyntax/a/nameconventions.htm...避免使用像“/”这样的字符是一个好习惯,也许您可​​以将其替换为“_”。

罗克珊娜

我建议你不要使用这个 String Cd_St_SSLC/PUC 因为它在 Java 变量名中是不合法的,如果你想要有意义的名称使用 String Cd_St_SSLC_PUC 下划线而不是这个。