所谓引用传递就是指将堆内存空间的使用权交给多个栈内存空间

例子<1>

  1. public class Aliasing {
  2. int temp = 30;
  3. public static void main(String[] args) {
  4. // TODO 自动生成的方法存根
  5. Aliasing d1 = new Aliasing();
  6. d1.temp = 50;
  7. System.out.println(d1.temp);
  8. fun(d1);
  9. System.out.println(d1.temp);
  10. }
  11.  
  12. public static void fun (Aliasing d2){
  13. d2.temp = 1000;
  14. }
  15. }

例子<2> 其中传递的是string对象,由于string的内容是不可以修改,所以str1的值还是hello,如果传递的是对象的string属性,那是可以修改的

  1. public class Aliasing {
  2. int temp = 30;
  3. public static void main(String[] args) {
  4. // TODO 自动生成的方法存根
  5. String str1 = "hello";
  6. System.out.println(str1);
  7. fun(str1);
  8. System.out.println(str1);
  9. }
  10.  
  11. public static void fun (String str2){
  12. str2 = "hello2";
  13. }
  14. }

例子<3>传递的是对象的string属性

  1. public class Aliasing {
  2. String temp = "hello";
  3. public static void main(String[] args) {
  4. // TODO 自动生成的方法存根
  5. Aliasing d1 = new Aliasing();
  6. d1.temp = "world";
  7. System.out.println(d1.temp);
  8. fun(d1);
  9. System.out.println(d1.temp);
  10. }
  11.  
  12. public static void fun (Aliasing d2){
  13. d2.temp="HELLO";
  14. }
  15. }

一对一关系   例子

一个人对应一本书,一本书对应一个人

  1. class Person{
  2. private String name;
  3. private int age;
  4. private Book book;
  5.  
  6. public Person(String name,int age){
  7. this.setName(name);
  8. this.setAge(age);
  9. }
  10.  
  11. public String getName(){
  12. return name;
  13. }
  14.  
  15. public void setName(String n){
  16. name = n;
  17. }
  18.  
  19. public int getAge(){
  20. return age;
  21. }
  22.  
  23. public void setAge(int a){
  24. age = a;
  25. }
  26.  
  27. public Book getBook(){
  28. return book;
  29. }
  30.  
  31. public void setBook(Book b){
  32. book = b;
  33. }
  34. }
  35.  
  36. class Book{
  37. private String title;
  38. private float price;
  39. private Person person;
  40.  
  41. public Book(String title,float price){
  42. this.setTitle(title);
  43. this.setPrice(price);
  44. }
  45.  
  46. public String getTitle(){
  47. return title;
  48. }
  49.  
  50. public void setTitle(String t){
  51. title = t;
  52. }
  53.  
  54. public float getPrice(){
  55. return price;
  56. }
  57.  
  58. public void setPrice(float p){
  59. price = p;
  60. }
  61.  
  62. public Person getPerson(){
  63. return person;
  64. }
  65.  
  66. public void setPerson(Person person){
  67. this.person = person;
  68. }
  69.  
  70. }
  71.  
  72. public class reference {
  73.  
  74. public static void main(String[] args) {
  75. // TODO 自动生成的方法存根
  76. Person per = new Person("zhangsan",30);
  77. Book bk = new Book("JAVA SE kaifa",90.0f);
  78. per.setBook(bk);
  79. bk.setPerson(per);
  80. System.out.println(" name "+per.getName()+" age "+per.getAge()+" book "+per.getBook().getTitle()+" price "+per.getBook().getPrice());
  81. System.out.println(" title "+bk.getTitle()+" price "+bk.getPrice()+" person "+bk.getPerson().getName()+" age "+bk.getPerson().getAge());
  82. }
  83.  
  84. }

一个人对应一本书,一本书对应一个人,一个孩子对应一本书,一本书对应一个孩子,一个人对应一个孩子

  1. class Person{
  2. private String name;
  3. private int age;
  4. private Book book;
  5. private Person child;
  6.  
  7. public Person(String name,int age){
  8. this.setName(name);
  9. this.setAge(age);
  10. }
  11.  
  12. public String getName(){
  13. return name;
  14. }
  15.  
  16. public void setName(String n){
  17. name = n;
  18. }
  19.  
  20. public int getAge(){
  21. return age;
  22. }
  23.  
  24. public void setAge(int a){
  25. age = a;
  26. }
  27.  
  28. public Book getBook(){
  29. return book;
  30. }
  31.  
  32. public void setBook(Book b){
  33. book = b;
  34. }
  35.  
  36. public Person getChild(){
  37. return child;
  38. }
  39.  
  40. public void setChild(Person child){
  41. this.child = child;
  42. }
  43. }
  44.  
  45. class Book{
  46. private String title;
  47. private float price;
  48. private Person person;
  49.  
  50. public Book(String title,float price){
  51. this.setTitle(title);
  52. this.setPrice(price);
  53. }
  54.  
  55. public String getTitle(){
  56. return title;
  57. }
  58.  
  59. public void setTitle(String t){
  60. title = t;
  61. }
  62.  
  63. public float getPrice(){
  64. return price;
  65. }
  66.  
  67. public void setPrice(float p){
  68. price = p;
  69. }
  70.  
  71. public Person getPerson(){
  72. return person;
  73. }
  74.  
  75. public void setPerson(Person person){
  76. this.person = person;
  77. }
  78.  
  79. }
  80.  
  81. public class reference {
  82.  
  83. public static void main(String[] args) {
  84. // TODO 自动生成的方法存根
  85. Person per = new Person("zhangsan",30);
  86. Person cld = new Person("zhangcao",10);
  87. Book bk = new Book("JAVA SE kaifa",90.0f);
  88. Book b = new Book("11111",30.0f);
  89. per.setBook(bk);
  90. bk.setPerson(per);
  91. cld.setBook(b);
  92. b.setPerson(cld);
  93. per.setChild(cld);
  94. System.out.println(" name "+per.getName()+" age "+per.getAge()+" book "+per.getBook().getTitle()+" price "+per.getBook().getPrice());
  95. System.out.println(" title "+bk.getTitle()+" price "+bk.getPrice()+" person "+bk.getPerson().getName()+" age "+bk.getPerson().getAge());
  96. System.out.println(" cldname "+per.getChild().getName()+" age "+per.getChild().getAge()+" book "+per.getChild().getBook().getTitle()+" price "+per.getChild().getBook().getPrice());
  97. }
  98.  
  99. }

Java引用机制——reference的更多相关文章

  1. Java编程开发之浅析Java引用机制

    对于一个Java的对象而言,存储主要分为两种,一种是内存堆(Heap),内存堆是无序的,主要用来存放创建的Java对象:一种是内存栈(Stack),主要用来存放Java引用,然后在管理过程使用Java ...

  2. Does Java pass by reference or pass by value?(Java是值传递还是引用传递) - 总结

    这个话题一直是Java程序员的一个热议话题,争论不断,但是不论是你百度搜也好还是去看官方的文档中所标明的也好,得到的都只有一个结论:Java只有值传递. 在这里就不贴代码细致解释了,让我们来看看一些论 ...

  3. Java中的函数式编程(四)方法引用method reference

    写在前面 我们已经知道,lambda表达式是一个匿名函数,可以用lambda表达式来实现一个函数式接口.   很自然的,我们会想到类的方法也是函数,本质上和lambda表达式是一样的,那是否也可以用类 ...

  4. java内存机制和GC垃圾回收机制

    Java 内存区域和GC机制 转载来源于:https://www.cnblogs.com/zhguang/p/3257367.html 感谢 目录 Java垃圾回收概况 Java内存区域 Java对象 ...

  5. <Java><类加载机制><反射>

    类加载过程 类从被加载到虚拟机内存开始,直到卸载出内存,它的整个生命周期包括:加载(Loading), 验证(Verification), 准备(Preparation), 解析(Resolution ...

  6. 浅谈Java引用和Threadlocal的那些事

      这篇文章主要介绍了Java引用和Threadlocal的那些事,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 1 背景 某一天在某一个群里面的某个群友突然提出了一个问 ...

  7. 一文读懂Java类加载机制

    Java 类加载机制 Java 类加载机制详解. @pdai Java 类加载机制 类的生命周期 类的加载:查找并加载类的二进制数据 连接 验证:确保被加载的类的正确性 准备:为类的静态变量分配内存, ...

  8. ClassLoader类加载器 & Java类加载机制 & 破坏双亲委托机制

    ClassLoader类加载器 Java 中的类加载器大致可以分成两类: 一类是系统提供的: 引导类加载器(Bootstrap classloader):它用来加载 Java 的核心库(如rt.jar ...

  9. java内存机制 垃圾回收

    gc机制一 1.JVM的gc概述 gc即垃圾收集机制是指jvm用于释放那些不再使用的对象所占用的内存.java语言并不要求jvm有gc,也没有规定gc如何工作.不过常用的jvm都有gc,而且大多数gc ...

随机推荐

  1. oracle数据匹配merge into

    来源于:http://blog.csdn.net/vtopqx/article/details/50633865 前言: 很久之前,估计在2010年左右在使用Oralce,当时有个需求就是需要对两个表 ...

  2. MySQL修改,表结构大幅修改

    ------------------create table t_video_file_temp( video_id bigint not null comment '视频Id', file_md5 ...

  3. android 入门笔迹(1)

    环境搭建JDK,JRE,Android SDK,ADT,Eclipse,安卓模拟器AVD xml控制UI界面  Java代码控制UI界面  XML与Java混合控制UI界面  UI:userinter ...

  4. 【BZOJ 1911】【APIO 2010】特别行动队

    http://www.lydsy.com/JudgeOnline/problem.php?id=1911 夏令营里斜率优化的例题,我调了一晚上,真是弱啊. 先推公式吧($sum_i$表示$x_1 \d ...

  5. hdu3572 最大流

    Task Schedule Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  6. java -日期

    package com.qinghuainvest.tsmarket.util; import java.text.ParseException; import java.text.SimpleDat ...

  7. 解决不能访问远程mysql的问题

    一般是没有给用户访问权限 给用户test_user授权,让他可以从外部登陆和本地登陆注意:@左边是用户名,右边是域名.IP和%,表示可以访问mysql的域名和IP,%表示外部任何地址都能访问.   m ...

  8. Hash_bzoj1862: [Zjoi2006]GameZ游戏排名系统

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  9. 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 ...

  10. 添加一个功能Action

    1,只用一个handler类,所有都事件的处理器都在一个handler类 handler要创建以Action为名称的方法 event要单独分开,继承KDEvent package com.kingde ...