Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

What is the difference between "implicit conversion" and "explicit conversion"? Is the difference different in Java and C++?

1 Answer

0 votes
by (19.7k points)

No, the difference between explicit and implicit conversion is the same in Java and C++. 

Implicit conversion doesn’t require you to write the syntax manually. It will be done by the compiler automatically. It happens when we assign smaller data types to larger data types. 

Here’s the code snippet for implicit conversion:

int i =123456;

float f = i;  

 

In explicit conversion, you write the syntax for the program to do the conversion otherwise it’ll not perform the conversion in this case. Typecasting is used to achieve explicit conversion. 

 

int i = 123456;

byte b = (byte) i;  //performs explicit conversion. 

b = i;  //throws compilation error because compiler can’t do implicit conversion here. 

 

In Java, conversions with primitive data types sometimes result in loss of precision. But conversion with reference types retains the fundamental representation. 

Browse Categories

...