From the course: Oracle Java Certification: 1. Data Types

Unlock the full course today

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

Reference type casting

Reference type casting - Java Tutorial

From the course: Oracle Java Certification: 1. Data Types

Start my 1-month free trial

Reference type casting

- [Instructor] What about type conversion between reference types? In this example, we create an integer object in line one and assign its reference to an object reference variable. When this code compile, it worked. Now let's see why. In the first line, we convert a primitive integer to a integer object through auto boxing. And then assign it to an object reference. This is an example of type conversion between primitive types and their rival class types. In the second line, we assign a value of a reference type to a variable of another reference type. In this case, it worked because the object class type is a superclass of the integer class type. Note that only one object gets created in this code. Line two copies the object reference from one variable to another and after that, both a and b points to the same object. Now, what if we want to assign b back to a? Would it work? A and b are reference types and we know that they are still pointing to the same object. What could go…

Contents