java char 类型可以归类为整数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11671908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:00:19  来源:igfitidea点击:

Can the char type be categorized as an integer?

javacchar

提问by Victor S

Just now I read "char is the only unsigned integral primitive type in Java." Does this mean the char is one of the integral types in Java?

刚才我读到“char是Java中唯一的无符号整数原始类型”。这是否意味着 char 是 Java 中的整数类型之一?

Same as in C, recently I have read that C types includes scalar types, function types, union types, aggregate types, and scalar types include pointer types and arithmetic types, then arithmetic types include integral types and floating-point types, the integral types include enumerated types and character types.

和C一样,最近读到C类型包括标量类型、函数类型、联合类型、聚合类型,标量类型包括指针类型和算术类型,然后算术类型包括整数类型和浮点类型,整数类型包括枚举类型和字符类型。

Can the char type really be categorized as a integer both in Java and C?

在 Java 和 C 中,char 类型真的可以归类为整数吗?

回答by Kerrek SB

Yes, a charis an integral type in all the popular languages in which it appears. "Integral" means that its spectrum is discrete and the smallest difference between any two distinct values is 1. The required range of supported values is usually quite small compared to that of other integral types. Computer hardware traditionally treats integers as the fundamental data type; by contrast, arithmetic floating-point types are a more recent and more complicated addition.

是的,achar是它出现的所有流行语言中的一个整数类型。“积分”意味着它的频谱是离散的,任何两个不同值之间的最小差异是1。与其他整数类型相比,所需的支持值范围通常很小。计算机硬件传统上将整数视为基本数据类型;相比之下,算术浮点类型是一种更新且更复杂的加法。

回答by Peter Lawrey

Similar to @aioobe's answer

类似于@aioobe 的回答

int n = 5;
char ch = (char) '0' + 5; // '5'

or the reverse

或者反过来

char ch = '9';
int i = ch - '0'; // 9

or the unusual

或不寻常的

char ch = 'a';
ch += 1; // 'b';

or the odd

或奇怪的

char ch = '0';
ch *= 1.1; // '4' as (char) (48 * 1.1) == '4'

ch %= 16; // `\u0004`
int i = ch; // 4

BTW: From String.hashCode()

顺便说一句:来自 String.hashCode()

// int h = 0;
char val[] = value;
int len = count;

for (int i = 0; i < len; i++) {
    h = 31*h + val[off++];
}

回答by Justin Ethier

According to the Java Primitive Data Typestutorial:

根据 Java原始数据类型教程:

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

char:char 数据类型是单个 16 位 Unicode 字符。它的最小值为 '\u0000'(或 0),最大值为 '\uffff'(或 65,535)。

So yes, it is a 16-bit unsigned integer. Whether you use the type to represent a number or a character is up to you...

所以是的,它是一个 16 位无符号整数。是使用类型来表示数字还是字符取决于您...



还要记住,虽然charchar类型在 Java 中保证为 16 位,但 C 强加的唯一限制是类型必须至少为 8 位。根据来自的 C 规范参考this answer这个答案

maximum number of bits for smallest object that is not a bit-field (byte)

CHAR_BIT 8

不是位域(字节)的最小对象的最大位数

字符位 8

So a charin C does not necessarily represent the same range of integer values as a charin Java.

因此char,C 中的a不一定表示与charJava 中的a 相同的整数值范围。

回答by aioobe

I'm unsure of the formal definition of an integral type, but in short, yes, charis an integral type in Java, since it can be seen as representing an integer.

我不确定整数类型的正式定义,但简而言之,是的,它char是 Java 中的整数类型,因为它可以被视为表示一个整数。

You can for instance do

例如你可以做

char c1 = 'a' + 'b';

char c2 = 5;

char c3 = c2 + 3;

int i = c3;
char c4 = (char) i;

and so on.

等等。

回答by linsek

In memory, essentially everything is integral... but yes. char is an integer type in C, C++ and Java.

在内存中,基本上一切都是不可或缺的......但是是的。char 是 C、C++ 和 Java 中的整数类型。

回答by Buggieboy

The C specification calls for the chartype to be implemented as a 1-byte integer. The specifications for other types are not always so clear. For example, the original Kernigan and Ritchie C book said that, as far as shortand longwere concerned, among different implementations, you could only rely on the fact that shortwas no longer than long.

C 规范要求将char类型实现为 1 字节整数。其他类型的规格并不总是那么清楚。例如,原 Kernigan 和 Ritchie C 的书说,就shortlong而言,在不同的实现中,您只能依赖于short不长于long的事实。

回答by Debarshi Majumdar

Char is an "Integer Data Type", because a variable of this Data Type uses integers (in Binary) to represent Character Data, as standardized by ASCII.

Char 是一种“整数数据类型”,因为这种数据类型的变量使用整数(二进制)来表示字符数据,如 ASCII 所标准化。

For example:

例如:

As per ASCII standard, the letter "x" is stored as 01111000 (decimal 120).

按照 ASCII 标准,字母“x”存储为 01111000(十进制 120)。

in C Language:

在 C 语言中:

int main()
{
    char a='x';
    int b=3+a;
    printf("value is:\n%d",b);
    return 0;
 }

output: b=123 (3 + 120 for x)

输出:b=123(x 为 3 + 120)

回答by Duracell De Monaco

Yes a char type can be categorized as an integer:

是的,char 类型可以归类为整数:

int i = 49;
int k = 36;

System.out.print((char)i + (char)k + "$"); //may expect 1$ but print: 85$