banner image
Sedang Dalam Perbaikan

Conversion and Casting in Java


Hello guys! What's up? Today I have a come up with another Java tutorial. Conversion and Casting are another two important concepts in Java but which are not discussed much. These concepts are related to converting variables from one type to another. You should have the basic knowledge on primitive data types, sizes and ranges. I will display those using a table. If you are not familiar with them, please refer the table.


Let's start!

Conversion

Mainly there are two types of conversion. They are Wide Conversion and Narrow Conversion. 

Wider Conversion

This is the type of conversion that is done implicitly by Java itself. This means assigning a value in a variable which has a shorter range respectively, to a variable which has a larger range respectively. Didn't you got it?
Ex: Assigning an integer value to a double.
       Assigning a byte value to a short.

These kind of assignments are always legal and no need to convert explicitly.

class Demo{
public static void main(String args[]){
//wider conversion
byte b=100;
double d;
d=b;
System.out.println(b+" "+d);
}
}

Output:

Look here...

What is the way of assigning a value to a variable? Simply the value/variable in the right side from the equal sign is assigned to the left side. When we declare and initialize a variable as int a = 100, this integer a can get any value within the range of integers. Not just 100! This is valid for any type of variable and not only for integers. I took it as an example only.. Remember it..

First 100 is assigned to byte variable called b. Then a new double variable named as d has been declared but not initialized. Next, the existing value in b is assigned to d. Now look at the Data Types table. The range and size of a double variable is always larger than a byte variable! So this can be done without an issue. Actually we don't want to do this..JVM together with Compiler does the job!

Narrow Conversion

What is narrow conversion? Simply the opposite of wider conversion. This means assigning a value in a variable which has a larger range respectively, to a variable which has a shorter range respectively. 
Ex: Assigning a double value to a integer.
       Assigning a short value to a byte.

Is this legal? NOOOOO... How to assign a value with 16 bits to a variable with 8 bits? Some time it can be done..But not always! Previously I told you that when we assigning a value existing to another variable, it checks for data type range. Not the value! If range is OK, then no errors! But if the new variable has a range shorter than the existing one, then you will get errors.



So we can not perform narrow conversion! TO overcome this problem we have to use Casting...Conversion is no more...


Casting

Mainly there are two types of casting. They are Wide Casting and Narrow Casting. 


Narrow Casting

This operation is done to narrow the range of a variable as we wanted to do in Narrow Conversion. We can assign a a value in a variable which has a larger range respectively, to a variable which has a shorter range respectively. But there's a way to do it! We can not assign it simply using a "=" mark..
How to do it? This is the SYNTAX.

short range variable = (short range data type)large range variable 

Example:

class Demo{
public static void main(String args[]){
//narrow casting
double d = 12.3456;
int x;
x=(int)d;
System.out.println(x+" "+d);
}
}

Output:

You can see now, I have converted a value in the double variable into an integer! This is Casing basically!

Wider Casting

This is the last type..Wider Casting..What is this? Actually this means the process of widening a range of a variable.
How to do it? This is the SYNTAX.

large range variable = (large range data type)short range variable 

Let's see with an example.

class Demo{
public static void main(String args[]){
int total=567; //total marks
int n=10; //no of students
double avg;
avg=total/n;
System.out.println("average : "+avg);

// apply wider casting
avg=(double)total/n;
System.out.println("average : "+avg);

// apply wider casting
avg=total/(double)n;
System.out.println("average : "+avg);

avg=(double)(total/n);
System.out.println("average : "+avg);
}
}

Output:

You can see in the code, avg is a double and total and n are integers. In the first printing process, avg is assigned to the result of total/n where total/n is always gives 56 rather than giving 56.7. So it does not return the actual answer and only gives the answer in double format. Because in Java "/" operator does not divide the integers completely! It divides numbers and return the answer in the integer form.  In the second printing process, total integer has been casted into a double and then divided by n. In third one n has been catsed into a double and avg is divided by the casted n. In both these second and third processes, avg is considered as a double exactly due to this casting and returns the correct answer. In the last process, total/n value is casted into a double...But you know int/int always returns an integer. So it does not return the actual answer and only gives the answer in double format.

Now we have come to the end of this tutorial. I wanted to discuss this Conversion and Casting since they are not touched usually in Java tutorials. They are useful sometimes and are not unforgettable. I think I have done a fair job...If not please let me know. So try to understand this concept and practice some examples. Then you will be familiar with these. 

Good Luck!



Conversion and Casting in Java Conversion and Casting in Java Reviewed by DAL on March 18, 2018 Rating: 5

No comments:

Powered by Blogger.