Java - Character.UnicodeBlock of() Method



The Java Character.UnicodeBlock of() method produces the object representing the Unicode block containing the given character (a char value or a code point), or null if the character is not a member of a defined block.

For example, the ordinary alphabets belong to the BASIC LATIN Unicode Block, Hebrew characters belong to the HEBREW Unicode Block, etc.

Note: The Surrogate code points fall under the HIGH_SURROGATES and LOW_SURROGATES Unicode Blocks. Supplementary characters are also recognized under various Unicode Blocks.

Syntax

Following is the declaration for Java Character.UnicodeBlock of() method:

public static Character.UnicodeBlock of(char c)
(or)
public static Character.UnicodeBlock of(int codePoint)

Parameters

  • c − This is the character.
  • codePoint − This is the character (Unicode code point).

Return Value

This method returns the UnicodeBlock instance representing the Unicode block of which this character is a member, or null if the character is not a member of any Unicode block.

Example

The following example shows the usage of Java Character.UnicodeBlock of(char c) method. Here, we are passing a certain character as an argument to the method which returns the UnicodeBlock instance containing the character.

package com.tutorialspoint;

import java.lang.*;

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

      /* returns the object representing the Unicode block containing
         the given character */ 
      System.out.println(Character.UnicodeBlock.of('\u20ac'));
      System.out.println(Character.UnicodeBlock.of('-'));
      System.out.println(Character.UnicodeBlock.of('='));
      System.out.println(Character.UnicodeBlock.of('Z'));
   }
}

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

CURRENCY_SYMBOLS
BASIC_LATIN
BASIC_LATIN
BASIC_LATIN

Example

Another example to show how to call the Character.UnicodeBlock of(char c) method is given below. We are declaring an object to store the return value of the method when called.

import java.lang.*;

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

      Character.UnicodeBlock name = Character.UnicodeBlock.of('\u0045');
      System.out.println(name);
      Character.UnicodeBlock name1 = Character.UnicodeBlock.of('6');
      System.out.println(name1);
   }
}

On executing the code above, the output will be displayed as given below −

BASIC_LATIN
BASIC_LATIN

Example

The following example shows the usage of Java Character.UnicodeBlock of(int codePoint) method. Here, we are passing a Unicode code point as an argument to the method and its return value is displayed.

package com.tutorialspoint;

import java.lang.*;

public class CharacterUnicodeBlockDemo {

   public static void main(String[] args) {

      // returns the UnicodeBlock by specifying codePoint
      System.out.println(Character.UnicodeBlock.of(20));
      System.out.println(Character.UnicodeBlock.of(1009));    
      System.out.println(Character.UnicodeBlock.of(12345));        
      System.out.println(Character.UnicodeBlock.of(999));   
   }
}

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

BASIC_LATIN
GREEK
CJK_SYMBOLS_AND_PUNCTUATION
GREEK

Example

Another way to use the Character.UnicodeBlock of(int codePoint) method is shown in this example program below. We are creating an object of the Character.UnicodeBlock class to store the return value of the method invoked.

import java.lang.*;

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

      Character.UnicodeBlock name = Character.UnicodeBlock.of(0x0012);
      System.out.println(name);
      Character.UnicodeBlock name1 = Character.UnicodeBlock.of(0x0ffff);
      System.out.println(name1);
      Character.UnicodeBlock name2 = Character.UnicodeBlock.of(0x4562);
      System.out.println(name2);
   }
}

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

BASIC_LATIN
SPECIALS
CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

Example

In this following example, we will see the working of the of() method if the argument passed is not a valid code point.

import java.lang.*;

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

      Character.UnicodeBlock name = Character.UnicodeBlock.of(1972310);
      System.out.println(name);
   }
}

Exception

If we compile and run the program above, instead of producing the output an exception is raised as given below −

Exception in thread "main" java.lang.IllegalArgumentExceptionat java.lang.Character$UnicodeBlock.of(Character.java:3131)
at CharacterUnicodeBlockDemo.main(CharacterUnicodeBlockDemo.java:7)
java_lang_character.unicodeblock.htm
Advertisements