为什么 ch1 == ch2 是 false,它不保存相同的 char 值吗?

发布于 2025-01-20 01:30:09 字数 397 浏览 0 评论 0原文

我正在尝试比较两个char原始ch1和ch2。两者都分配了值1,如下所示。

但是,当使用“ ==”操作员进行比较时,它会返回false,我不明白幕后如何或发生了什么。

char ch1 = (char)1;
char ch2 = '1';
System.out.println(ch1==ch2); //false

//further comparisions
System.out.println(ch1 == 1);       //true
System.out.println(ch1 == '\u0031'); //false

System.out.println(ch2 == 1);       //false
System.out.println(ch2 == '\u0031'); //true

I'm trying to compare two char primitives ch1 and ch2. Both are assigned the value 1 as shown below.

But when compared using the "==" operator it returns false, which I don't understand how or what's happening behind the scenes.

char ch1 = (char)1;
char ch2 = '1';
System.out.println(ch1==ch2); //false

//further comparisions
System.out.println(ch1 == 1);       //true
System.out.println(ch1 == '\u0031'); //false

System.out.println(ch2 == 1);       //false
System.out.println(ch2 == '\u0031'); //true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白鸥掠海 2025-01-27 01:30:09

'1' 的值为 49(31 十六进制)。

(char)1 的值为 1。

char 只是一个 16 位整数。符号 'x' 表示“字符 x 的字符代码”,其中 Java 中使用的编码是 Unicode,特别是 UTF-16。

强制转换 (char) 不会更改其右侧表达式的值,只是将其从全长整数截断为 16 位(对于 0 到 65535 的值没有变化)。

'1' has the value 49 (31 hexadecimal).

(char)1 has the value 1.

A char is just a 16-bit integer. The notation 'x' means 'the character code for the character x', where the encoding used in Java is Unicode, specifically UTF-16.

The cast (char) does not change the value of the expression to its right, except that it truncates it from a full-size integer to 16 bits (which is no change for values 0 to 65535).

黯淡〆 2025-01-27 01:30:09

基本上,您正在做的是将第一名作为char施放,因此ch1现在等于Unicode字符1(SOH或标题开始),

因此当您将CH1(SOH)与CH2进行比较时('1) ')它将返回假
如果将CH1(SOH -\ U0001)与`'1' - \ U0031进行比较,将返回false,

这是返回false的主要原因,您期望的CH1的Unicode值与您分配的一个不同

Basically what you are doing is casting the number one as a char, so ch1 is now equals to unicode character 1 (SOH or Start of Header)

So when you compare ch1 (SOH) to ch2 ('1') its going to return false
As well if you compare ch1 (SOH - \u0001) to `'1' - \u0031 is going to return false

That's the main reason why is returning false, the unicode value of ch1 that you expect is different from the one you assigned

梦里兽 2025-01-27 01:30:09

代码点

char类型基本上是损坏的,因为Java 2在物理上无能力代表大多数字符。

而是使用代码点整数数字。每个字符都是永久分配一个特定数字,一个代码点。

int codePoint = "1".codePointAt( 0 ) ;  // Annoying zero-based index counting. 

结果是49个十进制,31个十六进制。

根据代码点制作该单个字符的字符串。

String s = Character.toString( codePoint ) ;

或更具体地说:

String latinDigitOneCharacter = Character.toString( 49 ) ;

正如其他人指出的那样,您的代码被错误地比较了定义为拉丁数字“ 1”的字符与1的代码点。

分配给一个代码点的字符是控制代码 soh,soh,启动。在Unicode和Us-Ascii中都是如此(Unicode是US-ASCII的超集)。

Code point

The char type is essentially broken since Java 2, physically incapable of representing most characters.

Instead use code point integer numbers. Every character is permanently assigned a specific number, a code point.

int codePoint = "1".codePointAt( 0 ) ;  // Annoying zero-based index counting. 

The result is 49 decimal, 31 hexadecimal.

Make a string of that single character per the code point.

String s = Character.toString( codePoint ) ;

Or more specifically:

String latinDigitOneCharacter = Character.toString( 49 ) ;

As others pointed out, your code was mistakenly comparing the character defined as the Latin digit “1” with a code point of 1.

The character assigned to the code point of one is the control code SOH, Start of Heading. This is true in both Unicode and US-ASCII (Unicode is a superset of US-ASCII).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文