New Characteristics---autoboxing of J2SE 5.0

Source: Internet
Author: User
Tags array command line expression int size integer reference return string
J2SE autoboxing
As any Java programmer knows, you can ' t put an int (or other primitive value) into a collection. Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is Integer in the case of int). When you take the object out of the "collection", you have the the "Integer" If you need a int, you are must unbox the Integer using the Intvalue method. All of this boxing and unboxing are a pain, and clutters up your code. The autoboxing and unboxing feature automates the process, eliminating the pain and the clutter.

The following example illustrates autoboxing and unboxing, along with generics and the For-each loop. In a mere ten lines's code, it computes and prints a alphabetized frequency table of the words on the command Line.

Import java.util.*;//prints a frequency table of the words on the command Linepublic class frequency {public static void Main (string[] args) {map<string, integer> m = new treemap<string, integer> (); for (String Word:args) {Int Eger freq = M.get (word); M.put (Word, (freq = = null 1:freq + 1)); } System.out.println (M); }}java Frequency If it is to be it's up to me to do the watusi{be=1, Do=1, If=1, is=2, it=2, me=1, the=1, to=3, Up=1, wat Usi=1}

The program's declares a map from String to Integer, associating the number of times a word occurs on the command line With the word. Then it iterates over each word on the command line. For each word, it looks up the word in the map. Then it puts a revised entry for the word into the map. The line so does this (highlighted in green) contains both autoboxing and unboxing. To compute the "new value to associate" with the word, the ' it looks at ' the current value (Freq). If It is null, this is the occurrence of the word and so it puts 1 into the map. Otherwise, it adds 1 to the number of prior occurrences and puts that value into the map. But Of course you cannot put an int into a map, nor can you add one to an Integer. What is really happening are this:in order to add 1 to freq, it are automatically unboxed, resulting in a expression of Ty PE Int. Since both of the alternative expressions in the conditional expression-type int, so are is the too express Ion itself. In order to put this int value into the map and it is automatically boxed into an Integer.

The result ' all ' is ' that ' you can largely ignore the distinction between int and integers, with a few caveats. An Integer expression can have a null value. If your program tries to autounbox NULL, it would throw a nullpointerexception. The = = operator performs reference identity comparisons on integers expressions and value equality on int comparisons Ssions. Finally, there are performance costs associated with boxing and unboxing, even if it was done automatically.

This is another the sample program featuring Autoboxing and unboxing. It is a static factory that takes a int array and returns a List of integers backed by the array. In a mere ten lines ' Code this method provides the ' full richness of the ' List interface atop an int array. All changes to the list of write through to the array and Vice-versa. The lines that use autoboxing or unboxing are highlighted in green:

List adapter for primitive int arraypublic static list<integer> aslist (final int[] a) {return new ABSTRACTLIST&L T;integer> () {public integer get (int i) {return a[i];}//Throws nullpointerexception If val = null public integer Set (int i, Integer val) {Integer oldval = a[i]; A[i] = val; return oldval;} public int size () {return a.length;}}

The performance of the resulting list is likely to being poor, as it boxes or unboxes on every get or set operation. It is plenty fast enough for occasional use, but it would being folly to use it in a performance critical inner loop.

So when should your use autoboxing and unboxing? Use them the There is a "impedance mismatch" between reference types and primitives, for example, where you have to Put numerical values into a collection. It isn't appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical co De. An Integer isn't a substitute for an int; Autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.