当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Character.isJavaIdentifierStart()用法及代码示例


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

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

用法:

public static boolean isJavaIdentifierStart(int codePoint)

参数:参数codePoint是整数类型,它表示要测试的字符(Unicode代码点)。


返回值:如果字符可以启动Java标识符,则Character类的isJavaIdentifierStart(int codePoint)方法返回true。否则为假。

以下示例程序旨在说明Character.isJavaIdentifierStart()方法:

示例1:

// 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

示例2:

// 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://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isJavaIdentifierStart(char)



相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 Character.isJavaIdentifierStart() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。