Java - Character isMirrored() Method



The Java Character isMirrored() method determines whether the character is mirrored according to the Unicode specification or not. Mirrored characters should have their glyphs (also known as its design or representation) horizontally mirrored when displayed in a text that is read from right-to-left.

For example, '\u0028' LEFT PARENTHESIS is a Unicode character of an opening parenthesis. This will appear as "(" in text that is left-to-right but as a ")" in text that is right-to-left.

This method occurs in two polymorphic forms, with different parameter types: a char value and an integer code point value.

Syntax

Following is the syntax for Java Character isMirrored() method

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

Parameters

  • ch − char for which the mirrored property is requested

  • codePoint − the Unicode code point for which the mirrored property is requested

Return Value

This method returns true if the char is mirrored, false if the char is not mirrored or is not defined.

Example

The following example shows the usage of Java Character isMirrored(char ch) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

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

      // assign values to ch1, ch2
      ch1 = '}';
      ch2 = '^';

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

      // check if ch1, ch2 are mirrored and assign results to b1, b2
      b1 = Character.isMirrored(ch1);
      b2 = Character.isMirrored(ch2);

      String str1 = ch1 + " is a mirrored character is " + b1;
      String str2 = ch2 + " is a mirrored character is " + b2;

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

Output

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

} is a mirrored character is true
^ is a mirrored character is false

Example

The following example shows the usage of Java Character isMirrored(int codePoint) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

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

      // assign values to cp1, cp2
      cp1 = 0x0c01;
      cp2 = 0x003c; // represents <

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

      /**
       *  check if cp1, cp2 represent mirrored characters
       *  and assign results to b1, b2
       */
      b1 = Character.isMirrored(cp1);
      b2 = Character.isMirrored(cp2);
      String str1 = "cp1 represents a mirrored character is " + b1;
      String str2 = "cp2 represents a mirrored character is " + b2;

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

Output

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

cp1 represents a mirrored character is false
cp2 represents a mirrored character is true

Example

As the return type for this method is boolean, we can also use conditional statements with the method as its condition, to check whether the character is a mirrored character.

import java.lang.*;
public class MirroredDemo{
   public static void main(String []args) {
      char c1 = '[';
      char c2 = '{';
      char c3 = '/';
      if(Character.isMirrored(c1)) {
         System.out.println(c1 + " is the mirrored character");
      }
      if(Character.isMirrored(c2)) {
         System.out.println(c2 + " is the mirrored character");
      }
      if(Character.isMirrored(c3)) {
         System.out.println(c3 + " is the mirrored character");
      }
      else {
         System.out.println("Not a mirrored character");
      }
   }
}

Output

Compile and run the program above in order to display the output as follows −

[ is the mirrored character
{ is the mirrored character
Not a mirrored character

Example

The conditional statements can also be used within loops. Here, we initialize a character array and pass its elements as arguments to the method using loop statements. Each element is checked to be a Mirrored character using conditional statements.

import java.lang.*;
public class MirroredDemo{
   public static void main(String []args) {
      char c[] = {'(', ']', '-', '{', '<', '|'};
      for(int i = 0; i < c.length; i++) {
         if(Character.isMirrored(c[i])) {
            System.out.println(c[i] + " character is mirrored");
         }
         else {
            System.out.println(c[i] + " character is not mirrored");
         }
      }
   }
}

Output

The output after the program above is compiled and run will be displayed as −

( character is mirrored
] character is mirrored
- character is not mirrored
{ character is mirrored
< character is mirrored
| character is not mirrored
java_lang_character.htm
Advertisements