java 引用和对象,java中的引用和对象有什么区别?

I have class GUI so I can create object like this:

GUI g1 = new GUI();

and a reference variable like this:

GUI g2;

Now as much as I know, g2 is a reference variable which gives reference to GUI class and g1 is an object of GUI class. What is the difference between g1 and g2? I can use property of GUI class with object but what is the possible usage of g2?

解决方案

References are names. Objects are stuff. You can have different names for stuff, even for stuff that doesn't actually exist.

You can declare names, without actually giving them any "real" meaning, like this:

GUI g1;

You can assign meaning (real stuff to refer to) to names with the = operator:

GUI g1 = some_gui;

Names can change their meaning over time. The same name can refer to different things at different points in history.

GUI g1 = some_gui;

doSomething();

g1 = some_other_gui;

There are also synonyms: multiple names can refer to the same thing:

GUI g2 = g1;

That's pretty much what references do. They are names meant to refer to stuff.

Stuff can be created:

new GUI();

Stuff can be created and named on the spot for later reference (literally!):

GUI g1 = new GUI();

And stuff can be referred to, using its name (or any of its names!):

g1.doSomething();

g2.doSomethingAgain();

Different stuff of the same kind (Class) can be created, and named differently:

GUI g1 = new GUI();

GUI g2 = new GUI();

GUI g3 = new GUI();

GUI g1_synonym = g1;

:)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值