Skip to main content

Type Conversion

 

Operations in our applications will commonly involve multiple data types. Because of that, we must deal with type conversions. Now there are implicit type conversions, and those are type conversions that are performed automatically by the compiler. To see a simple example of that, we look at something like here we have an integer iVal that we've assigned the value 50. If I then say long lVal equals iVal, the compiler must convert that 32bit integer iVal into a 64bit integer as it's stored into lVal. And there are also explicit type conversions, and these are conversions that I explicitly perform in my code and I do that using a cast operator. We see here if I had that long lVal = 50 now, so I'm now assigning a 64bit integer the value of 50, if I want to assign that into this iVal, a 32bit integer, I have to explicitly cast it. I must tell part of that I want to go from that 64bit integer into a 32bit integer. And I do that by using the type I want to convert to, in this case, int, putting it inside of parentheses, and putting it next to the value I want to cast. So, this says, the int in parentheses says, take the lVal and cast it down to a 32bit integer. If we look now at these implicit type conversions, these are generally what we call widening conversions, and these can be done automatically because we're moving to a wider data type. So, if I have a 32bit integer and I want to move to a 64bit integer, that's wider, right? So, the values can be safely moved, so those can be done implicitly. And the compiler must make decisions about how to make these conversions. And the rules are straightforward. If I have expression with multiple integer sizes in it, maybe have a short and a long, whatever the largest integer size is what things will be converted to. So, if I do an operation with a short and the long, the short will be implicitly cast into a long. If I perform an operation with mixed floatingpoint sizes, so I have a float and a double, it'll always go to the double because double is the largest floatingpoint size. And then if I have an operation that uses integer types and floatingpoint types, the compiler will cast to whatever the largest floating point in the equation is. So, if I do an operation with a long and a float, the long will be cast to a float. If I do an operation with a long and a double, the long will then be cast to a double. Explicit type conversions are the ones that we explicitly perform in our code using this cast operation. When we do that, we're taking responsibility for whatever happens as a result of that type conversion. Because of that, we can perform both widening conversions and narrowing. So, widening going from a 32 bit to say a 64 bit, narrowing going from a 64 bit down to a 32 bit. We should just want to be aware that we know what could potentially happen. If we do an explicit cast from a floating point to an integer, while floating points can have fractional portions, integers can't. So, any fractional portion would be dropped when I cast that float down to an integer. You want to be careful when performing a narrowing conversion. If I have a 64bit integer, it can hold values that are too large to fit into a 32bit integer. So, if I cast that 64 bits to a 32 bit, the program will do it, but if the value was too large to fit into a 32 bit, you'll get some odd results from that. So, you want to make sure that when you're casting it, doing a narrowing cast, that you know that what you're doing is safe. And the last one is just that you want to be careful when converting from an integer to a floating point. Because if you have an integer with many significant digits, because of the way floating points are stored, you could lose some of those significant digits. The deep details of how these type conversions occur are beyond the scope of this course, but if you'd like to understand them in more detail, I've got that URL on the screen for you that actually points to the Java documentation that talks about those details. If you check that out, you'll particularly want to look at the sections on widening primitive conversion and narrowing primitive conversion.


Demo: Type Conversion


Comments

Popular posts from this blog

Difference Between Programming and Coding

What exactly is the difference between programming and coding?  The other day, one of my friends who is not from a computer science background, asked me this question. Even after learning many different programming languages and doing several projects, I could not answer him correctly. I said both are the same. But why do we use two different terms, if both are the same? That led me to some research, and I thought I’d share what I found.   It’s not that complicated.  And their definitions allow for a lot of overlap. We often recognize the terms coding and programming as synonymous because both are often used interchangeably.   what is the difference between programming and coding? Coding is the act of expressing programmatic ideas in computer language. Programming is crafting ideas that can be executed repeatedly by a machine, not necessarily a computing device. While both the terms are synonymous with each other and are often used interchangeably, t...

Types of Recording

Types of Recording: 4 types of recording modes are available Ø   Basic Ø   Desktop Ø   Web Ø   Citrix 2 ways of Recording Ø   Automatic Ø   Manual Basic – generates a full selector for each activity and no container, the resulted automation is slower than one that uses containers and is suitable for single activities. Desktop – suitable for all types of desktop apps and multiple actions; it is faster than the Basic recorder, and generates a container (with the selector of the top-level window) in which activities are enclosed, and partial selectors for each activity. Web – designed for recording in web apps and browsers (supported: Internet Explorer, Google Chrome), generates containers and uses the Simulate Type/Click input method by default. Citrix – used to record virtualized environments (VNC, virtual machines, Citrix, etc.) or SAP, permits only image, text and keyboard automation, and requires explici...

Variables, Data Types and Math Operations

  In order to do anything interesting in a program, we must have the ability to store and manipulate values. What allows us to do that are what we call variables. Now a variable, simply put, is just named data storage. Now Java is a strongly ‑ typed language. Now what that means is that when we declare a variable like, in this case, we have a variable named data Value, we have to specify the type of that variable, in this case, it's what we call an int, something that can store integers. So, what that means is that the data Value variable can only store things that are compatible with the type int. As we go through the course, we'll talk about this idea of one type being compatible with another. Now when we declare a variable, we can, of course, then assign a value to it, so our variable data Value now holds the value 100. Now when we use variables, we can do it the way we've done here where we declare it, then assign it, or as a matter of convenience, we can declare them a...