Java引用机制——reference
所谓引用传递就是指将堆内存空间的使用权交给多个栈内存空间。
例子<1>
- public class Aliasing {
- int temp = 30;
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- Aliasing d1 = new Aliasing();
- d1.temp = 50;
- System.out.println(d1.temp);
- fun(d1);
- System.out.println(d1.temp);
- }
- public static void fun (Aliasing d2){
- d2.temp = 1000;
- }
- }
例子<2> 其中传递的是string对象,由于string的内容是不可以修改,所以str1的值还是hello,如果传递的是对象的string属性,那是可以修改的
- public class Aliasing {
- int temp = 30;
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- String str1 = "hello";
- System.out.println(str1);
- fun(str1);
- System.out.println(str1);
- }
- public static void fun (String str2){
- str2 = "hello2";
- }
- }
例子<3>传递的是对象的string属性
- public class Aliasing {
- String temp = "hello";
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- Aliasing d1 = new Aliasing();
- d1.temp = "world";
- System.out.println(d1.temp);
- fun(d1);
- System.out.println(d1.temp);
- }
- public static void fun (Aliasing d2){
- d2.temp="HELLO";
- }
- }
一对一关系 例子
一个人对应一本书,一本书对应一个人
- class Person{
- private String name;
- private int age;
- private Book book;
- public Person(String name,int age){
- this.setName(name);
- this.setAge(age);
- }
- public String getName(){
- return name;
- }
- public void setName(String n){
- name = n;
- }
- public int getAge(){
- return age;
- }
- public void setAge(int a){
- age = a;
- }
- public Book getBook(){
- return book;
- }
- public void setBook(Book b){
- book = b;
- }
- }
- class Book{
- private String title;
- private float price;
- private Person person;
- public Book(String title,float price){
- this.setTitle(title);
- this.setPrice(price);
- }
- public String getTitle(){
- return title;
- }
- public void setTitle(String t){
- title = t;
- }
- public float getPrice(){
- return price;
- }
- public void setPrice(float p){
- price = p;
- }
- public Person getPerson(){
- return person;
- }
- public void setPerson(Person person){
- this.person = person;
- }
- }
- public class reference {
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- Person per = new Person("zhangsan",30);
- Book bk = new Book("JAVA SE kaifa",90.0f);
- per.setBook(bk);
- bk.setPerson(per);
- System.out.println(" name "+per.getName()+" age "+per.getAge()+" book "+per.getBook().getTitle()+" price "+per.getBook().getPrice());
- System.out.println(" title "+bk.getTitle()+" price "+bk.getPrice()+" person "+bk.getPerson().getName()+" age "+bk.getPerson().getAge());
- }
- }
一个人对应一本书,一本书对应一个人,一个孩子对应一本书,一本书对应一个孩子,一个人对应一个孩子
- class Person{
- private String name;
- private int age;
- private Book book;
- private Person child;
- public Person(String name,int age){
- this.setName(name);
- this.setAge(age);
- }
- public String getName(){
- return name;
- }
- public void setName(String n){
- name = n;
- }
- public int getAge(){
- return age;
- }
- public void setAge(int a){
- age = a;
- }
- public Book getBook(){
- return book;
- }
- public void setBook(Book b){
- book = b;
- }
- public Person getChild(){
- return child;
- }
- public void setChild(Person child){
- this.child = child;
- }
- }
- class Book{
- private String title;
- private float price;
- private Person person;
- public Book(String title,float price){
- this.setTitle(title);
- this.setPrice(price);
- }
- public String getTitle(){
- return title;
- }
- public void setTitle(String t){
- title = t;
- }
- public float getPrice(){
- return price;
- }
- public void setPrice(float p){
- price = p;
- }
- public Person getPerson(){
- return person;
- }
- public void setPerson(Person person){
- this.person = person;
- }
- }
- public class reference {
- public static void main(String[] args) {
- // TODO 自动生成的方法存根
- Person per = new Person("zhangsan",30);
- Person cld = new Person("zhangcao",10);
- Book bk = new Book("JAVA SE kaifa",90.0f);
- Book b = new Book("11111",30.0f);
- per.setBook(bk);
- bk.setPerson(per);
- cld.setBook(b);
- b.setPerson(cld);
- per.setChild(cld);
- System.out.println(" name "+per.getName()+" age "+per.getAge()+" book "+per.getBook().getTitle()+" price "+per.getBook().getPrice());
- System.out.println(" title "+bk.getTitle()+" price "+bk.getPrice()+" person "+bk.getPerson().getName()+" age "+bk.getPerson().getAge());
- System.out.println(" cldname "+per.getChild().getName()+" age "+per.getChild().getAge()+" book "+per.getChild().getBook().getTitle()+" price "+per.getChild().getBook().getPrice());
- }
- }
Java引用机制——reference的更多相关文章
- Java编程开发之浅析Java引用机制
对于一个Java的对象而言,存储主要分为两种,一种是内存堆(Heap),内存堆是无序的,主要用来存放创建的Java对象:一种是内存栈(Stack),主要用来存放Java引用,然后在管理过程使用Java ...
- Does Java pass by reference or pass by value?(Java是值传递还是引用传递) - 总结
这个话题一直是Java程序员的一个热议话题,争论不断,但是不论是你百度搜也好还是去看官方的文档中所标明的也好,得到的都只有一个结论:Java只有值传递. 在这里就不贴代码细致解释了,让我们来看看一些论 ...
- Java中的函数式编程(四)方法引用method reference
写在前面 我们已经知道,lambda表达式是一个匿名函数,可以用lambda表达式来实现一个函数式接口. 很自然的,我们会想到类的方法也是函数,本质上和lambda表达式是一样的,那是否也可以用类 ...
- java内存机制和GC垃圾回收机制
Java 内存区域和GC机制 转载来源于:https://www.cnblogs.com/zhguang/p/3257367.html 感谢 目录 Java垃圾回收概况 Java内存区域 Java对象 ...
- <Java><类加载机制><反射>
类加载过程 类从被加载到虚拟机内存开始,直到卸载出内存,它的整个生命周期包括:加载(Loading), 验证(Verification), 准备(Preparation), 解析(Resolution ...
- 浅谈Java引用和Threadlocal的那些事
这篇文章主要介绍了Java引用和Threadlocal的那些事,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 1 背景 某一天在某一个群里面的某个群友突然提出了一个问 ...
- 一文读懂Java类加载机制
Java 类加载机制 Java 类加载机制详解. @pdai Java 类加载机制 类的生命周期 类的加载:查找并加载类的二进制数据 连接 验证:确保被加载的类的正确性 准备:为类的静态变量分配内存, ...
- ClassLoader类加载器 & Java类加载机制 & 破坏双亲委托机制
ClassLoader类加载器 Java 中的类加载器大致可以分成两类: 一类是系统提供的: 引导类加载器(Bootstrap classloader):它用来加载 Java 的核心库(如rt.jar ...
- java内存机制 垃圾回收
gc机制一 1.JVM的gc概述 gc即垃圾收集机制是指jvm用于释放那些不再使用的对象所占用的内存.java语言并不要求jvm有gc,也没有规定gc如何工作.不过常用的jvm都有gc,而且大多数gc ...
随机推荐
- oracle数据匹配merge into
来源于:http://blog.csdn.net/vtopqx/article/details/50633865 前言: 很久之前,估计在2010年左右在使用Oralce,当时有个需求就是需要对两个表 ...
- MySQL修改,表结构大幅修改
------------------create table t_video_file_temp( video_id bigint not null comment '视频Id', file_md5 ...
- android 入门笔迹(1)
环境搭建JDK,JRE,Android SDK,ADT,Eclipse,安卓模拟器AVD xml控制UI界面 Java代码控制UI界面 XML与Java混合控制UI界面 UI:userinter ...
- 【BZOJ 1911】【APIO 2010】特别行动队
http://www.lydsy.com/JudgeOnline/problem.php?id=1911 夏令营里斜率优化的例题,我调了一晚上,真是弱啊. 先推公式吧($sum_i$表示$x_1 \d ...
- hdu3572 最大流
Task Schedule Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- java -日期
package com.qinghuainvest.tsmarket.util; import java.text.ParseException; import java.text.SimpleDat ...
- 解决不能访问远程mysql的问题
一般是没有给用户访问权限 给用户test_user授权,让他可以从外部登陆和本地登陆注意:@左边是用户名,右边是域名.IP和%,表示可以访问mysql的域名和IP,%表示外部任何地址都能访问. m ...
- Hash_bzoj1862: [Zjoi2006]GameZ游戏排名系统
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- struts2 CVE-2013-2251 S2-016 action、redirect code injection remote command execution
catalog . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch Fix 1 ...
- 添加一个功能Action
1,只用一个handler类,所有都事件的处理器都在一个handler类 handler要创建以Action为名称的方法 event要单独分开,继承KDEvent package com.kingde ...