Easy-mix problem summary --- (by value vs by reason)

xiaoxiao2021-03-06  141

The value and the address; the basic type of passage in Java is completed by the pass value, so although the value of the incoming INT i is changed in the example, it does not affect the original INT. i's value, simple point is the copy of the value of incoming int i's value and reference to the reference (Reference)? It is a bit different, and the change in ATest () affects the original object is content, because the passage of Reference in Java is by value, that is, it is the copy of the Reference, ==== ======= Memory allocation map ============ Object O1 = new object (); object o2 = new object (); ---------- At this point, the allocation of the storage space ----------

O1 = O2 ---------- At this time, allocation of storage space ----------

At this time, the value of the REFERENCE of O2 is passed to O1. Therefore, the object pointing now is an object pointing to the start O2. If it is now changed, then the O1 is now changed, then the method of calling the object is Visible (of course, O2 will change accordingly, the same thing), so in the example below: Ateest () changes in the last println () in the last println () ----- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ - Import Java.awt.Point; Class PassValue {PUBLIC Static Void ATest (POINT P, INT I) {P.SETLOCATION (2, 2); I = 12; System.Out.println ("During Ateest P =" P "/ n and i =" i);} public static void main (string arg []) {Point PO = new point (0); int J = 0; system.out.println ("Before ATest P = " PO " / n and i = " j); test (po, j); system.out.println (" After Ateest P = " PO " / n and i = " j);}} ---------- Output ---------- Before Atest P = java.awt.point [x = 0, y = 0] and i = 0DURING ATEST P = java.awt. Point [x = 2, y = 2] and i = 12AFTER ATEST P = java.awt.point [x = 2, y = 2] and i = 0

Output completion (time consumption 0 seconds) - Normal termination ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----------------------

转载请注明原文地址:https://www.9cbs.com/read-127901.html

New Post(0)