国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Pass Parameters to A Function

2019-11-14 21:17:47
字體:
供稿:網(wǎng)友
Pass Parameters to A Function

As we know, in java, a PRimitive-type variable is passed to a function by value while a Object-type variable is passed by its reference(i.e, the address to the memory where the real variable is stored).

But it should be kept in mind that the reference will never be passed back to the caller. We can just think this as a copy of reference is passed to the called method.

If the parameter alters to refer to other object in the called method, the corresponding variable(the block of memory) in the caller won't be affected. However, the memory which the parameter is pointing to gets changed in the called method, it will affect the world outside.

Here is an example to illustrate this.

 class TryReferncenPassBack{ // some code we are not caring about this moment int i; }

class Test{    public static void main(String [] args){        TryReferncenPassBack t = new TryReferncenPassBack();        t.i = 1;        System.out.println(t.toString());        System.out.println(t.i);        method1(t);        System.out.println(t.toString());        System.out.println(t.i);    }    public static void method1(TryReferncenPassBack t){        t.i = 2;        t = new TryReferncenPassBack();        System.out.println(t.toString());        t.i = 3;    }}   

Result:

1. TryReferncenPassBack@510bfe2c2. 13. TryReferncenPassBack@6a5c2445 // point to the new object4. TryReferncenPassBack@510bfe2c // after getting back, the reference still remains5. 2 // the original memory gets changed at line 16 instead of line 19

?2014 Alain


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄浦区| 隆子县| 舟山市| 丰台区| 全椒县| 合作市| 沅陵县| 岳西县| 安达市| 连云港市| 广昌县| 绥中县| 景宁| 武胜县| 廉江市| 营口市| 桑植县| 黄山市| 西安市| 葫芦岛市| 荆门市| 大理市| 澜沧| 华亭县| 商都县| 曲阳县| 贵德县| 台安县| 灵武市| 都匀市| 昌乐县| 临城县| 牟定县| 封丘县| 余姚市| 清丰县| 江津市| 南溪县| 江津市| 政和县| 青州市|