Java - Character isJavaIdentifierPart() 方法



Java 的 Character isJavaIdentifierPart() 方法用于确定指定的字符是否可以作为 Java 标识符的一部分(除了第一个字符)。

如果满足以下任何条件,则字符可以是 Java 标识符的一部分:

  • 它是字母

  • 它是货币符号(例如 '$')

  • 它是连接标点符号(例如 '_')

  • 它是数字

  • 它是数字字母(例如罗马数字字符)

  • 它是组合标记

  • 它是非间距标记

注意 - 该方法以两种多态形式出现,具有相同的返回类型,但参数类型不同。

语法

以下是 Java Character isJavaIdentifierPart() 方法的语法:

public static boolean isJavaIdentifierPart(char ch)
(or)
public static boolean isJavaIdentifierPart(int codePoint)

参数

  • ch - 要测试的字符

  • codePoint - 要测试的 Unicode 代码点

返回值

如果字符可以是 Java 标识符的一部分,则此方法返回 true,否则返回 false。

检查字符是否为 Java 标识符的一部分示例

以下示例显示了 Java Character isJavaIdentifierPart(char ch) 方法的使用。在此程序中,我们创建了两个 char 变量并为其分配了一些值。现在使用 isJavaIdentifierPart() 方法,我们检查 char 变量是否为 Java 标识符的一部分,并打印结果。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = '3';
      ch2 = '/';

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if ch1, ch2 can be part of java identifier
       *  and assign results to b1, b2
       */
      b1 = Character.isJavaIdentifierPart(ch1);
      b2 = Character.isJavaIdentifierPart(ch2);
      String str1 = ch1 + " may be part of a Java identifier is " + b1;
      String str2 = ch2 + " may be part of a Java identifier is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

3 may be part of a Java identifier is true
/ may be part of a Java identifier is false

检查代码点是否为 Java 标识符的一部分示例

以下示例显示了 Java Character isJavaIdentifierPart(int codePoint) 方法的使用。在此程序中,我们创建了两个 int 变量并为其分配了一些值。现在使用 isJavaIdentifierPart() 方法,我们检查 int 变量是否为 Java 标识符的一部分,并打印结果。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x01f1;
      cp2 = 0x07c0;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if characters represented by cp1, cp2 can be part of
       *  java identifier and assign results to b1, b2
       */
      b1 = Character.isJavaIdentifierPart(cp1);
      b2 = Character.isJavaIdentifierPart(cp2);
      String str1 = "cp1 may be part of a Java identifier is " + b1;
      String str2 = "cp2 may be part of a Java identifier is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

cp1 may be part of a Java identifier is true
cp2 may be part of a Java identifier is false

检查字符是否为 Java 标识符的一部分示例

在此示例程序中,我们将声明一个字符数组并将其初始化为长度为 7;然后我们使用循环语句(for 循环)通过将字符数组的每个元素作为其参数来调用该方法。使用条件语句,检查元素是否为 Java 标识符的一部分。

package com.tutorialspoint;

public class Main {
   public static void main(String[] args) {
      char ch[] = new char[] {'a', '3', '/', '~', 'X', '_', '$'};
      int count = 0;
      for(int i = 0; i < ch.length; i++){
         if(Character.isJavaIdentifierPart(ch[i])){
            System.out.println("The character is a part of Java Identifiers");
            count++;
         }
         else
            System.out.println("The character is not a part of Java Identifiers");
      }
      if(count == 0)
         System.out.println("Java Identifiers do not exist in this array");
   }
}

输出

编译并运行上面给出的程序,然后显示输出如下:

The character is a part of Java Identifiers
The character is a part of Java Identifiers
The character is not a part of Java Identifiers
The character is not a part of Java Identifiers
The character is a part of Java Identifiers
The character is a part of Java Identifiers
The character is a part of Java Identifiers

检查字符是否为 Java 标识符的一部分示例

在此示例程序中,我们将仅使用条件语句而不用循环语句。因此,仅初始化一个 char 变量作为参数传递。

package com.tutorialspoint;

public class Main {
   public static void main(String[] args) {
      char ch = '%';
      if(Character.isJavaIdentifierPart(ch))
         System.out.println("The character is a part of Java Identifiers");
      else
         System.out.println("The character is not a part of Java Identifiers");
   }
}

输出

编译并运行上面的程序后,输出将打印如下:

The character is not a part of Java Identifiers

检查无效字符是否为 Java 标识符的一部分示例

最后,让我们看看此方法可能引发的某些常见错误:

public class Demo {
   public static void main(String[] args) {
      char c = '\u0026';
      System.out.println((int)Character.isJavaIdentifierPart(c));
   }
}

编译时错误

布尔数据类型不能在 Java 中进行类型转换:

Demo.java:6: error: incompatible types: boolean cannot be converted to int
    System.out.println((int)Character.isJavaIdentifierPart(c));
                                                          ^
1 error
java_lang_character.htm
广告

© . All rights reserved.