Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
What is Autoboxing? Category: java.lang, viewed: 24K time(s). Autoboxing is a new feature offered in the Tiger (1.5) release of Java SDK. In short auto boxing is a capability to convert or cast between object wrapper and it's primitive type. Previously when placing a primitive data into one of the Java Collection Framework we have to wrap it to an object because the collection cannot work with primitive data. Also when calling a method that requires an instance of object than an int or long, than we have to convert it too. But now, starting from version 1.5 we were offered a new feature in the Java Language, which automate this process, this is call the Autoboxing. When we place an int value into a collection it will be converted into an Integer object behind the scene, on the other we can read the Integer value as an int type. In most way this simplify the way we code, no need to do an explisit object casting. Here an example how it will look like using the Autoboxing feature: view source print? 01.public static void main(String[] args) 02.{ 03. Map<String, Integer> map = new HashMap<String, Integer>(); 04. 05. // Here we put an int into the Map, and it accepted 06. // as it will be autoboxed or converted into the wrapper 07. // of this type, in this case the Integer object. 08. map.put("Age", 25); 09. 10. // Here we can just get the value from the map, no need 11. // to cast it from Integer to int. 12. int age = map.get("Age"); 13. 14. // Here we simply do the math on the primitive type 15. // and got the result as an Integer. 16. Integer newAge = age + 10; 17.} Autoboxing and Unboxing in Java 5 Java 5 (and hence AspectJ 1.5) supports automatic conversion of primitive types (int, float, double etc.) to their object equivalents (Integer, Float, Double,...) in assignments and method and constructor invocations. This conversion is know as autoboxing. Java 5 also supports automatic unboxing, where wrapper types are automatically converted into their primitive equivalents if needed for assignments or method or constructor invocations. For example: int i = 0; i = new Integer(5); // auto-unboxing Integer i2 = 5; // autoboxing How to sort a list of Strings or Integers in Java Following on from our introduction to sorting in Java, we consider here one of the simplest cases of sorting a List of simple objects such as Strings or Integers. Let's say we have a simple list of Strings: List<String> words = new ArrayList<String>(50); words.add("once"); words.add("upon"); words.add("a"); words.add("time"); ... Now, we can sort this list with a simple call to the following library method: Collections.sort(words); The Collections.sort() method will work "out of the can" on lists of various "basic" objects that you'd expect to be able to sort, including instances of Number– the primitive wrapper classes Integer, Long, Float etc plus BigInteger and BigDecimal. Sorting an array Sorting an array of these objects is just as easy1: Arrays.sort(myArray); The above method takes an array of objects, but the Arrays class also provides similar methods for sorting primitive arrays. Next: other issues What we've seen above is that sorting simple objects in Java such as numbers or strings is generally dead simple. But there are a couple more issues to deal with on the following pages: What if we want to sort a different type of object, such as one that we've created? For this, we need to look at how to use the Java Comparable interface. What if we want to control the ordering of a specific type of sort, even if the objects in question are 'naturally sortable'? For example, we might want to perform a case insensitive sort on Strings, or perform a "proper" alphabetic sort that takes account of things like the correct ordering of accents in non-English strings. For this, we need to look at Java Comparators. In some cases, we may need to consider the performance of the Java sort algorithm: in particular, where we are repeatedly sorting very small lists, a simpler (even if less scalable) algo 1. In fact, the Collections.sort() method copies the list into an array and calls the Arrays.sort() method before copying the elements back into the list.