Java Character.UnicodeBlock forName() Method



The Java Character.UnicodeBlock forName() method prints the UnicodeBlock with the given name. These block names are determined by The Unicode Standards. This method accepts block names in the following forms −

  • Canonical block names as defined by the Unicode Standard. For example, the standard defines a "Basic Latin" block.
  • Canonical block names with all spaces removed. For example, "BasicLatin" is a valid block name for the "Basic Latin" block.
  • The text representation of each constant UnicodeBlock identifier. For example, this method will return the BASIC_LATIN block if provided with the "BASIC_LATIN" name.

Syntax

Following is the syntax for Java Character.UnicodeBlock forName() method

public static final Character.UnicodeBlock.forName(String blockName)

Parameters

  • blockName − This is a UnicodeBlock name.

Return Value

This method returns the UnicodeBlock instance identified by blockName.

Example

When we pass a valid Unicode Block Name as an argument to the method, the return value will be the instance of the Unicode Block.

The following example shows the usage of Java Character.UnicodeBlock forName() method.

package com.tutorialspoint;

import java.lang.*;

public class CharacterUnicodeBlockDemo {
  
   public static void main(String[] args) {
    
      // returns the UnicodeBlock instance with blockName "BASIC_LATIN"
      System.out.println(Character.UnicodeBlock.forName("BASIC_LATIN"));
  
      // returns the UnicodeBlock instance with blockName "BASIC_LATIN"
      System.out.println(Character.UnicodeBlock.forName("BasicLatin"));

      // returns the UnicodeBlock instance with specified blockName 
      System.out.println(Character.UnicodeBlock.forName("ARABIC"));
      System.out.println(Character.UnicodeBlock.forName("MUSICALSYMBOLS"));
      System.out.println(Character.UnicodeBlock.forName("TAMIL"));
   }
}

Let us compile and run the above program, this will produce the following result −

BASIC_LATIN
BASIC_LATIN
ARABIC
MUSICAL_SYMBOLS
TAMIL

Example

Another example to call the forName() method on various UnicodeBlock objects is given below:

import java.lang.*;

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

    Character.UnicodeBlock s = Character.UnicodeBlock.forName("BASIC_LATIN");
    Character.UnicodeBlock s1 = Character.UnicodeBlock.forName("ARABIC");
    Character.UnicodeBlock s2 = Character.UnicodeBlock.forName("HEBREW");
    System.out.println("The unicode block instance is : " + s);
    System.out.println("The unicode block instance is : " + s1);
    System.out.println("The unicode block instance is : " + s2);
   }
}

Let us compile and run the program above and the output will be achieved as follows −

The unicode block instance is : BASIC_LATIN
The unicode block instance is : ARABIC
The unicode block instance is : HEBREW

Example

If we pass an argument that is not a valid Unicode Block name to the method, an IllegalArgumentException is thrown.

In the following example, we will instantiate an object for the Character.UnicodeBlock class. We invoke the forName() method on this object created and pass an argument that is not a valid block name.

import java.lang.*;

public class CharacterUnicodeBlockDemo {
  
   public static void main(String[] args) {
    
    // throws an illegal argument exception as there is no unicode block called english
    Character.UnicodeBlock s = Character.UnicodeBlock.forName("ENGLISH");
    
    // to print the output
    System.out.println("The unicode block instance is : " + s);
   }
}

Exception

After compiling and executing the program above, the output is printed as follows:

Exception in thread "main" java.lang.IllegalArgumentExceptionat java.lang.Character$UnicodeBlock.forName(Character.java:3190)
	at CharacterUnicodeBlockDemo.main(CharacterUnicodeBlockDemo.java:8)

Example

If we pass null as the argument to the method, a NullPointerException is thrown.

In the following example, we will instantiate an object for the Character.UnicodeBlock class. We invoke the forName() method on this object created and pass a null argument to it.

import java.lang.*;

public class CharacterUnicodeBlockDemo {
  
   public static void main(String[] args) {
    
    // throws a null pointer exception
    Character.UnicodeBlock s = Character.UnicodeBlock.forName(null);
    
    // to print the output
    System.out.println("The unicode block instance is : " + s);
   }
}

Exception

On compiling and executing, a NullPointerException is thrown as the UnicodeBlock name is set to null; so the output is obtained as follows −

Exception in thread "main" java.lang.NullPointerException
at java.lang.Character$UnicodeBlock.forName(Character.java:3188)
	at CharacterUnicodeBlockDemo.main(CharacterUnicodeBlockDemo.java:8)
java_lang_character.unicodeblock.htm
Advertisements