Java: 对象赋值

前言: 首先明确Java中万物都是类。而对于类的赋值,一直都很疑惑,因此总结一下


一个例子

图示

先贴一张图
在这里插入图片描述

程序实现:

int型做测试,输出其地址

  • C:
#include<stdio.h>
using namespace std;

int main(){
    int a, b;
    a = 1;
    b = 2;
    printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, b, &a,&b);
    a = b;
    printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, b, &a,&b);
    b = 3;
    printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, b, &a,&b);
}

输出:
在这里插入图片描述

  • Java

public class Main {

    public static void main(String[] args) {
    	int a, b;
    	//这里若加下面这句会报错,因为java中万物皆对象,我们还未给其实例化。但是C中我们可以打印
    	// System.out.printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, b, System.identityHashCode(a),  System.identityHashCode(b));
    	a = 1;
    	b = 2;
    	System.out.printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, b, System.identityHashCode(a),  System.identityHashCode(b));
    	a = b;
    	System.out.printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, b, System.identityHashCode(a),  System.identityHashCode(b));
    	a = 3;
    	System.out.printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, b, System.identityHashCode(a),  System.identityHashCode(b));
       
    }
}

输出
在这里插入图片描述


  • 可以看到,虽然a和b的值最后结果一样,可是他们的地址的变换却是不一样的。我们看到C中由于int不是一个对象,因此地址没有改变,但是Java中却改变了。
  • 而且值得注意的是a=3这一句,实际上已经重新创建了一个对象, 所以说,虽然地址层不一样,但是实际实现效果一样,但是java由于地址转换多次,所以很耗内存

而Java中的类和C++中的类=赋值还不一样

#include<stdio.h>
#include<iostream>
using namespace std;

class INT{
public:
    int x;
    INT(int num){
        x = num;
    }
};
int main(){
    INT a = INT(1);
    INT b = INT(2);
    printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a.x, b.x, &a,&b);
    a = b;
    printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a.x, b.x, &a,&b);
    a = INT(3);
    printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a.x, b.x, &a,&b);
}

输出:
在这里插入图片描述

  • 可以看到地址依旧没变。这是因为:在C++中B=A;, 是把对象A的数据成员的值逐位复制给对象B

Java大数类

  • 类声明、初始化三个阶段。如图

在这里插入图片描述
Java中的类=赋值,实际上是指向地址的转换,所以Java大数类的赋值可以当做C中的普通整数赋值操作,虽然地址不一样了,但是值一样。

package acm;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int m;
        String s;
        BigInteger a,b;
        BigInteger inf = new BigInteger("111111111111");
        a = BigInteger.valueOf(4);
        b = BigInteger.valueOf(5);
    	System.out.printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, System.identityHashCode(a), b,   System.identityHashCode(b));
        a = b;
        System.out.printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, System.identityHashCode(a), b,   System.identityHashCode(b));
        a = inf;
        System.out.printf("a = %d; address of a: %d; b = %d, address of b: %d\n", a, System.identityHashCode(a), b,   System.identityHashCode(b));
    }
}

输出
在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值