GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.dremendo.com — Typecasting in Java Programming Language | Dremendo
?>
Announcements
No new announcements.
Java Programming Tutorial
Table of Contents
Typecasting in Java Programming LanguageIn this lesson, we will learn about what is Typecasting and different types of it. We will also go through some examples and take a quiz at the end. What is TypecastingIn Java program the process of changing one data type into another data type is known as Typecasting. There are two types of typecasting:
Video Explanation
Click the button below to watch the explanation.
Please wait while the video is loading....
Generally when we add, subtract, multiply, or divide two or more values of similar data type the output is also of the same data type. For example:
In typecasting, when we work with dissimilar data types, the output can be a different data type. Let's explore the explanations of each type of typecasting with examples and how to use them in programming. Implicit TypecastingThis is also known as automatic type conversion. In this type of casting (conversion) the data is converted from one data type to another data type by the compiler itself without programmer interference. This usually happens when we assign a smaller data type to a larger data type.
Example
In the above example, when we assign the integer variable num to the float variable result, the compiler will automatically converts the integer value into a float value because the int datatype is smaller than float so it can be easily stored in float. Let's see some more examples.
Example
Whenever an equation contains two or more different data types, as shown in the above examples, the result will always be in the data type which is largest amongst the data types involved in the equation. This is because all other data types automatically convert to the highest data type available in the equation, resulting in the final output being of the highest data type. This type of conversion is known as Implicit Casting. Explicit TypecastingThis is also known as manual type conversion. In this type of casting (conversion) the programmer manually converts a variable from one type to another using the cast operator. The syntax for explicit type casting is:
Syntax
Example
In this example, the float num is explicitly cast to an integer when assigned to result variable. As a result, the fractional part of the float is discarded, and only the integer part is retained.
Note
Whenever we want to change the data type of a variable using explicit casting method, just write the name of the data type within ( ) before the variable name we want to convert, as shown in the example above. Let's see one more example for more clarification.
Example
In the above example, the variables a and b are of int datatype. So if we divide then without typecasting the result will be 2.0 which is wrong. If we want the correct result, then we have to cast any one of the integer variables (a or b) to float using the cast operator as shown in the above example.
Note
Changing the data type of the variables a or b using explicit casting within the equation does not alter their original declared data types. The original data types remain unchanged. The changes are temporary and only apply for the purpose of solving the equation. What should we do if the expression looks like this?
In this case, the result is 1.0 because both numbers are treated as integers. To get a more accurate result, we can change either of the numbers to a decimal by adding a f (here f represent float) to the end of the number like this:
Or like this:
Note
To change an integer into a double type value, you can add .0 to the end of the number, like 7.0. This ensures that the number is treated as a double type value. Test Your KnowledgeAttempt the questions in the exercise to check whether you have understood the lesson clearly.
We care about your privacyDremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy. Cookie PreferencesChoose which types of cookies Dremendo may use.
Necessary Cookies
Allow
Required for essential website functionality and security.
Analytics Cookies
Allow
Help us understand how visitors use Dremendo.
Marketing Cookies
Allow
Help us measure advertising performance and understand interactions with our advertisements.
Mentora
Question Asked: 0 / 20
Hi, I’m Mentora! I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |