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

例子<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的更多相关文章

  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从游标批量提取数据

    来源于:http://blog.csdn.net/ceclar123/article/details/7974973 传统的fetch into一次只能取得一条数据,使用fetch bulk coll ...

  2. Java--剑指offer(4)

    16.输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. a)这里首先判断两个链表中有没有空表,这个就是依据表头是否为空.然后就是比较节点值的大小,然后就是使 ...

  3. XML 简介

    什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输数据,而非显示数据 XML 标签没 ...

  4. jQuery常用的元素查找方法总结

    $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("di ...

  5. Apache Shiro和Spring Security的详细对比

    参考资料: 1)Apache Shiro Apache Shiro:http://shiro.apache.org/ 在Web项目中应用 Apache Shiro:http://www.ibm.com ...

  6. maven-腾讯SDK(QQ)接口java引入pom配置

    maven的pom.xml配置 <dependency> <groupId>net.gplatform</groupId> <artifactId>Sd ...

  7. Spring-程序中获取注册bean的方式

    获得spring里注册Bean的四种方法,特别是第三种方法,简单: 一:方法一(多在struts框架中)继承BaseDispatchAction  import com.mas.wawacommuni ...

  8. 6 this的使用方法

    class Person { String name; void talk() { System.out.println("my name is "+this.name); } } ...

  9. Lucene学习笔记

    师兄推荐我学习Lucene这门技术,用了两天时间,大概整理了一下相关知识点. 一.什么是Lucene Lucene即全文检索.全文检索是计算机程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明 ...

  10. 面向对象_python

    面向对象_python 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定 ...