Saturday, September 29, 2018

Type Casting or Type Conversion in BlueJ


Casting or type conversion means converting one type of value into other type. There are two types of casting :
1.    Implicit  / Automation casting
2.    Explicit / Forceful casting

Implicit Casting: When variable or variables are automatically converted into other type during execution of any expression, it is known as implicit casting or conversion.

In java, also in other languages, expression execution among different type of variables is not possible. All the variables must be of same type before execution of the expression. What will happen if we use integer type,long type and double type variables in any expression? The value of integer type and long type variables will be automatically converted into double type value before the execution of the expression and this is known as implicit or automatic casting. In implicit casting, values in variables with lower hierarchical order are automatically converted into values of variable type with higher hierarchical order. 

Examples of Implicit Casting

int num1=10;
long num2=100000;
double num3=num1 + num2;

Value of num3 would be 100010.00

Another example:

int a=10;
double b=10.5;
double c=a+b;

Value of c would be 10.50


Explicit Casting

In many cases, two or more same type of variables are used in any expression but the result should be in another type. How to get it? Suppose we want to display the average marks obtained by a student. Here is a program to demonstrate explicit casting.

class Avg
{
public void show (int marks, int no)
{
double av=marks/no;
System.out.print(“Average marks=”+av);
}
}

If total marks obtained by the student is 250 and number of subjects is 3, output of the program will be 83.00

Why so? Integer division gives quotient only. When 250 is divided by 3, it gives 83, not 83.33. 83 is stored in double variable so it is implicitly casted into double. To get the exact result, one of the variables in the expression is to be explicitly casted into double, so that double value divided by int will be ultimately double divided by double (implicit casting) and the result will be in double. So the program would be:

class Avg
{
public void show (int marks, int no)
{
double av=(double) marks/no;
System.out.print(“Average marks=”+av);
}
}

So, explicit casting is the technique where one of the variables in any expression is converted into any specific type so that others will implicitly casted into that type. Syntax of explicit casting is to write the target type within bracket before anyone of the variables in the expression.

What are the advantages and disadvantages of casting or conversion

Advantage: We can get our results as desired through type casting.
                   We can convert one type of variable or object to other type

Disadvantage: Chances of loss in precision

e.g:
int a;
double b=100.25;
a=(int) b;
Now the value of ‘a’ is 100. 0.25 is lost

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner