From the course: Oracle Java Certification: 1. Data Types
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Primitive type casting - Java Tutorial
From the course: Oracle Java Certification: 1. Data Types
Primitive type casting
- Every Java data item has a type. At some point we'll have to copy data items of one type to variables items of another type. There are three categories of type conversions. Between primitive types, between reference types, and between primitive types and wrapper class types. Let's take a look at a few examples of do's and do not when it comes to type conversions. I'll say this in general, it's always possible to do a conversion from narrower to wider data types. If you undergo the opposite way and force the issue you can use type casting. I'll show you examples of that as well. Here in line one we define a char variable with the letter a in it. In line two we assign the value of the variable a to an integer. What would the output look like? Generally speaking, assigning values of a narrower type to variables of a wider type is allowed. In this case the narrower type is char. Java automatically promotes the narrower types to the wider types. In this example when we assign a to b, the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.