Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use java to cast

Shulou Source: shulou.com Published: 2022-06-03 04:04:12 10月05日 Update

This article introduces the knowledge of "how to use java to force type conversion". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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 RichPeople class (equivalent to the boss above) / / the reference to this anonymous object and RichPeople type staff [0] is the same "how to use java cast" content here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Type memory object force content instance actual subclass that is point more knowledge different practical same necessary learned next information on the other hand Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple OPPO Reno Docker MySQL vpn