views:

130

answers:

6

what is the difference between typecasting and typeconversion in c++ or java ?

+3  A: 

Typecasting is just taking a pen and writing "this is now a int" on the variable, conversion is actually convert the content to the desired type so the value keeps having a sense.

joni
+5  A: 
T.J. Crowder
Could you please provide me examples for both typecasting and typeconversion ?
Jagan
@Jagan: Curiously enough, I was just doing that. :-)
T.J. Crowder
At least in C++, use of the term _cast_ is not so limited; there are several (four) types of casts. Reinterpretation is only one of them and is the least commonly used of them.
James McNellis
@James: "Least commonly used" in well designed high level code, at least.
Merlyn Morgan-Graham
@James: That's part of why I said *"In many languages, some casts (usually numeric ones) do result in conversions (this will vary quite a bit by language)..."* C++ has gone its own way a bit in terminology (IMV). Not being a C++ guru (at all), I stuck to the Java side of things. :-)
T.J. Crowder
@Merlyn: Yes, and I should have said "one of," since const casting should also be done sparingly.
James McNellis
''Type conversion is actually performing a conversion'' - LOL nice one ;)
AOI Karasu
+4  A: 

According to the Wikipedia article:

"In the C family of languages, the word cast typically refers to an explicit type conversion (as opposed to an implicit conversion), regardless of whether this is a re-interpretaion of a bit-pattern or a real conversion."

Here is a C++ example:

double d = 42.0;
int i = d; // Here you have an implicit conversion from double to int
int j = static_cast<int>(d); // Here you have a cast (explicit conversion).

Here is a Java example (note that in Java unlike C++ you can't implicitly convert from double to int):

int i = 42;
double d = i; // Here you have an implicit conversion from int to double
int j = (int)d; // Here you have a cast (explicit conversion).
vitaut
+4  A: 

Type conversion:

double value = 3; // implicit conversion to double value 3.0
int nValue = 3.14156; // implicit conversion to integer value 3

Casting is a request by the programmer to do an explicit type conversion.

int nValue = 10;
double dvalue = double(nValue); // explicit type casting
sukhbir
+2  A: 

The terms are often used interchangeably.

Java

Type casting and type conversion are the same thing in Java, though if someone says that they are casting, they are usually referring to an explicit conversion.

A cast in Java will be done at runtime, so can potentially fail (throw an exception). Some types of invalid casts can be caught at compile time. When the cast fails, the instance was seated in an object reference, so the compiler couldn't tell what cast was going to be performed, until it actually ran the code.

C++

Type casting and type conversion are different in C++. There are five types of casts in C++, which all have different behavior: static_cast, dynamic_cast, reinterpret_cast, const_cast, and c-style casts ((int)someVariable).

Some C++ casts perform type conversion (hence why this concept is confusing), calling code and potentially doing runtime checks. Other C++ casts simply fake the type change of the referring variable - no memory will be modified, moved, or copied, so the resulting datatype might not be properly converted. This can give great speed at runtime, and can be a powerful tool for low-level code, but tends to be avoided for high level code.

Note that the c-style cast syntax (which happens to look exactly like the Java syntax) is one of the casts that will not necessarily call conversion code.

Type conversion in C++ often refers to calling a copy constructor or assignment operator, which will copy data over to a new instance of a different class/structure. If a type has conversion operators defined, then the conversion syntax could look like a cast, or simply a straight assignment. The main difference here is that code is called to do the conversion.

Merlyn Morgan-Graham
+3  A: 

Maybe an example can help:

  • If you cast 33 to a string, you get "!".
  • If you convert 33 to a string, you get "33".

[Note: this example makes all sorts of not-necessarily-valid assumptions about the encodings and in-memory representations of numbers and strings, but I hope the mechanism is clear.]

Jörg W Mittag
Yes.Good example.
Jagan