The difference between implicit conversion and explicit conversion

Possible duplicate:
Implicit VS Explicit Conversion

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

+7
source share
3 answers

An explicit conversion is where you use some kind of syntax to tell the program to do the conversion. For example (in Java):

int i = 999999999; byte b = (byte) i; // The type cast causes an explicit conversion b = i; // Compilation error!! No implicit conversion here. 

Implicit conversion is where the conversion occurs without any syntax. For example (in Java):

 int i = 999999999; float f = i; // An implicit conversion is performed here 

It should be noted that (in Java) transformations involving primitive types usually involve some change in presentation, and this can lead to loss of accuracy or loss of information. Conversely, transformations that include reference types (only) do not change the fundamental idea.


Difference is different in Java and C ++?

I can’t imagine it. Obviously, the available conversions will be different, but the difference between “implicit” and “explicit” will be the same. (Note: I am not an expert in C ++ ... but these words have a natural meaning in English, and I cannot imagine that the C ++ specifications use them in a contradictory sense.)

+17
source

Do you mean cast? Implicit means that you are passing an instance of a type, for example B, which inherits from a type, for example A as A.

For example:

 Class A; Class B extends A; function f(A a) {...}; main() { B b = new B; f(b); // <-- b will be implicitly upcast to A. } 

There are actually other types of implicit castings - between primitives using default constructors. You will need to clarify your question.

implicit with default constructor:

 class A { A (B b) { ... }; } class B {}; main() { B b = new B(); A a = b; // Implict conversion using the default constructor of A, C++ only. } 
+2
source

Casting is an explicit type conversion specified in the code and obeying very few rules at compile time. Throws may be unsafe; they may fail at runtime or lose information.
An implicit conversion is a type conversion or primitive data conversion performed by the compiler to comply with data promotion rules or to match the method signature. In Java, only safe implicit conversions are performed: promotion and promotion. \

I would also suggest reading about C ++ implicit coversion: http://blogs.msdn.com/b/oldnewthing/archive/2006/05/24/605974.aspx

0
source

All Articles