From the course: Java 11+ Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Convert values between numeric types

Convert values between numeric types - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Convert values between numeric types

- In Traverse, you can make copies of and convert numbers to different types. Certain conversions are automatic. When you're converting from a more precise to a less precise type, it's known as widening, but when you narrow the type, you need some special syntax. Let's take a look at some conversions that widen. I'll start off with a short integer that I'll set to a value of 100. Then I'll make a copy of that value, but set the new version as an int. With int I equals sh. I and sh contain the same value, but the int variable takes up more memory than the short. Because I'm expanding the amount of memory I'm using, I'm widening the type and I don't need any special syntax. Here's another example. I'll create a long integer that I'll call longValue, and I'll assign it to I. And once again, this is an automatic conversion, because I'm widening the value. But now I'll try to take that long value and turn it into a short…

Contents