为什么String没有原始类型? [英] Why is there no primitive type for String?

查看:162
本文介绍了为什么String没有原始类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Java在大多数其他数据类型中没有String的原始类型?

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

推荐答案

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

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:


Bill Venners:为什么Java中有原始的
类型?为什么不是所有
只是一个对象?

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

James Gosling:完全是一个效率
的东西。有各种各样的人
,他们建立了所有对象的系统和
。有一种
的各种方法可以做到这一点,并且所有
都有一些非常严重的
问题。其中一些只是慢,
因为他们为
所有内容分配内存。他们中的一些尝试做
对象,有时它们是
对象,有时它们不是(b $ b是标准LISP系统做的),
然后事情变得非常奇怪。这是
的一种作品,但它很奇怪。

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.

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

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.

这篇关于为什么String没有原始类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆