java中的包装类 [英] Wrapper class in java

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

问题描述

包装类有什么用?我无法理解本主题中的包装内容。它包装了原始数据类型,但它包含了什么以及如何包装?当他们告诉包装它真正意味着什么?

What is the use of wrapper class? I can't understand the wrapping thing in this topic. It wraps primitive data types but for what and how it wraps? When they tell wrapping what does it really mean?

推荐答案

如你所知,在Java中并非所有东西都是对象。有原始类型,如int,double float等。所以如果你想使用原始类型代替Object,即如果一个方法需要一个Object,但你需要发送一个原始类型,没有Wrapper类,这将不会是可能。以Map接口为例。 put(Object,Object)方法将对象作为键和值。例如,如果您希望将一个整数值1( int i = 1 )之间的映射存储到一个Object,而没有包装类,那么这是不可能的。当需要对象时,包装类用于表示原始值。

As you are aware, In Java not everything is an Object. There are primitive types such as int ,double float etc. So if you wanted to use a primitive type in place of Object i.e. if a method expects an Object but you need to send in a primitive type, without Wrapper classes this wouldn't be possible. Take for instance the Map interface. The put(Object,Object) method takes Objects as both key and value. If you, for example wanted to store a mapping between an integer value 1 (int i=1) to an Object, without wrapper classes this wouldn't be possible. Wrapper classes are used to represent primitive values when an object is required.

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

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