包装类与原始数据类型 [英] Wrapper Class vs. Primitive datatypes

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

问题描述

如果我们已经拥有原始数据类型,为什么会有包装类?

例如:我们可以创建一个整数变量

int x = 10;

然后我们为什么要使用Wrapper类

整数x = new整数(10);

Why are there Wrapper Classes if we already have Primitive datatypes??
Eg: we can create an integer variable as
int x = 10;
then why would we use Wrapper class as
Integer x = new Integer(10);

推荐答案

原始数据类型是不是Java中的类。因此,它们没有实例变量和方法。这对效率有好处,但似乎迫使我们处于非面向对象的方向。为了适应这种情况,有包装类。
Primitive data types are not classes in Java. Therefore they do not come with instance variables and methods. This is good for efficiency, but seems to force us in a non-object oriented direction. To accommodate this, there are wrapper classes.


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

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