answersLogoWhite

0

Why do we need type casting in programming?

Updated: 8/10/2023
User Avatar

Eavves

Lvl 1
16y ago

Best Answer

Becuase, sometimes data is in a form that is unrecognizable to the program. Example:

int a = 2;

int b = 3;

double c = a / b;

We cast c as a double. if it was not, then the data would not have a decimal value.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

The most common use of type casting is to convert one numeric type into another.

// Let's say we have this square root function:

double sqrt(double s) {

double xn = s / 2.0;

double lastX = 0.0;

while(xn != lastX) {

lastX = xn;

xn = (xn + s/xn) / 2.0;

}

return xn;

}

// Now let's also say that we have an array with some data already in it:

int[] array = {/*some data*/};

// If we want to access the information in that array, we need an integer-typed value.

// So what happens if we want to use the square root of a number as an index?

// This line will not work, since sqrt(4) will return the floating point value 2.0

print array[sqrt(4)];

// So we try it again, but cast that 2.0 as an integer to get 2

print array[(int) sqrt(4)];

Type casting is also popular in object-oriented programming languages when polymorphism and inheritance come into play.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

You sometimes need to explicitly cast objects in Java when dealing with polymorphism. The reason is best explained in the example below. We'll be looking at an example where you press a button and make it so that the button is set to a disabled status.

// Create the button object

JButton reloadButton = new JButton("Disable me");

// Add a new ActionListener, which will define the code to run when the button is pressed

reloadButton.addActionListener(new ActionListener() {

public final void actionPerformed(ActionEvent ev) {

// Here is where we need to perform an action on the button.

// Unfortunately, this class cannot directly reference the button.

// (Actually, we can modify some things so that we can, but let's pretend

// it's impossible for the sake of a simple example.)

// Fortunately, there's a nice little function in ActionEvent called getSource

// which will return the "object on which the Event initially occurred."

// Unfortunately (again), it returns the Object on which the event occurred.

// That is to say, we don't get a reference to a JButton. So we need to

// clear that up to Java before we can call the setEnabled function on it.

// So let's cast our Object to get a JButton we can use...

final JButton source = (JButton) ev.getSource();

// ...and modify.

source.setEnabled(false);

// For those of you who like concise code, we could have written it in one line:

((JButton) ev.getSource()).setEnabled(false);

// And thus we need to explicitly cast an object in Java.

}

});

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Because, without Casting we cannot utilize the full potential of the Java Programming Language

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why do we need type casting in programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


Programming Software?

form_title=Programming Software form_header=Increase your productivity level with the right programming software for you. What type of programming software are you looking for?=_ Do you have a senior discount card?= () Yes () No Do you need help installing the software?= () Yes () No


What year is a 5.7 vortec with block casting?

Need the casting # to answer that question.


Do you need to be a good programmer to be good at engineering?

It depends what type of engineering you want to get into. Electrical engineers will use programming.


What type of education do you need as a web designer?

none but its better if you have at least two years of colege on programming i thnk


What type of GM transmission is casting number 3925656?

A Trans with the casting 3925656 on the main case is a saginaw 4-speed. To find out the year and other info you need to find other numbers on the extension housing and side cover. You also need to find the vin.


What type of surface defects on the casting?

your a Dylan


Is there a program that can help you make a programming language but you don't need to know a programming language?

No. In order to make or use a program or a programming language, you need to know a programming language.


Is the process of casting actors that have a physical appearance and personality similar to a particular character in a play?

Type-casting.


What is converting data types?

Type cast


Top gates are provided in which type of casting like as shallow casting or simple or complex or none?

ans is none because top gate are used in very large casting due to erosion occur in this casting


What is type conversion?

Type conversion or type casting is the method of changing the entity of datatype to another.