All the rar files have the same 
password : http://learning4you.blogspot.com/

Monday, November 10, 2008

Casting and Converting Objects and Primitive Types in java

Casting and Converting Objects and Primitive Types

Sometimes in your Java programs you may have a value stored somewhere that is the wrong type for what you want to do with it. Maybe it's an instance of the wrong class, or perhaps it's a float and you want it to be an int. To convert the value of one type to another, you use casting. Casting is a programming term that means, effectively, converting a value or an object from one type to another. The result of a cast is a new value or object; casting does not change the original object or value.
New Time
Casting converts the value of an object or primitive type into another type.

Although the concept of casting is a simple one, the rules for what types in Java can be converted to what other types are complicated by the fact that Java has both primitive types (int, float, boolean), and object types (String, Point, Window, and so on). There are three forms of casts and conversions to talk about in this section:

  • Casting between primitive types: int to float or float to double
  • Casting between object types: an instance of a class to an instance of another class
  • Converting primitive types to objects and then extracting primitive values back out of those objects

Casting Primitive Types

Casting between primitive types allows you to "convert" the value of one type to another primitive type-for example, to assign a number of one type to a variable of another type. Casting between primitive types most commonly occurs with the numeric types; boolean values cannot be cast to any other primitive type.

Often, if the type you are casting to is "larger" than the type of the value you're converting, you may not have to use an explicit cast. You can often automatically treat a byte or a character as an int, for example, or an int as a long, an int as a float, or anything as a double automatically. In most cases, because the larger type provides more precision than the smaller, no loss of information occurs when the value is cast. The exception is casting integers to floating-point values; casting an int or a long to a float or a long to a double may cause some loss of precision.

To convert a large value to smaller type, you must use an explicit cast, because converting that value may result in a loss of precision. Explicit casts look like this:

(typename)value
In this form, typename is the name of the type you're converting to (for example: short, int, float, boolean), and value is an expression that results in the value you want to convert. So, for example, in this expression the value of x is divided by the value of y and the result is cast to an int:
(int) (x / y);
Note that because the precedence of casting is higher than that of arithmetic, you have to use parentheses here; otherwise, the value of x would be cast first and then divided by y (which might very well be a very different result).

Casting Objects

Instances of classes can also be cast to instances of other classes, with one restriction: The class of the object you're casting and the class you're casting it to must be related by inheritance; that is, you can cast an object only to an instance of its class's sub- or superclass-not to any random class.

Analogous to converting a primitive value to a larger type, some objects may not need to be cast explicitly. In particular, because subclasses contain all the same information as their superclass, you can use an instance of a subclass anywhere a superclass is expected. (Did you just have to read that sentence four times before you understood it? I had to rewrite it a whole lot of times before it became even that simple. Bear with me, its not that bad. Let's try an example.) Suppose you have a method that takes two arguments: one of type Object, and one of type Number. You don't have to pass instances of those particular classes to that method. For the Object argument, you can pass any subclass of Object (any object, in other words), and for the Number argument you can pass in any instance of any subclass of Number (Integer, Boolean, Float, and so on); you don't have to explicitly convert them first.

Casting downward in the class hierarchy is automatic, but casting upward is not. Converting an instance of a subclass to an instance of a superclass loses the information the original subclass provided and requires an explicit cast. To cast an object to another class, you use the same casting operation that you used for base types:

(classname)object
In this case, classname is the name of the class you want to cast the object to, and object is a reference to the object you're casting. Note that casting creates a reference to the old object of the type classname; the old object still continues to exist as it did before.

Here's a (fictitious) example of a cast of an instance of the class GreenApple to an instance of the class Apple (where GreenApple is theoretically a subclass of Apple with more information to define the apple as green):

GreenApple a;
Apple a2;
a = new GreenApple();
a2 = (Apple) a;
In addition to casting objects to classes, you can also cast objects to interfaces-but only if that object's class or one of its superclasses actually implements that interface. Casting an object to an interface means that you can call one of that interface's methods even if that object's class does not actually implement that interface. You'll learn more about interfaces in Week 3.

Converting Primitive Types to Objects and Vice Versa

Now you know how to cast a primitive type to another primitive type and how to cast between classes. How can you cast one to the other?

You can't! Primitive types and objects are very different things in Java and you can't automatically cast or convert between the two. However, the java.lang package includes several special classes that correspond to each primitive data type: Integer for ints, Float for floats, Boolean for booleans, and so on. Note that the class names have an initial capital letter, and the primitive types are lowercase. Java treats these names very differently, so don't confuse them, or your methods and variables won't behave the way you expect.

Using class methods defined in these classes, you can create an object-equivalent for all the primitive types using new. The following line of code creates an instance of the Integer class with the value 35:

Integer intObject = new Integer(35);
Once you have actual objects, you can treat those values as objects. Then, when you want the primitive values back again, there are methods for that as well-for example, the intValue() method extracts an int primitive value from an Integer object:
int theInt = intObject.intValue();  // returns 35
See the Java API documentation for these special classes for specifics on the methods for converting primitives to and from objects.
Note
In Java 1.0 there are special type classes for Boolean, Character, Double, Float, Integer, and Long. Java 1.1 adds classes for Byte and Short, as well as a special wrapper class for Void. The latter classes are used primarily for object reflection.

0 comments:

Related Posts with Thumbnails