CASTING


Primitives

Conversion of primitive types may occur in these contexts:

* Assignment
* Method call
* Arithmetic promotion
* Explicit casting

Widening conversions: -

Your browser may not support display of this image.

Assignment

General rules for primitive assignment conversions:

* A boolean may not be converted to any other type.
* A non-boolean may be converted to another non-boolean type, provided the conversion is a widening conversion.
* A non-boolean may not be converted to another non-boolean type, if the conversion would be a narrowing conversion.

Example #1:

int i = 5;

float j = i;

Method call

Widening conversion takes place on method calls as on assignments. You can pass to a method any primitive narrower than the expected one. Implicit casting will naturally occur.

For instance:

public static void main(String args[]) {

byte x = 126;

System.out.println( DoIt(x) );

}

static String DoIt(int a) {

return "I've received an int of value "+a;

}

Result: I've received an int value of 126

The method DoIt(int a) expects an int, but you can throw a char, byte or short there, the value will be promoted to int and the method will IN FACT receive an int.

This special behavior occurs if a method to handle a narrower type hasn't been declared. If you declare a method to handle bytes, then that method will "catch" the call. This is an OOP feature called overloading.

Example:

public static void main(String args[]) {

byte x = 126;

System.out.println( DoIt(x) );

}

static String DoIt(int a) {

return "I've received an int of value "+a;

}

static String DoIt(byte a) {

return "I've received a byte of value "+a;

}

Result: I've received a byte value of 126

If the argument type if wider than expected, no implicit casting will occur and you will need to perform an explicit cast:

public static void main(String args[]) {

float x = 1.26f;

System.out.println( DoIt( (int)x ) );

}

static String DoIt(int a) {

return "I've received an int of value "+a;

}

Result: I've received an int value of 1

Last example:

public static void main(String args[]) {

char x = 'A';

System.out.println( DoIt(x) );

}

static String DoIt(int a) {

return "I've received an int of value "+a;

}

static String DoIt(byte a) {

return "I've received a byte of value "+a;

}

Result: I've received an int value of 65

As you can see, there's no method to catch char types so the value is promoted to int and caught by DoIt(int a).

Arithmetic Promotion

Arithmetic promotion happens when narrower types need to be promoted to wider types in order to make sense in an operation among other wider types.

Basic rules for binary operators and most of the unary operators:

* All arithmetic expressions are promoted to the wider of the operands.
* The promotion is at least to int, even if no int operand appears.

These rules don't apply to the unary operators: ++ and -- and assignment operators.

Example #1:

byte x = 1;

x++; // Ok. x is now equal to 2.

x = x + 1; // Error. Expression x + 1 is promoted to int.

Example #2:

byte a = 1;

byte x = 23;

x <<= a; // Ok. x is now equal to 46.

x = x << a; // Error. Expression x << a is promoted to int.

Example #3:

char a = 5;

short x = 3;

x *= a; // Ok. x is now equal to 15.

x = x * a; // Error. Expression x = x * a is promoted to int.

Example #4:

byte a = 15;

a = -a; // Error. -a is promoted to int.

a = ~a; // Error. ~a is promoted to int.

Example #5:

float a = 1.0f;

int b = 15;

int x = a * b; // Error. Expression is promoted to float.

int x = (int)(a*b); // Ok. We cast the float result back to int.

Primitives and Casting

Casting means explicitly telling Java to make a conversion. A casting operation may widen or narrow its argument. To cast a value, you need to precede it with the name of the desired type enclosed within parentheses:

byte x = 15;

int r = (int)(x * 3);

Booleans cannot be casted. Don't bother with them, stuff like this doesn't work:

byte a = 1;

boolean status = a;

Narrowing runs the risk of loosing information, in fact, many times you know that you are going to loose information and it is important to know which part information is going to be loosen and naturally, what information will be kept.

2 comments:

clover said...

I’m very glad to found this website because; it carries awesome and actually good data in favor of readers.

clover
www.n8fan.net

Unknown said...

I really enjoy this post. I spend great time, reading it. It's interesting topic and it's great, that author decided to write about it. Seems like he's broad minded person. I would like to read his another posts with great pleasure. It can be my inspiration.

www.imarksweb.org

Post a Comment