Home > Java > javaTutorial > Java program that uses character literals to store Unicode characters

Java program that uses character literals to store Unicode characters

WBOY
Release: 2023-09-02 21:45:05
forward
1007 people have browsed it

Unicode is an international character set that contains a large number of characters, symbols, and scripts from many languages ​​around the world. The Java programming language is platform independent with built-in support for Unicode characters, allowing developers to create applications that work seamlessly with multiple languages ​​and scripts.

In Java, the char data type is used to store Unicode characters, and character literals are used in source code to represent these characters. A character literal is a single Unicode character enclosed in single quotes (' ') and can be assigned directly to a char variable.

algorithm

  • Step 1 - Declare a char variable.

    • Declare a char variable with a suitable name.

    • Example: char myChar;

  • Step 2 - Assign the Unicode character literal to the variable.

    • Assign a Unicode character literal enclosed in single quotes to a char variable

    • Example: myChar = '\u0041'; (Assign Unicode character "A" to myChar)

  • Step 3 - (Optional) Perform operations or manipulate Unicode characters.

    • Perform any operation on the Unicode characters stored in the char variable according to the requirements of the program.

  • Step 4 - Print the stored Unicode characters.

    • Use the System.out.println() method to print the Unicode characters stored in the char variable.

    • Example: System.out.println("Stored character: " myChar); (Print "Stored character: A" to the console)

method

There are two ways to use Unicode characters in Java: using Unicode escape sequences and storing Unicode characters directly.

The first method involves using escape sequences to represent Unicode characters and is useful when the characters cannot be typed or displayed directly in Java code. The second method is to store Unicode characters directly in variables, which is more convenient when the characters can be typed or displayed directly.

The choice of method depends on the specific requirements of the program. But generally speaking, when characters can be directly input or displayed, it is simpler and more convenient to use method two; and when characters cannot be directly input or displayed, method one needs to be used.

Method 1: Use Unicode escape sequences

One way to store Unicode characters in Java is to use Unicode escape sequences. An escape sequence is a sequence of characters representing special characters. In Java, a Unicode escape sequence begins with the character "\u", followed by four hexadecimal digits that represent the Unicode code point of the desired character.

public class UnicodeCharacterLiteral {
   public static void main (String[]args)
   {   		 
      //Unicode escape sequence
      char unicodeChar = '\u0041';
      // point for 'A'
      System.out.println("Stored Unicode Character: " + unicodeChar);
   }
}
Copy after login

Output

Stored Unicode Character: A
Copy after login
Copy after login

In the above code snippet, the Unicode escape sequence "\u0041" represents the character "A". The escape sequence is assigned to the char variable unicodeChar, and the stored characters are printed to the console.

Method 2: Directly store Unicode characters

Alternatively, you can store Unicode characters directly in a char variable by enclosing the characters in single quotes. However, this method may not be feasible for characters that cannot be entered directly using the keyboard or are invisible, such as control characters.

public class UnicodeCharacterLiteral {
   public static void main(String[] args) { // Storing Unicode character directly
      char unicodeChar = 'A';
      // Directly storing the character 'A'
      System.out.println("Stored Unicode Character: " + unicodeChar);
   }
}
Copy after login

Output

Stored Unicode Character: A
Copy after login
Copy after login

In this example, the character "A" is directly enclosed in single quotes and assigned to the char variable unicodeChar. The stored characters are then printed to the console.

Working example 1: Storing and printing various Unicode characters

public class UnicodeCharacterExamples {
   public static void main(String[] args) {
      // Storing Unicode characters using escape sequences
      char letterA = '\u0041';
      char letterSigma = '\u03A3';
      char copyrightSymbol = '\u00A9';
      // Storing Unicode characters directly
      char letterZ = 'Z';
      char letterOmega = 'Ω';
      char registeredSymbol = '®';
      // Printing the stored Unicode characters
      System.out.println("Stored Unicode Characters using Escape Sequences:");
      System.out.println("Letter A: " + letterA);
      System.out.println("Greek Capital Letter Sigma: " + letterSigma);
      System.out.println("Copyright Symbol: " + copyrightSymbol);
      System.out.println("\nStored Unicode Characters Directly:");
      System.out.println("Letter Z: " + letterZ);
      System.out.println("Greek Capital Letter Omega: " + letterOmega);
      System.out.println("Registered Symbol: " + registeredSymbol);
   }
}
Copy after login

Output

Stored Unicode Characters using Escape Sequences:
Letter A: A
Greek Capital Letter Sigma: Σ
Copyright Symbol: ©

Stored Unicode Characters Directly:
Letter Z: Z
Greek Capital Letter Omega: Ω
Registered Symbol: ®
Copy after login

Java program that uses character literals to store Unicode characters

Worked Example 2: Manipulating Unicode Characters

This example demonstrates how to manipulate stored Unicode characters. It calculates the difference between uppercase letter "A" and lowercase letter "a" and uses that difference to calculate uppercase letter "C". It then calculates the lowercase "c" by adding 32 to the Unicode code point of the uppercase "C". The Unicode characters manipulated are printed to the console.

public class UnicodeCharacterManipulation {
   public static void main(String[] args) {
      // Storing Unicode characters using escape sequences
      char letterA = '\u0041';
      char letterSmallA = '\u0061';
      // Storing Unicode characters directly
      char letterB = 'B';
      char letterSmallB = 'b';
      // Manipulating the stored Unicode characters
      int difference = letterA - letterSmallA;
      char letterC = (char) (letterB + difference);
      char letterSmallC = (char) (letterC + 32);
      // Printing the manipulated Unicode characters
      System.out.println("Manipulated Unicode Characters:");
      System.out.println("Difference between A and a: " + difference);
      System.out.println("Calculated Letter C: " + letterC);
      System.out.println("Calculated Letter c: " + letterSmallC);
   }
}
Copy after login

Output

Manipulated Unicode Characters:
Difference between A and a: -32
Calculated Letter C: C
Calculated Letter c: c
Copy after login

in conclusion

In Java, you can store Unicode characters using character literals by using Unicode escape sequences or directly enclosing the characters in single quotes. Both methods have their advantages and limitations. Escape sequences provide a consistent way to represent any Unicode character in source code, and when working with characters that can be easily typed or displayed, it is more convenient to store the character directly.

This article provides an algorithm for storing Unicode characters in Java, discusses two different methods of storing these characters, and demonstrates working examples of each method. Understanding these technologies will help developers create applications that work seamlessly with different languages ​​and scripts, and take advantage of the power of Unicode in Java programming.

The above is the detailed content of Java program that uses character literals to store Unicode characters. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template