129k views
1 vote
Difference between implicit and explicit type casting

User Saulyasar
by
5.3k points

2 Answers

2 votes

.Answer: When one data type is converted to another data type it is called typecasting. Typecasting can be both implicit and explicit.

Implicit typecasting is done by the compiler without the involvement of the programmer. However, explicit typecasting is done by the programmer whereby it asks the compiler to make the desirable data type changes.

Step-by-step explanation:

Examples of implicit typecasting are:

int a = 2;

float b= 3.14;

Here, the compiler can make the desired changes.

Examples of explicit typecasting :

double a = 4;

int b = (int)a;

Here, the programmer is explicitly asking the compiler to make the changes in the data type.

User DWX
by
4.5k points
3 votes

Answer:

Implicit type casting means the type casting done by the compiler during compilation of program whereas Explicit type casting means the programmer explicitly type casts the variables. This is the difference implicit and explicit type casting.

Explanation:

Type casting means the conversion of one data type into another data type. There are two types of type casting. They are Implicit type casting and Explicit type casting.

Implicit type casting: The type casting which is done by the compiler during the compilation of a program. This occurs when we assigning smaller data type to larger data type and also both the data type are compatible. Below example clears the above concepts.

Ex:
int \, i = 10;


long \, l = i;

In the above example " i " is " int " data type whereas " l " is long data type but as we assigned the value of " i " to " l ". Here " int " and " long " are compatible data types and we assigned smaller data type ( int - i ) to larger data type ( long - l ) implicit type casting occurs.

Explicit type casting: The type casting which is done by the programmer explicitly to convert one data type into other data type. If we want to assign a value of larger data type to smaller data type explicit type casting is required. Below example clears the above concepts.

Ex:
double \, d = 10.4;


long\, l = (long) d;

In the above example " d " is " double " data type whereas " l " is " long " data type. Here we are explicitly type casting to change larger data value ( double - d ) to smaller data value ( long - l ).

User Christopher Klein
by
4.8k points