📜  Java中的字符.isJavaIdentifierStart() 方法

📅  最后修改于: 2022-05-13 01:54:20.626000             🧑  作者: Mango

Java中的字符.isJavaIdentifierStart() 方法

字符.isJavaIdentifierStart(int codePoint)是Java中的一个内置方法,用于确定字符(Unicode 代码点)是否允许作为Java标识符中的第一个字符。需要注意的是,当且仅当以下条件之一为真时,字符才可以开始Java标识符:

  • isLetter(ch) 返回真
  • getType(ch) 返回 LETTER_NUMBER
  • ch 是货币符号(例如'$')
  • ch 是连接标点符号(例如“_”)。

句法:

public static boolean isJavaIdentifierStart(int codePoint)

参数:参数codePoint是整数类型,指的是要测试的字符(Unicode码位)。

返回值:如果字符可以开始一个Java标识符, 字符类的isJavaIdentifierStart(int codePoint)方法返回true;否则为假。

下面的程序说明了字符.isJavaIdentifierStart() 方法:

方案一:

// Java program to illustrate
// Character.isJavaIdentifierStart() method 
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // Create 2 int primitives c1, c2
        int c1 = 0x0039, c2 = 0x004b, c3 = 0x0081;
  
        // Assign isJavaIdentifierPart results of 
        // c1, c2 to boolean primitives bool1, bool2
  
        boolean bool1 = Character.isJavaIdentifierStart(c1);
        boolean bool2 = Character.isJavaIdentifierStart(c2);
        boolean bool3 = Character.isJavaIdentifierStart(c3);
  
        String str1 = "c1 may start a Java identifier is " + bool1;
        String str2 = "c2 may start a Java identifier is " + bool2;
        String str3 = "c3 may start a Java identifier is " + bool3;
  
        // Print bool1, bool2 values
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }
}
输出:
c1 may start a Java identifier is false
c2 may start a Java identifier is true
c3 may start a Java identifier is false

方案二:

// Java program to illustrate
// Character.isJavaIdentifierStart() method 
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // Create 2 int primitives c1, c2
        int c1 = 0x0034, c2 = 0x005a;
  
        // Assign isJavaIdentifierPart results of 
        // c1, c2 to boolean primitives bool1, bool2
  
        boolean bool1 = Character.isJavaIdentifierStart(c1);
        boolean bool2 = Character.isJavaIdentifierStart(c2);
  
        String str1 = "c1 may start a Java identifier is " + bool1;
        String str2 = "c2 may start a Java identifier is " + bool2;
  
        // Print bool1, bool2 values
        System.out.println(str1);
        System.out.println(str2);
    }
}
输出:
c1 may start a Java identifier is false
c2 may start a Java identifier is true

参考:https: Java/lang/ 字符.html#isJavaIdentifierStart(char)