answersLogoWhite

0


Best Answer

Type casting means that you explicitly convert from one type to another. Consider this example:

int x;
long y = 10;
x = y;

The compiler will protest, because of a possible overflow. Now, if you are confident - from a knowledge of the computer program you wrote - that there will be no overflow, you can explicitly:

x = (int) y;

You are telling the compiler that you know what you are doing. The danger, of course, is that you may be wrong, and there may still be an overflow.

The situation is similar if you convert from double to float, or from double or float, to integer. In all of these cases, there is a loss of precision.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is type casting and what is the danger of casting from one type to another?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can the value of an expression be converted to a different data type in c?

There are different ways, there is one straight forward though. For instance you have a variable of type char: var1. And you want to convert it to another variable of type int: var2. You can use casting for it:var2 = (int) var1;You have to be very careful using casting because it cause buffer overflow. Newer versions of C have many different function to assure correctness of parsing, for instance, TryToParse...


What do you mean by casting a value?

casting a value is when the data type is changed from one type to another.int a = 100;float b:b = (float)a;------The (float)a casts an int to a float. In C this is automatic but in C++ it is not. For the sake of readability and debugging it is a good idea to put the cast in the code, this is relay helpful 6 months later when you review your work.


Name of the type of welding?

One type of welding is tube welding. Another type if mig welding.


What are examples of 3 types of technology?

computers are 1 type pumps are another type of technology and one last one is solar power


What is casting in java and c plus plus?

Implicit casting is done automatically by the compiler and virtual machine.Explicit casting is needed to convert types of data when Java is not sure if the result will be valid. There are two times when you will need to perform explicit casts: casting between primitives and casting between objects.Between primitives: Primitive types come in different sizes. For example, an int is 32 bit, whereas a long is 64. If you are sticking a smaller type into a larger type (eg. an int into a long), there is no need to cast since there is no risk that the value will be larger than its container. However, when you go in the opposite direction (eg. a long into an int), a compilation error will occur since Java is afraid the value will be too large for its type. To "silence" the compiler, you can cast the value to the intended type. In doing so, you risk the value overflowing its container. (Example below)Between objects: There are two types of object to object casts: upcasts and downcasts. Upcasting occurs when you take a subclass and stick it in a more general type (Object o = new String();). Upcasting is always implicit. Downcasting is going in the opposite direction. You always need to explicitly cast when downcasting, and Java will only allow you to do so if the conversion is possible. (Example below).Examples between primitives:byte b; short s;s = b; //validb = s; //invalidb = (byte)s; //validb = (byte)Short.MAX_VALUE; //valid, but overflow will occur.Examples between objects:Object o = s; //upcast - no explicit casting requiredString s = (String)new Integer(); //always a compile errorString s = (String)getObject(); //assume that getObject() returns type Object. This will only work if the value stored in getObject() is a String or a subclass of String. Otherwise a runtime error will occur.

Related questions

What type of programs use PC Cast?

PC Cast is used for type casting in C and C++ programming. Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation.


What is one object in space casting a shadow on another?

A shadow cast on one object in space on another is ECLIPSE


Why do use typecasting in java?

Type casting is used to convert from one datatype to the other


Can you use a 32 instead of 7.65 in gun?

DO NOT ATTEMPT THIS WITHOUT HAVING A GOOD GUNSMITH CHECK THE GUN AND AMMO!! DANGER, DANGER, DANGER, DANGER There is more than one type of 32 and 7.65 ammuntion and some of them ARE NOT INTERCHANGEABLE. DANGER, DANGER, DANGER, DANGER


Another word for great danger?

Hi, Another words for great danger are below. Just take it and put both together one from great and one from Danager pile :) Great: Huge, large, vast, grand, Immense, enormous Danger: Hazard, Risk, peril, threat, menace, jeopardy


What is the value of converting information from one type to another?

Converting information from one type to another. Converting information from one type to another.


What is it called when one Risk factor adds to another to increase danger?

Death


Where can one watch videos about casting on knitting?

There are many places where one can watch videos about casting on knitting. One can watch videos on casting on knitting at popular on the web sources such as YouTube and Vimeo.


Who is the danger man of hockey?

There is a few of them. The main one is Colton Orr. Another one that is pretty dangerous is Derek Boogard


Explain about type casting in java?

Type casting means explicitly converting one data type to another. For example, the following won't be allowed:int a;long b;b = 5;a = b;In the last line, the compiler will complain, due to a possible data loss - long has a larger range. But if you believe that the conversion won't cause a problem in your program, you can override the error message with an explicit conversion (or typecast):...a = (int) b;Type casting means explicitly converting one data type to another. For example, the following won't be allowed:int a;long b;b = 5;a = b;In the last line, the compiler will complain, due to a possible data loss - long has a larger range. But if you believe that the conversion won't cause a problem in your program, you can override the error message with an explicit conversion (or typecast):...a = (int) b;Type casting means explicitly converting one data type to another. For example, the following won't be allowed:int a;long b;b = 5;a = b;In the last line, the compiler will complain, due to a possible data loss - long has a larger range. But if you believe that the conversion won't cause a problem in your program, you can override the error message with an explicit conversion (or typecast):...a = (int) b;Type casting means explicitly converting one data type to another. For example, the following won't be allowed:int a;long b;b = 5;a = b;In the last line, the compiler will complain, due to a possible data loss - long has a larger range. But if you believe that the conversion won't cause a problem in your program, you can override the error message with an explicit conversion (or typecast):...a = (int) b;


Is energy destroyed in transformations from one type to another?

Energy cannot be created or destroyed - only transformed from one type to another.


Can one consider steel casting and cast iron as same for all practical purposes?

No, cast iron is much more brittle and cannot withstand as much pressures as cast steel. It cannot be used for many of the same purposes that cast steel can be used for. Yes you can condsider steel casting and casting irom the same. Mainly because both are as strong as one another.