整数为原始类型 [英] Integer as primitive type

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

问题描述

为什么整数(int)有原始类型,即使我们有一个整数对象为Integer?
但是String类型不一样。 String没有这种原始类型。 Always String处理对象引用?

Why there is primitive type for integer(int) even though we have an object for integer as Integer? But the same is not with String type. There is no such primitive type for String. Always String deals with object reference?

推荐答案

速度。机器代码使用本机CPU指令添加两个int要快得多,而不是必须取两个Integer对象,从中提取int值,然后添加它们,创建一个新结果Integer对象来包含结果。 ( JNI如何映射基元

Speed. It's much faster for machine code to add two int's using native CPU instructions, rather than having to take two Integer objects, extract the int values from them, then add those, creating a new result Integer object to contain the result. (how JNI maps primitives)

字符串很复杂,有很多方法,因此没有机器码对位。它们被提升为真正的对象。此外,String与使用相同值创建的其他字符串共享状态。没有原始值与其他原始值共享状态。 (不可变的共享 | 原始无共享

Strings are complex, have many methods, and as such have no machine code counterpoint. They are promoted to a true Object. Also, a String shares state with other Strings created with the same value. No primitive value shares state with other primitive values like this. (immutable can be shared | primitive no sharing)

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

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