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 ...
随机推荐
- 【BZOJ 2157】旅游
再水一道模板题,明天就要出发去参加二轮省选了赶紧复习复习模板. 链剖模板题,可是写链剖太麻烦了,还是写lct吧. 但这个lct比较麻烦了,因为边权有正有负,要统计最大值和最小值,这样点权赋为什么值都会 ...
- HTMLTestRunner修改Python3的版本
在拜读虫师大神的Selenium2+Python2.7时,发现生成HTMLTestRunner的测试报告使用的HTMLTestRunner的模块是用的Python2的语法.而我本人比较习惯与Pytho ...
- shell 字符串截取
${expression}一共有9种使用方法. ${parameter:-word},如果parameter为空,则用word的值做parameter的缺省值 ${parameter:=word},在 ...
- ECIF OCRM ACRM关系
ECIF :Enterprise Customer Information Facility 企业客户信息工厂: CRM:Customer Relationship Management 客户关系管 ...
- javaweb项目打包成war包
从来没有想过web项目还能打包的,但是有要求,就不得不去实现,在网上找了一下,发现挺简单的. 首先是使用MyEclipse将web项目打包,如下图所示. 右键选中项目,选择export. 然后选择J2 ...
- Leetcode 132. Palindrome Partitioning II
求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...
- BZOJ1853 [Scoi2010]幸运数字
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- CSS中的a标签几个访问状态记录
a:link {color: #FF0000} /* 未访问的链接 */a:visited {color: #00FF00} /* 已访问的链接 */a:hover {color: #FF0 ...
- Writing a simple Lexer in PHP/C++/Java
catalog . Comparison of parser generators . Writing a simple lexer in PHP . phc . JLexPHP: A PHP Lex ...
- QCustomPlot 使用整理
QCustomPlot 是一个比较小的 QT 图表插件.使用时,我们在程序中写完相关调用的代码后,只需将 QCunstomPlot.cpp 和 QCustomPlot.h 两个文件加入工程,正常编译即 ...