search
HomeJavajavaTutorialJava program that uses character literals to store Unicode characters

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);
   }
}

Output

Stored Unicode Character: A

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);
   }
}

Output

Stored Unicode Character: A

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);
   }
}

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: ®

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);
   }
}

Output

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

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
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Java程序打开命令提示符并插入命令Java程序打开命令提示符并插入命令Aug 19, 2023 pm 12:29 PM

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

Java程序获取给定文件的大小(以字节、千字节和兆字节为单位)Java程序获取给定文件的大小(以字节、千字节和兆字节为单位)Sep 06, 2023 am 10:13 AM

文件的大小是特定文件在特定存储设备(例如硬盘驱动器)上占用的存储空间量。文件的大小以字节为单位来衡量。在本节中,我们将讨论如何实现一个java程序来获取给定文件的大小(以字节、千字节和兆字节为单位)。字节是数字信息的最小单位。一个字节等于八位。1千字节(KB)=1,024字节1兆字节(MB)=1,024KB千兆字节(GB)=1,024MB和1太字节(TB)=1,024GB。文件的大小通常取决于文件的类型及其包含的数据量。以文本文档为例,文件的大小可能只有几千字节,而高分辨率图像或视频文件的大小可

使用类的概念编写Java程序来计算矩形的面积和周长使用类的概念编写Java程序来计算矩形的面积和周长Sep 03, 2023 am 11:37 AM

Java语言是当今世界上最常用的面向对象编程语言之一。类的概念是面向对象语言中最重要的特性之一。一个类就像一个对象的蓝图。例如,当我们想要建造一座房子时,我们首先创建一份房子的蓝图,换句话说,我们创建一个显示我们将如何建造房子的计划。根据这个计划,我们可以建造许多房子。同样地,使用类,我们可以创建许多对象。类是创建许多对象的蓝图,其中对象是真实世界的实体,如汽车、自行车、笔等。一个类具有所有对象的特征,而对象具有这些特征的值。在本文中,我们将使用类的概念编写一个Java程序,以找到矩形的周长和面

使用继承的Java程序来计算定期存款(FDs)和定期存款(RDs)的利息使用继承的Java程序来计算定期存款(FDs)和定期存款(RDs)的利息Aug 20, 2023 pm 10:49 PM

继承是一个概念,它允许我们从一个类访问另一个类的属性和行为。被继承方法和成员变量的类被称为超类或父类,而继承这些方法和成员变量的类被称为子类或子类。在Java中,我们使用“extends”关键字来继承一个类。在本文中,我们将讨论使用继承来计算定期存款和定期存款的利息的Java程序。首先,在您的本地机器IDE中创建这四个Java文件-Acnt.java−这个文件将包含一个抽象类‘Acnt’,用于存储账户详情,如利率和金额。它还将具有一个带有参数‘amnt’的抽象方法‘calcIntrst’,用于计

JAVA程序将罗马数字转换为整数数字JAVA程序将罗马数字转换为整数数字Aug 25, 2023 am 11:41 AM

罗马数字-基于古罗马系统,使用符号来表示数字。这些数字称为罗马数字。符号为I、V、X、L、C、D和M,分别代表1、5、10、50、100、500和1,000。整数-整数就是由正值、负值和零值组成的整数。分数不是整数。这里我们根据整数值设置符号值。每当输入罗马数字作为输入时,我们都会将其划分为单位,然后计算适当的罗马数字。I-1II–2III–3IV–4V–5VI–6...X–10XI–11..XV-15在本文中,我们将了解如何在Java中将罗马数字转换为整数。向您展示一些实例-实例1InputR

Java程序创建金字塔和图案Java程序创建金字塔和图案Sep 05, 2023 pm 03:05 PM

如果有人想在Java编程语言方面打下坚实的基础。然后,有必要了解循环的工作原理。此外,解决金字塔模式问题是增强Java基础知识的最佳方法,因为它包括for和while循环的广泛使用。本文旨在提供一些Java程序,借助Java中可用的不同类型的循环来打印金字塔图案。创建金字塔图案的Java程序我们将通过Java程序打印以下金字塔图案-倒星金字塔星金字塔数字金字塔让我们一一讨论。模式1:倒星金字塔方法声明并初始化一个指定行数的整数“n”。接下来,将空间的初始计数定义为0,将星形的初始计数定义为“n+

Java程序旋转图像Java程序旋转图像Sep 01, 2023 pm 04:25 PM

一个图像文件可以顺时针或逆时针旋转。要旋转图像,需要下载一个随机的图像文件并将其保存在系统的任何文件夹中。此外,需要一个.pdf文件,在打开下载的图像文件后,可以在该特定的.pdf文件中旋转一些角度。对于90度的旋转,新图像的锚点可以帮助我们使用Java中的平移变换执行旋转操作。锚点是任何特定图像的中心。AlgorithmtoRotateanImagebyUsingJavaThe"AffineTransformOp"classisthesimplestwaytorotatea

Java程序用于检查TPP学生是否有资格参加面试Java程序用于检查TPP学生是否有资格参加面试Sep 06, 2023 pm 10:33 PM

请考虑下表了解不同公司的资格标准-CGPA的中文翻译为:绩点平均成绩符合条件的公司大于或等于8谷歌、微软、亚马逊、戴尔、英特尔、Wipro大于或等于7教程点、accenture、Infosys、Emicon、Rellins大于或等于6rtCamp、Cyber​​tech、Skybags、Killer、Raymond大于或等于5Patronics、鞋子、NoBrokers让我们进入java程序来检查tpp学生参加面试的资格。方法1:使用ifelseif条件通常,当我们必须检查多个条件时,我们会使用

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.