About Forced type conversion

Source: Internet
Author: User

Forced type conversion is widely used in Java and is related to inheritance, interface implementation, and polymorphism. If forced type conversion is not implemented, many functions may not be implemented. In actual use, there are always some misunderstandings about its understanding and use, and we will analyze it in detail. First, let's look at two scenarios.

Example 1:

I asked a student this question in class: "How can I convert a string that represents a number into a number in Java ?"

Multiple students in different classes replied, "use forced type conversion"

I continue to ask, "How to write code ?"

Some students answer "int I = (int) s", s is a string, and some students answer "int I = Integer. parseInt (s )"

The former writes code for forced type conversion, but it is incorrect. The latter is correct, but not forced type conversion.

Example 2:

When guiding students in practical training, you often need to use the tool class List or Map. Because List or Map can store any type of data, you need to convert the data to the desired type after extracting the data, at this time, students understand forced type conversion as omnipotent, and convert the result to any type. In fact, this idea is wrong, and there are still many people with this idea.

"Forced type conversion" means to forcibly convert a type to another type. Is that true? In Java, Object Instantiation is based on the type. That is to say, the type of the object is determined when it is created and cannot be changed.

What is "forced type conversion? In fact, the type conversion here refers to directing a type of object reference to an object instance of another type of reference. For example:

Animal aninal = new Dog ();

Dog dog = (Dog) animal;

Row 3 points aninal to the instance of Dog (instantiated through new), and row 3 points the Dog type object reference dog to the instance of Aninal type animal reference. In fact, there is no type conversion here, because animal itself points to a dog, so dog points to the instance that animal points to, that is, pointing dog to a dog. There is really no type conversion !!!

Why do we need such a syntax? In our life, we see a Dog saying that this is a dog (Dog = new dog ()), seeing a Cat will say it is a cat (Cat = new cat ()). But sometimes, when we see a Dog, we say this is an Animal (in fact this animal is a Dog, Animal animal = new Dog ()), then you may say that this animal is a dog. Just like telling a story to a child, "there is an animal under the big tree. It is a dog ". But we know that an animal is not necessarily a dog. If you think it is a dog, use forced type conversion, in fact, it is declared that this animal is really a Dog (dog Dog = (Dog) animal ).

Will this happen? You can see that it is not a dog but a wolf in the dog family. In this case, the program will not be wrong during compilation, but it will be wrong during running, that is, the famous ClassCastException. The following is a typical example:

Aninal animal = new Cat ();

Dog dog = (Dog) aninal;

There are no syntax errors, but the runtime is wrong because you are confused. Of course, this kind of opportunity will not happen. When will it happen? If you call a method and return an Animal, you take it for granted that it is a Dog and it is converted to a Dog. If it is not a Dog, an error occurs. If you are sure it is a Dog, you can do this. If you are not sure what to do?

First, judge whether it is Animal, because it may be null.

Next, use instanceof to determine its type and then process it:

If animal instanceof Dog

Dog dog = (Dog) animal;

I don't know if you understand the role of forced type conversion. Remember the following:

To turn an animal into a dog, it must first be a dog. Forced type conversion is to tell the compiler that it is indeed a dog.

Li xucheng CSDN Blog: http://blog.csdn.net/javaeeteacher
Invite you as friend: http://student.csdn.net/invite.php? U= 124362 & c = 7be8ba2b6f3b6cc5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.