How to use the java cast method
This article mainly explains "how to use java forced type conversion method". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use the java forced type conversion method.
1. If necessary, the value of type int will be automatically converted to type double.
On the other hand, the double type can be forced to int, but information may be lost.
2. If you try to cast a value from one type to another and go beyond the representation range of the target type, the result will be truncated to a completely different value.
For example, the actual value of (byte) 300 is 44.
Example
/ / if the parent object occupies 1m of memory, it takes up 0.5m more memory because the subclass object has some other content, and the subclass object occupies 1.5m memory People [] staff = new People [2]; / / here we define an object of the parent class, which can point to 1m memory; staff [0] = new RichPeople ("John", 18,1001,200) / / 1.5m memory is opened, but it only points to 1m memory staff [1] = new RichPeople ("Tp"); if (staff [0] instanceof RichPeople) {/ / determine whether staff [0] is an instance of RichPeople / / create a boss instance for temporary storage and cast staff [0] / / because the reference to boss is staff [0], so the operation on boss is RichPeople boss = (RichPeople) staff [0]. Boss.addDeposit (10000); / / you can also write: / / ((RichPeople) staff [0]) .addDeposit (10000) / / (RichPeople) staff [0]) can be seen as creating an anonymous object of the RichPeople class (equivalent to the boss above) / / the reference to this anonymous object and the staff [0] of the RichPeople type are the same. I believe you have a deeper understanding of "how to use the java forced type conversion method". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!