简体   繁体   English

包装器类作为方法返回类型

[英]Wrapper class as method return type

Can someone please explain how/why is this allowed in Java? 有人可以解释一下Java为什么/为什么允许这样做吗?

public class Test {
private int text;

public Integer getText() {
    return text;
}

I am basically having the wrapper class as the return type, while I am infact returning a primitive. 实际上,我实际上是将包装器类作为返回类型,而实际上却在返回一个基元。

Because Java supports Autoboxing and Unboxing in versions 5 and up. 因为Java在版本5及更高版本中支持自动装箱和取消装箱 That is an example of the former, but the later is equally important (and the reverse conversion). 那是前者的一个例子,但后者同样重要(和反向转换)。 Per the link, 根据链接,

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. 自动装箱是Java编译器在原始类型及其对应的对象包装器类之间进行的自动转换。

Consider the following code: (From: http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html ) 考虑以下代码:(来自: http : //docs.oracle.com/javase/tutorial/java/data/autoboxing.html

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(i);

Although you add the int values as primitive types, rather than Integer objects, to li , the code compiles. 尽管您将int值作为基本类型(而不是Integer对象)添加到li ,但是代码仍会编译。 Because li is a list of Integer objects, not a list of int values, you may wonder why the Java compiler does not issue a compile-time error. 由于li是一个Integer对象的列表,而不是一个int值的列表,因此您可能想知道Java编译器为什么不发出编译时错误。 The compiler does not generate an error because it creates an Integer object from i and adds the object to li . 编译器不会产生错误,因为它会从i创建一个Integer对象并将该对象添加到li Thus, the compiler converts the previous code to the following at runtime: 因此,编译器在运行时将先前的代码转换为以下代码:

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(Integer.valueOf(i));

Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. 将原始值(例如int)转换为相应包装类(Integer)的对象的过程称为自动装箱。 The Java compiler applies autoboxing when a primitive value is: 当原始值是:Java编译器将应用自动装箱:

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class. 作为参数传递给需要相应包装类对象的方法。
  • Assigned to a variable of the corresponding wrapper class. 分配给相应包装器类的变量。

From javadocs : Since java 5 Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. 来自javadocs :由于Java 5 Autoboxing是Java编译器在原始类型及其对应的对象包装器类之间进行的自动转换。 For example, converting an int to an Integer, a double to a Double, and so on. 例如,将int转换为Integer,将double转换为Double,依此类推。

The java compiler applies autoboxing usually when - Java编译器通常在以下情况下应用自动装箱-

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class. 作为参数传递给需要相应包装类对象的方法。

  • Assigned to a variable of the corresponding wrapper class. 分配给相应包装器类的变量。

For the sake of performance, not everything in Java is an object. 为了提高性能,并不是Java中的所有对象都是对象。 There are also primitives, such as int, long, float, double, etc. 还有一些原语,例如int,long,float,double等。

For example java.lang.Integer class :- 例如 java.lang.Integer类:-

  1. It wraps an int. 它包装一个int。
  2. Has two static final fields of type int: MIN_VALUE and MAX_VALUE. 具有int类型的两个静态final字段:MIN_VALUE和MAX_VALUE。
  3. MIN_VALUE contains the minimum possible value for an int (-2^31) and MAX_VALUE the maximum possible value for an int (2^31 - 1). MIN_VALUE包含一个int的最小值(-2 ^ 31),MAX_VALUE包含一个int的最大值(2 ^ 31-1)。
  4. It also has two constructors - public Integer (int value) & public Integer (String value). 它还具有两个构造函数-public Integer(int值)和public Integer(字符串值)。
  5. Integer has the no-arg byteValue, doubleValue, floatValue, intValue, longValue, and shortValue methods that convert the wrapped value to a byte, double, float, int, long, and short, respectively. Integer具有无参数的byteValue,doubleValue,floatValue,intValue,longValue和shortValue方法,这些方法将包装后的值分别转换为字节,double,float,int,long和short。
  6. In addition, the toString method converts the value to a String. 另外,toString方法将值转换为String。
  7. Static methods parse a String to an int (parseInt) and convert an int to a String (toString). 静态方法将String解析为int(parseInt),然后将int转换为String(toString)。

class AutoBox { public static void main(String args[]) { // autobox an int Integer a = 100; // auto-unbox int b = a; System.out.println(b + " " + a); // displays 100 100 } }

This feature has been added in Java 5. Java 5中已添加此功能。

text gets converted automatically into Integer by compiler. 文本会由编译器自动转换为Integer。 So basically its a syntactic sugar that can shorten your code (otherwise you would do conversions back and forth by yourself). 因此,从根本上讲,它是一种语法糖,可以缩短您的代码(否则,您将自己进行来回转换)。 Of course it has its price and if this happens a lot (I mean really a lot, big loops, frequent invocations and so forth), it can become a performance issue, so when using it, just remember the it happens under the hood and you'll be fine. 当然,它有其价格,如果这种情况发生很多(我的意思是很多事情,大循环,频繁的调用等),它可能会成为性能问题,因此在使用它时,请记住它是在引擎盖下发生的,并且你会没事的。

Integer.valueOf(text)

is called under the hood 被称为幕后

The feature is called autoboxing btw 该功能称为自动装箱

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM