Table of Contents
algorithm
method
Method 1: Use Unicode escape sequences
Output
Method 2: Directly store Unicode characters
Working example 1: Storing and printing various Unicode characters
Worked Example 2: Manipulating Unicode Characters
in conclusion
Home Java javaTutorial Java program that uses character literals to store Unicode characters

Java program that uses character literals to store Unicode characters

Sep 02, 2023 pm 09:45 PM
unicode characters java program character literal

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Java program used to check if TPP students are eligible for interviews Java program used to check if TPP students are eligible for interviews Sep 06, 2023 pm 10:33 PM

Please consider the table below to know the eligibility criteria of different companies - The Chinese translation of CGPA is: GPA greater than or equal to 8 Eligible companies Google, Microsoft, Amazon, Dell, Intel, Wipro greater than or equal to 7 Tutorial points, accenture, Infosys , Emicon, Rellins greater than or equal to 6rtCamp, Cybertech, Skybags, Killer, Raymond greater than or equal to 5Patronics, Shoes, NoBrokers Let us enter the java program to check the eligibility of tpp students for interview. Method 1: Using ifelseif condition Normally when we have to check multiple conditions we use

Java program opens command prompt and inserts command Java program opens command prompt and inserts command Aug 19, 2023 pm 12:29 PM

ThisarticleusesvariousapproachesforselectingthecommandsinsertedintheopenedcommandwindowthroughtheJavacode.Thecommandwindowisopenedbyusing‘cmd’.Here,themethodsofdoingthesamearespecifiedusingJavacode.TheCommandwindowisfirstopenedusingtheJavaprogram.Iti

Java program to get the size of a given file in bytes, kilobytes and megabytes Java program to get the size of a given file in bytes, kilobytes and megabytes Sep 06, 2023 am 10:13 AM

The size of a file is the amount of storage space that a specific file takes up on a specific storage device, such as a hard drive. The size of a file is measured in bytes. In this section, we will discuss how to implement a java program to get the size of a given file in bytes, kilobytes and megabytes. A byte is the smallest unit of digital information. One byte equals eight bits. One kilobyte (KB) = 1,024 bytes, one megabyte (MB) = 1,024KB, one gigabyte (GB) = 1,024MB and one terabyte (TB) = 1,024GB. The size of a file usually depends on the type of file and the amount of data it contains. Taking a text document as an example, the file size may be only a few kilobytes, while a high-resolution image or video file may be

Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program Calculate interest on fixed deposits (FDs) and fixed deposits (RDs) using inherited Java program Aug 20, 2023 pm 10:49 PM

Inheritance is a concept that allows us to access the properties and behavior of one class from another class. The class that inherits methods and member variables is called a superclass or parent class, and the class that inherits these methods and member variables is called a subclass or subclass. In Java, we use "extends" keyword to inherit a class. In this article, we will discuss a Java program to calculate interest on fixed deposits and time deposits using inheritance. First, create these four Java files - Acnt.java − in your local machine IDE. This file will contain an abstract class ‘Acnt’ which is used to store account details like interest rate and amount. It will also have an abstract method 'calcIntrst' with parameter 'amnt' for calculating

Write a Java program to calculate the area and perimeter of a rectangle using the concept of classes Write a Java program to calculate the area and perimeter of a rectangle using the concept of classes Sep 03, 2023 am 11:37 AM

The Java language is one of the most commonly used object-oriented programming languages ​​in the world today. The concept of classes is one of the most important features of object-oriented languages. A class is like a blueprint for an object. For example, when we want to build a house, we first create a blueprint of the house, in other words, we create a plan that shows how we are going to build the house. According to this plan we can build many houses. Likewise, using classes, we can create many objects. Classes are blueprints for creating many objects, where objects are real-world entities like cars, bikes, pens, etc. A class has the characteristics of all objects, and the objects have the values ​​of these characteristics. In this article, we will write a Java program to find the perimeter and faces of a rectangle using the concept of classes

JAVA program to convert Roman numerals to integer numbers JAVA program to convert Roman numerals to integer numbers Aug 25, 2023 am 11:41 AM

Roman Numerals - Based on the ancient Roman system that uses symbols to represent numbers. These numbers are called Roman numerals. The symbols are I, V, X, L, C, D and M, which represent 1, 5, 10, 50, 100, 500 and 1,000 respectively. Integers - An integer is an integer consisting of positive, negative and zero values. Fractions are not whole numbers. Here we set the symbol value based on the integer value. Whenever a Roman numeral is given as input, we divide it into units and then calculate the appropriate Roman numeral. I-1II–2III–3IV–4V–5VI–6…X–10XI–11..XV-15 In this article, we will learn how to convert Roman numerals to integers in Java. Show you some examples - Example 1InputR

Java program to rotate image Java program to rotate image Sep 01, 2023 pm 04:25 PM

An image file can be rotated clockwise or counterclockwise. To rotate an image, you need to download a random image file and save it in any folder on your system. Also, a .pdf file is required and after opening the downloaded image file, some angle can be rotated in that particular .pdf file. For a 90 degree rotation, the anchor points of the new image can help us perform the rotation using the translation transform in Java. The anchor point is the center of any particular image. AlgorithmtoRotateanImagebyUsingJavaThe"AffineTransformOp"classisthesimplestwaytorotatea

Java program to create pyramids and patterns Java program to create pyramids and patterns Sep 05, 2023 pm 03:05 PM

If anyone wants to get a solid foundation in Java programming language. Then, it is necessary to understand how the loop works. Furthermore, solving Pyramid Pattern problems is the best way to enhance your knowledge of Java basics as it includes extensive use of for and while loops. This article aims to provide some Java programs to print pyramid patterns with the help of different types of loops available in Java. Java Program to Create Pyramid Pattern We will print the following pyramid patterns through Java program - Inverted Star Pyramid Star Pyramid Number Pyramid Let's discuss one by one. Mode 1: The inverted star pyramid method declares and initializes an integer "n" with the specified number of rows. Next, define the initial count of the space as 0 and the initial count of the star as "n+

See all articles