From the course: Java 8 Essential Training

Unlock the full course today

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

Converting numeric values

Converting numeric values - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Converting numeric values

- You can make copies of and convert numbers to different types. In this example, in the ConvertingNumbers project, I have one line of executable code. It's declaring an integer value named intValue1 with a value of 56. If I declare a second integer value, with intValue2, and I say assign the value intValue1, I know have two copies of the value. When you're working with primitives, you're always making a copy of the value, you're not creating a reference to the original value. So now, if I want to output the value, with System.out.print.ln, I'll say The second value is, and then I'll append the new value. Then I'll run the code. And I get the second value is also 56. But let's say that I wanted to take that integer and turn it into a long integer. Here are the rules. When you're converting from a type that uses a smaller amount of memory, to a type that uses a larger amount of memory, that's called widening the type. When you're going from a larger to a smaller, that's called…

Contents