I am new to JAVA and would like to ask what is the difference between data type and object. To be specific, I would like to ask what is the difference between data type (float) and object (Float) ? What is the advantage of using object Float, instead of type float? Can you give me some examples?
我是JAVA的新手,想问一下数据类型和对象有什么区别。具体来说,我想问一下数据类型(float)和object(Float)有什么区别?使用对象Float的优点是什么,而不是float类型?你能举个例子吗?
In addition, what is the advantage of using type float, instead of object Float?
另外,使用float类型而不是对象Float有什么好处?
Moreover, can I store multiple float values into object Float?
此外,我可以将多个浮点值存储到对象Float中吗?
1 个解决方案
#1
3
Float is a wrapper class. Wrapper classes are generally used to represent a primitive type (In Java primitive types are byte, int, float, etc.) when an Object is required. All of the wrapper classes inherit from the Number class.
Float是一个包装类。当需要Object时,包装类通常用于表示基本类型(在Java原始类型中是byte,int,float等)。所有包装类都继承自Number类。
The advantage of using a wrapper object like Float is that in some cases you will be able to use Float
where you couldn't use float
. For instance Lists such as an ArrayList can only hold reference types (i.e. objects, not primitives).
使用像Float这样的包装器对象的优点是,在某些情况下,您可以在不能使用float的情况下使用Float。例如,诸如ArrayList之类的列表只能保存引用类型(即对象,而不是基元)。
You can go:
你可以走了:
ArrayList<Float>....
but using
ArrayList<float>....
won't work and will result in a syntax error.
将无法正常工作,并将导致语法错误。
Oracle has a tutorial on this
Oracle有一个关于此的教程
#1
3
Float is a wrapper class. Wrapper classes are generally used to represent a primitive type (In Java primitive types are byte, int, float, etc.) when an Object is required. All of the wrapper classes inherit from the Number class.
Float是一个包装类。当需要Object时,包装类通常用于表示基本类型(在Java原始类型中是byte,int,float等)。所有包装类都继承自Number类。
The advantage of using a wrapper object like Float is that in some cases you will be able to use Float
where you couldn't use float
. For instance Lists such as an ArrayList can only hold reference types (i.e. objects, not primitives).
使用像Float这样的包装器对象的优点是,在某些情况下,您可以在不能使用float的情况下使用Float。例如,诸如ArrayList之类的列表只能保存引用类型(即对象,而不是基元)。
You can go:
你可以走了:
ArrayList<Float>....
but using
ArrayList<float>....
won't work and will result in a syntax error.
将无法正常工作,并将导致语法错误。
Oracle has a tutorial on this
Oracle有一个关于此的教程