Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between type casting and type conversion in C++ or Java?

What is the difference between typecasting and typeconversion in C++ or Java ?

like image 718
Jagan Avatar asked Oct 29 '10 06:10

Jagan


People also ask

What is type casting in Java?

Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size. byte -> short -> char -> int -> long -> float -> double.

What is type casting and type conversion in C Explain with program?

Typecasting is a method in C language of converting one data type to another. There are two types of typecasting. 1. Implicit Type casting − This conversion is done by the compiler. When more than one data type of variables are used in an expression, the compiler converts data types to avoid loss of data.

What is type casting in C or C ++?

Typecasting in C and C++ Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable.

What is type conversion in C language?

In C programming, we can convert the value of one data type ( int, float , double , etc.) to another. This process is known as type conversion.


2 Answers

Type casting is treating a value (block of memory) referenced by a variable as being of a different type than the type the variable is declared as.

Type conversion is actually performing a conversion of that value.

In many languages, some casts (usually numeric ones) do result in conversions (this will vary quite a bit by language), but mostly it's just "treat this X as a Y".

Like most aspects of human language, unfortunately the terms are used slightly differently in different communities, mostly along language lines. For instance, see James' comment below about C++ — the word "cast" there has a much broader meaning than the above definition, which is more in the C or Java mold. And just to make things fun, the Java Language Spec actually gets into various kinds of casts, including casting conversions. But the above is a good rule of thumb.

But to take a simple case:

In Java, prior to generics it wasn't unusual to have to do a lot of typecasting when dealing with maps:

Map m = new HashMap();
m.put("one", "uno");

// This would give a compiler error, because although we know
// we'll get a String back, all the compiler knows is that it's
// an Object
String italian = m.get("one");

// This works, we're telling the compiler "trust me, it's a String"
String italian = (String)m.get("one");

Fortunately, the addition of generics addressed this, as casting in this way tends to be a fragile process with maintenance issues.

In contrast, you'd convert if you had a String of digits:

String s = "1234";

...and needed to know what number those digits represented in decimal:

// Wrong (cast)
int n = (int)s;

// Right (conversion)
int n = Integer.parseInt(s, 10);
like image 112
T.J. Crowder Avatar answered Oct 11 '22 06:10

T.J. Crowder


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.]

like image 33
Jörg W Mittag Avatar answered Oct 11 '22 07:10

Jörg W Mittag