Java 内存分配全面浅析

static void increaseNum1(int num) {
         num = 10 + num++;
    }
static void increaseNum2(Integer num) {
     num = 10 + num++;
}
public static void main(String[] args) {
     int a = 0;
     increaseNum1(a);
     System.out.println(a);
     Integer b = 0;
     increaseNum2(b);
     System.out.println(b); 
}

结果:a=0 b=0

static void appendString(String src) {
         src = src + "-appended";
}

public static void main(String[] args) {
     String s = "src";
     appendString(s);
     System.out.println(s);     
}

结果:s = src

static void increaseNum1(int num) {
         num = 10 + num++;
}
static void increaseNum2(Integer num) {
     num = 10 + num++;
}
public static void main(String[] args) {
     int a = 0;
     increaseNum1(a);
     System.out.println(a);
     Integer b = 129;
     increaseNum2(b);
     System.out.println(b);     
}

结果:a=0 b=129

static void increaseNum1(int num) {
         num = 10 + num++;
}
static void increaseNum2(Integer num) {
     num = 10 + num++;
     System.out.println(num);
}
public static void main(String[] args) {
     int a = 0;
     increaseNum1(a);
     System.out.println(a);
     Integer b = new Integer(129);
     increaseNum2(b);
     System.out.println(b);     
}

结果:a=0 b=129

public static void main(String[] args) {
        int a = 1;
        int b = 2;

    change(a, b);
    System.out.println("a " + a + " b " + b);
}

public static void change(int a, int b) {
    int temp = 0;
    temp = a;
    a = b;
    b = temp;
}

结果:a 1 b 2

public static void main(String[] args) {
    c c = new c();
    d d = new d();

    change(c, d);
    System.out.println("c " + c.cc + " d " + d.dd);
    }

    public static class c {
        public int cc = 1;
    }

    public static class d {
        public int dd = 2;
    }

    public static void change(c c, d d) {
        int temp = 0;
        temp = c.cc;
        c.cc = d.dd;
        d.dd = temp;
    }

结果:c 2 d 1

public static void main(String[] args) {
    int[] a = new int[10];
    int[] b = new int[10];
    a[0] = 1;
    b[0] = 2;

    change(a, b);
    System.out.println("a " + a[0] + " b " + b[0]);
}

public static void change(int[] a, int[] b) {
    int temp = 0;
    temp = a[0];
    a[0] = b[0];
    b[0] = temp;
}

结果:a 2 b 1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值