简体   繁体   中英

Wrapper class as method return type

Can someone please explain how/why is this allowed in 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. 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.

Consider the following code: (From: 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. 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. The compiler does not generate an error because it creates an Integer object from i and adds the object to 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. The Java compiler applies autoboxing when a primitive value is:

  • 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. For example, converting an int to an Integer, a double to a Double, and so on.

The java compiler applies autoboxing usually when -

  • 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. There are also primitives, such as int, long, float, double, etc.

For example java.lang.Integer class :-

  1. It wraps an int.
  2. Has two static final fields of type int: MIN_VALUE and 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).
  4. It also has two constructors - public Integer (int value) & public Integer (String value).
  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.
  6. In addition, the toString method converts the value to a String.
  7. Static methods parse a String to an int (parseInt) and convert an int to a 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.

text gets converted automatically into Integer by compiler. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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