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 - Java Tutorial
From the course: Java 11+ Essential Training
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
-
-
-
-
-
(Locked)
Work with primitive variables5m 43s
-
(Locked)
Declare and modify primitive values3m 27s
-
(Locked)
Declare and initialize object variables3m 40s
-
(Locked)
Manage currency values with BigDecimal3m 8s
-
(Locked)
Convert values between numeric types2m 40s
-
(Locked)
Math operators and the Math class2m 22s
-
(Locked)
Manage true and false Boolean values2m 43s
-
Manage character values as primitives3m 13s
-
(Locked)
More about Java operators5m 45s
-
(Locked)
-
-
-
-
-
-
-