Occasionally, you need to convert a primitive type like int to an object. All primitive typeshave class counterparts. For example, a class Integer corresponds to the primitive type int.
These kinds of classes are usually called wrappers. The wrapper classes have obviousnames: Integer, Long, Float, Double, Short, Byte, Character, Void, and Boolean. (The first six inheritfrom the common superclass Number.) The wrapper classes are immutable—you cannot change a wrapped value after the wrapper has been constructed. They are also final, soyou cannot subclass them.
Suppose we want an array list of integers. Unfortunately, the type parameter inside the angle brackets cannot be a primitive type. It is not possible to form an ArrayList<int>.
Here, the Integer wrapper class comes in. It is ok to declare an array list of Integer objects.
CAUTION: An ArrayList<Integer> is far less efficient than an int array because each valueis separately wrapped inside an object. You would only want to use this construct for small collections when programmer convenience is more important than efficiency.
Another Java SE 5.0 innovation makes it easy to add and get array elements. The callis automatically translated to
This conversion is called autoboxing.
NOTE:You might think that auto wrapping would be more consistent, but the “boxing” metaphorwas taken from C#.
Conversely, when you assign an Integer object to an int value, it is automatically unboxed. That is, the compiler translates
Automatic boxing and unboxing even works with arithmetic expressions. For example, you can apply the increment operator to a wrapper reference:
The compiler automatically inserts instructions to unbox the object, increment the resulting value, and box it back.
In most cases, you get the illusion that the primitive types and their wrappers are one and the same. There is just one point in which they differ considerably: identity. As youknow, the == operator, applied to wrapper objects, only tests whether the objects have identical memory locations. The following comparison would therefore probably fail:
However, a Java implementation may, if it chooses, wrap commonly occurring valuesinto identical objects, and thus the comparison might succeed. This ambiguity is notwhat you want. The remedy is to call the equals method when comparing wrapperobjects.
NOTE: The autoboxing specification requires that boolean, byte, char _ 127, and short andint between –128 and 127 are wrapped into fixed objects. For example, if a and b had been initialized with 100 in the preceding example, then the comparison would have had tosucceed.
Finally, let us emphasize that boxing and unboxing is a courtesy of the compiler, not the virtual machine. The compiler inserts the necessary calls when it generates the byte codesof a class. The virtual machine simply executes those bytecodes.
You will often see the number wrappers for another reason. The designers of Java foundthe wrappers a convenient place to put certain basic methods, like the ones for convertingstrings of digits to numbers.
To convert a string to an integer, you use the following statement:
This has nothing to do with Integer objects —parseInt is a static method. But the Integerclass was a good place to put it.
The API notes show some of the more important methods of the Integer class. The othernumber classes implement corresponding methods.
CAUTION: Some people think that the wrapper classes can be used to implement methods that can modify numeric parameters. However, that is not correct. Recall from previous sections thatit is impossible to write a Java method that increments an integer parameter because parameters to Java methods are always passed by value.
Could we overcome this by using an Integer instead of an int?
The problem is that Integer objects are immutable: the information contained inside the wrapper can’t change. You cannot use these wrapper classes to create a method that modifiesnumeric parameters.
If you do want to write a method to change numeric parameters, you can use one of theholder types defined in the org.omg.CORBA package. There are types Int Holder, BooleanHolder,and so on. Each holder type has a public (!) field value through which you can access thestored value.
Core Java Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||JSP Interview Questions|
|Android Interview Questions||JavaServer Faces (JSF) Interview Questions|
|Java collections framework Interview Questions||Java 8 Interview Questions|
|Java Collections Interview Questions||Java Exception Handling Interview Questions|
|Java Concurrency Interview Questions||Java Serialization Interview Questions|
|Java Programmer Interview Questions||Java Inheritance Interview Questions|
|Java IO Interview Questions||Object Oriented Programming in PHP Interview Questions|
Core Java Tutorial
An Introduction To Java
The Java Programming Environment
Fundamental Programming Structures In Java
Objects And Classes
Interfaces And Inner Classes
User Interface Components With Swing
Deploying Applications And Applets
Exceptions, Logging, Assertions, And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.