为什么String没有原始类型? [重复]

时间:2021-12-15 16:30:39

This question already has an answer here:

这个问题在这里已有答案:

Why doesn't Java have a primitive type for String when most of the other data types do?

当大多数其他数据类型执行时,为什么Java没有String的基本类型?

3 个解决方案

#1


51  

String is an object, it isn't a primitive type at all, just an array of chars. The reason why primitive types exist in Java at all is an interesting one, excerpt from a James Gosling interview:

String是一个对象,它根本不是一个原始类型,只是一个字符数组。原始类型存在于Java中的原因很有意思,摘自James Gosling的采访:

Bill Venners: Why are there primitive types in Java? Why wasn't everything just an object?

Bill Venners:为什么Java中存在原始类型?为什么不是一切都只是一个对象?

James Gosling: Totally an efficiency thing. There are all kinds of people who have built systems where ints and that are all objects. There are a variety of ways to do that, and all of them have some pretty serious problems. Some of them are just slow, because they allocate memory for everything. Some of them try to do objects where sometimes they are objects, sometimes they are not (which is what the standard LISP system did), and then things get really weird. It kind of works, but it's strange.

詹姆斯戈斯林:完全是一种效率的东西。有各种各样的人建立了系统,其中包括整体和所有对象。有很多方法可以做到这一点,而且所有这些方法都有一些非常严重的问题。其中一些只是很慢,因为它们为一切分配内存。他们中的一些人试图做有时它们是对象的对象,有时它们不是(这是标准的LISP系统所做的),然后事情变得非常奇怪。它有点工作,但很奇怪。

Just making it such that there are primitive and objects, and they're just different. You solve a whole lot of problems.

只是让它有原始和对象,它们只是不同。你解决了很多问题。

So in short the primitive types exist for efficiency reasons.

因此,简而言之,出于效率原因存在原始类型。

#2


15  

int, char, float, double, etc. all have a fixed length in memory. e.g. a int have 4 bytes, thus 32bits.

int,char,float,double等都在内存中有固定的长度。例如int有4个字节,因此是32位。

but a string can have different length, it is actually an array of char.

但是一个字符串可以有不同的长度,它实际上是一个char数组。

#3


6  

Most programming languages don't consider a string primitive because it's actually an array of characters. Primitive types almost always have a fixed size.

大多数编程语言都不考虑字符串原语,因为它实际上是一个字符数组。原始类型几乎总是具有固定的大小。

I should say though that some people might consider String to be "primitive" because it is built-in. But it's not primitive in the sense of being a basic type as opposed to a composite type. Because a string is an array of characters, it is a composite type.

我应该说有些人可能认为String是“原始的”,因为它是内置的。但它在基本类型意义上并不是原始的,而不是复合类型。因为字符串是一个字符数组,所以它是一个复合类型。

#1


51  

String is an object, it isn't a primitive type at all, just an array of chars. The reason why primitive types exist in Java at all is an interesting one, excerpt from a James Gosling interview:

String是一个对象,它根本不是一个原始类型,只是一个字符数组。原始类型存在于Java中的原因很有意思,摘自James Gosling的采访:

Bill Venners: Why are there primitive types in Java? Why wasn't everything just an object?

Bill Venners:为什么Java中存在原始类型?为什么不是一切都只是一个对象?

James Gosling: Totally an efficiency thing. There are all kinds of people who have built systems where ints and that are all objects. There are a variety of ways to do that, and all of them have some pretty serious problems. Some of them are just slow, because they allocate memory for everything. Some of them try to do objects where sometimes they are objects, sometimes they are not (which is what the standard LISP system did), and then things get really weird. It kind of works, but it's strange.

詹姆斯戈斯林:完全是一种效率的东西。有各种各样的人建立了系统,其中包括整体和所有对象。有很多方法可以做到这一点,而且所有这些方法都有一些非常严重的问题。其中一些只是很慢,因为它们为一切分配内存。他们中的一些人试图做有时它们是对象的对象,有时它们不是(这是标准的LISP系统所做的),然后事情变得非常奇怪。它有点工作,但很奇怪。

Just making it such that there are primitive and objects, and they're just different. You solve a whole lot of problems.

只是让它有原始和对象,它们只是不同。你解决了很多问题。

So in short the primitive types exist for efficiency reasons.

因此,简而言之,出于效率原因存在原始类型。

#2


15  

int, char, float, double, etc. all have a fixed length in memory. e.g. a int have 4 bytes, thus 32bits.

int,char,float,double等都在内存中有固定的长度。例如int有4个字节,因此是32位。

but a string can have different length, it is actually an array of char.

但是一个字符串可以有不同的长度,它实际上是一个char数组。

#3


6  

Most programming languages don't consider a string primitive because it's actually an array of characters. Primitive types almost always have a fixed size.

大多数编程语言都不考虑字符串原语,因为它实际上是一个字符数组。原始类型几乎总是具有固定的大小。

I should say though that some people might consider String to be "primitive" because it is built-in. But it's not primitive in the sense of being a basic type as opposed to a composite type. Because a string is an array of characters, it is a composite type.

我应该说有些人可能认为String是“原始的”,因为它是内置的。但它在基本类型意义上并不是原始的,而不是复合类型。因为字符串是一个字符数组,所以它是一个复合类型。