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

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

RPA Life Cycle

RPA Life Cycle: It is similar to software development life cycle with some minor changes. Technically, we called it as ‘ Life Cycle of a Bot’   Analysis:           ·     RPA Cycle begins from ‘ Analysis ’ phase.           ·     In this, Business Team & RPA Strategist/Architect identifies business process to automate           ·     After finalizing feasibility on process, implement plan of work (which includes documentation, Resources count & Time  ...) Development:          ·       Dev. Team starts working on requirements wrt to Environment.          ·       We called this phase as ‘ Bot Development ’          ·       Based on Tool selection , Limited/No coding is required ...

Saving your files as macro-enabled workbooks

It's really important that whenever you make something in the VBE(Called Visual Basic Editor), whenever you insert a module and you put code in there, you have to make sure that you're saving your workbook as not just a regular XLS exe file , but XLS EM .So, it's called the Macro Enabled Workbook. How can you do that , 1)Open Excel, Go into File 2)Select Save As option 3)And for the file type, you can choose here instead of Excel workbook, you can choose Excel Macro-Enabled Workbook.Again. Take a Note Of : If you've any sort of macros or you write code in a sub or a function, it has to be a Macro-Enabled Workbook, if you just save it as an Excel workbook, it's going to lose all of those modules and the code that you put in there.