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

What are the reference types in Java and ABAP

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the reference types in Java and ABAP". In the operation of actual cases, many people will encounter such a dilemma, 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!

Several different reference types in the Java programming language are often asked during interviews: strong references, soft references, weak references, and virtual references.

In fact, in addition to Java, some other programming languages have similar concepts, such as ABAP. Let's make a comparison today.

According to the ABAP help documentation, we can wrap a reference to an object in an instance of Weak Reference. The Weak Reference instance of ABAP is implemented through the class CL_ABAP_WEAK_REFERENCE.

Look at the following example: first I created a new LCL_PERSON instance on the heap and then packaged it into an ABAP weak reference.

Lo_person = NEW lcl_person ('Jerry').

Lo_weak = NEW cl_abap_weak_reference (lo_person).

Later, when we want to get the wrapped lo_person reference, we use the get method provided by weak reference. See the following figure for an example:

Lo_person = CAST lcl_person (lo_weak- > get ()).

When will the reference lo_person become initial? If there are no references to lo_person when the ABAP garbage collector (Garbage Collector) starts working, the lo_person becomes initial.

Take a look at the following example to deepen your understanding.

REPORT ztest.PARAMETERS: clear TYPE char1 as CHECKBOX DEFAULT abap_true,gc TYPE char1 as CHECKBOX DEFAULT abap_true.CLASS lcl_person DEFINITION.PUBLIC SECTION.DATA: mv_name TYPE string.METHODS: constructor IMPORTING! iv_name TYPE string.ENDCLASS.CLASS lcl_person IMPLEMENTATION.METHOD: constructor.me- > mv_name = iv_name.ENDMETHOD.ENDCLASS.START-OF-SELECTION.DATA: lo_person TYPE REF TO lcl_person Lo_weak TYPE REF TO cl_abap_weak_reference.lo_person = NEW lcl_person ('Jerry'). Lo_weak = NEW cl_abap_weak_reference (lo_person). IF clear = abap_true.CLEAR: lo_person.ENDIF.IF gc = abap_true.cl_abap_memory_utilities= > do_garbage_collection (). ENDIF.lo_person = CAST lcl_person (lo_weak- > get ()). IF lo_person IS INITIAL.WRITE: /' Reference not available'.ELSE.WRITE: / 'reference still available'.ENDIF.

This report has two switches, as shown in the following figure. The first switch controls whether the reference lo_person is explicitly set to INITIAL by the keyword CLEAR, and the second switch determines whether to explicitly call the ABAP garbage collector in the code.

There are four combinations of the opening and closing states of these two switches.

In the first case, the reference to lo_person is cleared by the keyword CLEAR, and you can see from ABAP's memory checker (transaction code s_memory_inspector) that lo_person is no longer pointing to any objects in memory.

For the other three cases, instances of LCL_PERSON are not cleared by the ABAP garbage collector:

Java

The behavior of weak reference in Java is consistent with that of ABAP.

I rewrote the above ABAP test code with the Java program:

Import java.lang.ref.WeakReference;class Person {private String mName; public Person (String name) {this.mName = name;} public String getName () {return this.mName;}} public class WeakReferenceTest {public static void check (Person person) {if (person = = null) {System.out.println ("Reference invalid") } else {System.out.println ("Reference still available");} public static void main (String [] args) {Person jerry = null; WeakReference person = new WeakReference (new Person ("Jerry")); jerry = new Person ("Ben"); / / if you comment out this line, Reference will be available System.gc () Person restore = person.get (); check (restore);}} ABAP Soft reference-ABAP soft application

In the ABAP Netweaver 750SP4 system I currently use, the ABAP soft application has not been implemented yet

Analysis and comparison of several reference types in Java and ABAP

There is only an empty CL_ABAP_SOFT_REFERENCE in the system, and its description information is Do Not Use this Class!

Analysis and comparison of several reference types in Java and ABAP

So let's try Java's soft application Soft Reference:

Package reference;import java.lang.ref.SoftReference;import java.util.ArrayList;class Person2 {private String mName; public Person2 (String name) {this.mName = name;} public String getName () {return this.mName;} public void finalize () {System.out.println ("finalize called:" + this.mName);} public String toString () {return "Hello, I am" + this.mName }} public class SoftReferenceTest {public static void main (String [] args) {SoftReference person = new SoftReference (new Person2 ("Jerry")); System.out.println (person.get ()); ArrayList big = new ArrayList (); for (int I = 0; I < 10000; iTunes +) {big.add (new Person2 (String.valueOf (I) } System.gc (); System.out.println ("End:" + person.get ());}}

The output printed by the console:

Hello, I am Jerry

End: Hello, I am Jerry

Even if I created 10, 000 instances of Person objects, it did consume some memory, and then the memory consumption was far from large enough to cause references to Person2 classes contained in soft applications to be deleted by JDK. So after I call Java's garbage collector System.gc () in my code, the reference still exists.

In Java, soft applications are usually used to implement caching mechanisms in environments with limited memory resources, such as Android mobile phone development.

Java virtual reference PhantomReference

Test the virtual reference using the following code:

Package aop;import java.lang.ref.PhantomReference;import java.lang.ref.ReferenceQueue;public class PhantomReferenceTest {public static void main (String [] args) {Object phantomObj; PhantomReference phantomRef, phantomRef2; ReferenceQueue phantomQueue; phantomObj = new String ("PhantomReference"); phantomQueue = new ReferenceQueue (); phantomRef = new PhantomReference (phantomObj, phantomQueue); System.out.println ("1 PhantomReference:" + phantomRef.get ()) System.out.println ("2 Phantom Queued:" + phantomRef.isEnqueued ()); phantomObj = null; System.gc (); System.out.println ("3 Anything in Queue?:" + phantomQueue.poll ()); if (! phantomRef.isEnqueued ()) {System.out.println ("4 Requestion finalization."); System.runFinalization () } System.out.println ("5 Anything in Queue?:" + phantomRef.isEnqueued ()); phantomRef2 = (PhantomReference) phantomQueue.poll (); System.out.println ("6 Original PhantomReference:" + phantomRef); System.out.println ("7 PhantomReference from Queue:" + phantomRef2);}}

Test output:

1. Phantom Reference: null

2. Phantom Queued: false

3. Anything in Queue?: null

5. Anything in Queue?: true

6. Original PhantomReference: java.lang.ref.PhantomReference@2a139a55

7. PhantomReference from Queue: java.lang.ref.PhantomReference@2a139a55

Unlike the weak reference (WeakReference) and soft reference (SoftReference) introduced earlier, the object instance wrapped in the virtual reference (PhantomReference) cannot be returned through the get method to be referenced, so we will see "1. PhantomReference: null" in the first line of output.

In the constructor that falsely references PhantomReference in the above sample code, I pass in a queue as an input parameter. When an object reference wrapped in a virtual reference instance is deleted by the Java garbage collector, the virtual reference instance itself is automatically inserted by JVM into the queue I assigned to the input parameters of the virtual reference constructor.

Before System.runFinalization () executes, phantomRef.isEnqueued () returns false,phantomQueue.poll (), which returns null.

When the phantomObj instance is deleted by JVM, the virtual reference PhantomReference itself is added to the queue and can be accessed through the API provided by the queue: phantomQueue.poll (). Lines 6 and 7 of the printout also illustrate this.

That's all for "what are the reference types in Java and ABAP?" 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!

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

Development

Wechat

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

12
Report