Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the java cast method

2025-02-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report