Markdown版本笔记 | 我的GitHub首页 | 我的博客 | 我的微信 | 我的邮箱 |
---|---|---|---|---|
MyAndroidBlogs | baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
对象引用 方法传参 值传递 引用传递 易错点
目录
概念
Java中有没有引用传递?
答:Java中只有按值传递,没有按引用传递!
当一个对象被当作参数传递到一个方法中后,在此方法中可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
答:是值传递!
- 不管是原始类型还是引用类型,传递的都是【副本】,也可以说是【值】。
- 如果参数类型是【原始类型】,那么传过来的就是这个参数的值,如果在函数中改变了副本的值不会改变原始的值。
- 如果参数类型是【引用类型】,那么传过来的就是这个引用参数的副本(对象的引用),这个副本存放的是参数的【地址】。如果在函数中没有改变这个副本的地址,而是改变了地址中的值,那么在函数内的改变会影响到传入的参数;如果在函数中改变了副本的地址,如new一个,那么副本就指向了一个新的地址,此时传入的参数还是指向原来的地址,所以不会改变参数的值。
方法传递
案例一
public class Test {
public static void main(String[] args) {
int a = 1, b = 2;
System.out.println(a + "," + b); //1,2
swap(a, b);
System.out.println(a + "," + b); //1,2
}
public static void swap(int a, int b) {
int tem = a;
a = b;
b = tem;
}
}
案例二
public class Test {
public static void main(String[] args) {
Person p1 = new Person("1");
Person p2 = new Person("2");
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,1 2
test(p1, p2);
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,2 1
test2(p1, p2);
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,3 4
test3(p1, p2);
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,6 5【乱】
test4(p1, p2);
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,6 5
}
private static void test(Person person1, Person person2) {
String temp = person1.name;
person1.name = person2.name;
person2.name = temp;
}
private static void test2(Person person1, Person person2) {
person1.name = "3";
person2.name = "4";
Person person = person1;
person1 = person2;
person2 = person;
}
private static void test3(Person person1, Person person2) {
Person person = person1; //【乱】
person1 = person2;
person2 = person;
person1.name = "5";
person2.name = "6";
}
private static void test4(Person person1, Person person2) {
person1 = new Person("7");
person2 = new Person("8");
person1.name = "9";
person2.name = "10";
}
}
class Person {
public String name;
public Person(String name) {
this.name = name;
}
}
对象引用
案例一
public class Test {
public static void main(String[] args) {
Work work = new Work();
work.valuse = 1;
Person person = new Person();
person.work = work;
work.valuse = 2;
System.out.println(person.work.valuse); //2
work = new Work();// new 之后 Person 中引用的 Work 就不再是 work 所引用的 Work 了
work.valuse = 3;
System.out.println(person.work.valuse); //2
person.work = work; //将 Person 中引用的 Work 更改为 work 所引用的 Work
System.out.println(person.work.valuse); //3
}
}
class Work {
int valuse;
}
class Person {
Work work;
}
案例二
public class Test {
public static void main(String[] args) {
Work work = new Work();
work.valuse = 1;
ArrayList<Work> list = new ArrayList<Work>();
list.add(work);
Person person = new Person();
person.setList(list);
work.valuse = 2;
System.out.println(person.list.get(0).valuse); //2
person.list.get(0).valuse = 3;
System.out.println(list.get(0).valuse); //3
System.out.println(person.list.get(0).valuse); //3
System.out.println("---------以上并没有更改引用的对象地址--------");
work = new Work();
work.valuse = 4;
System.out.println(list.get(0).valuse); //3
System.out.println(person.list.get(0).valuse); //3
list.set(0, work);
System.out.println(list.get(0).valuse); //4
System.out.println(person.list.get(0).valuse); //4
}
}
class Work {
int valuse;
}
class Person {
List<Work> list;
public void setList(List<Work> list) {
this.list = list;
}
}
案例三
public class Test {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
A a = new A();
a.setList(list);
list.add(2);
System.out.println(a.list1.toString()); //[1, 2]
System.out.println(a.list2.toString()); //[1]
System.out.println(a.list3.toString()); //[1]
a.setList(list);
System.out.println(a.list1.toString()); //[1, 2]
System.out.println(a.list2.toString()); //[1, 2]
System.out.println(a.list3.toString()); //[1, 2]
}
}
class A {
public List<Integer> list1 = new ArrayList<Integer>();
public List<Integer> list2 = new ArrayList<Integer>();
public List<Integer> list3 = new ArrayList<Integer>();
public void setList(List<Integer> list) {
this.list1 = list;
this.list2 = new ArrayList<Integer>(list); //只是添加元素
list3.clear();
this.list3.addAll(list); //同样只是添加元素
}
}
案例四
public class Test {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();//一个集合
list.add(new Person("包青天", 1, new Work("百度", 1)));//添加数据
String name = list.get(0).name;
int age = list.get(0).age;
name = "2";//修改数据。注意,这里的name局部变量和person对象中的name字段指向的是不同的实例对象
age = 2;
Work work = list.get(0).work;
work = new Work("2", 2);
System.out.println(list.get(0).name + "," + list.get(0).age + "," + list.get(0).work.company + "," + list.get(0).work.time);//包青天,1,百度,1
list.get(0).name = "3";
list.get(0).age = 3;
list.get(0).work.company = "3";
list.get(0).work.time = 3;
System.out.println(list.get(0).name + "," + list.get(0).age + "," + list.get(0).work.company + "," + list.get(0).work.time);//3,3,3,3
Person temPerson = list.get(0);
temPerson.name = "4";
temPerson.age = 4;
Work tempWork = temPerson.work;
tempWork.company = "4";
tempWork.time = 4;
System.out.println(list.get(0).name + "," + list.get(0).age + "," + list.get(0).work.company + "," + list.get(0).work.time);//4,4,4,4
}
}
class Work {
int time;
String company;
public Work(String company, int time) {
this.time = time;
this.company = company;
}
}
class Person {
int age;
String name;
Work work;
public Person(String name, int age, Work work) {
this.age = age;
this.name = name;
this.work = work;
}
}
public class Test {
public static void main(String[] args) {
int a = 1, b = 2;
System.out.println(a + "," + b); //1,2
swap(a, b);
System.out.println(a + "," + b); //1,2
}
public static void swap(int a, int b) {
int tem = a;
a = b;
b = tem;
}
}
public class Test {
public static void main(String[] args) {
Person p1 = new Person("1");
Person p2 = new Person("2");
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,1 2
test(p1, p2);
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,2 1
test2(p1, p2);
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,3 4
test3(p1, p2);
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,6 5【乱】
test4(p1, p2);
System.out.println(p1 + " " + p2 + "," + p1.name + " " + p2.name);//Person@15db9742 Person@6d06d69c,6 5
}
private static void test(Person person1, Person person2) {
String temp = person1.name;
person1.name = person2.name;
person2.name = temp;
}
private static void test2(Person person1, Person person2) {
person1.name = "3";
person2.name = "4";
Person person = person1;
person1 = person2;
person2 = person;
}
private static void test3(Person person1, Person person2) {
Person person = person1; //【乱】
person1 = person2;
person2 = person;
person1.name = "5";
person2.name = "6";
}
private static void test4(Person person1, Person person2) {
person1 = new Person("7");
person2 = new Person("8");
person1.name = "9";
person2.name = "10";
}
}
class Person {
public String name;
public Person(String name) {
this.name = name;
}
}
案例一
public class Test {
public static void main(String[] args) {
Work work = new Work();
work.valuse = 1;
Person person = new Person();
person.work = work;
work.valuse = 2;
System.out.println(person.work.valuse); //2
work = new Work();// new 之后 Person 中引用的 Work 就不再是 work 所引用的 Work 了
work.valuse = 3;
System.out.println(person.work.valuse); //2
person.work = work; //将 Person 中引用的 Work 更改为 work 所引用的 Work
System.out.println(person.work.valuse); //3
}
}
class Work {
int valuse;
}
class Person {
Work work;
}
案例二
public class Test {
public static void main(String[] args) {
Work work = new Work();
work.valuse = 1;
ArrayList<Work> list = new ArrayList<Work>();
list.add(work);
Person person = new Person();
person.setList(list);
work.valuse = 2;
System.out.println(person.list.get(0).valuse); //2
person.list.get(0).valuse = 3;
System.out.println(list.get(0).valuse); //3
System.out.println(person.list.get(0).valuse); //3
System.out.println("---------以上并没有更改引用的对象地址--------");
work = new Work();
work.valuse = 4;
System.out.println(list.get(0).valuse); //3
System.out.println(person.list.get(0).valuse); //3
list.set(0, work);
System.out.println(list.get(0).valuse); //4
System.out.println(person.list.get(0).valuse); //4
}
}
class Work {
int valuse;
}
class Person {
List<Work> list;
public void setList(List<Work> list) {
this.list = list;
}
}
案例三
public class Test {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
A a = new A();
a.setList(list);
list.add(2);
System.out.println(a.list1.toString()); //[1, 2]
System.out.println(a.list2.toString()); //[1]
System.out.println(a.list3.toString()); //[1]
a.setList(list);
System.out.println(a.list1.toString()); //[1, 2]
System.out.println(a.list2.toString()); //[1, 2]
System.out.println(a.list3.toString()); //[1, 2]
}
}
class A {
public List<Integer> list1 = new ArrayList<Integer>();
public List<Integer> list2 = new ArrayList<Integer>();
public List<Integer> list3 = new ArrayList<Integer>();
public void setList(List<Integer> list) {
this.list1 = list;
this.list2 = new ArrayList<Integer>(list); //只是添加元素
list3.clear();
this.list3.addAll(list); //同样只是添加元素
}
}
案例四
public class Test {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();//一个集合
list.add(new Person("包青天", 1, new Work("百度", 1)));//添加数据
String name = list.get(0).name;
int age = list.get(0).age;
name = "2";//修改数据。注意,这里的name局部变量和person对象中的name字段指向的是不同的实例对象
age = 2;
Work work = list.get(0).work;
work = new Work("2", 2);
System.out.println(list.get(0).name + "," + list.get(0).age + "," + list.get(0).work.company + "," + list.get(0).work.time);//包青天,1,百度,1
list.get(0).name = "3";
list.get(0).age = 3;
list.get(0).work.company = "3";
list.get(0).work.time = 3;
System.out.println(list.get(0).name + "," + list.get(0).age + "," + list.get(0).work.company + "," + list.get(0).work.time);//3,3,3,3
Person temPerson = list.get(0);
temPerson.name = "4";
temPerson.age = 4;
Work tempWork = temPerson.work;
tempWork.company = "4";
tempWork.time = 4;
System.out.println(list.get(0).name + "," + list.get(0).age + "," + list.get(0).work.company + "," + list.get(0).work.time);//4,4,4,4
}
}
class Work {
int time;
String company;
public Work(String company, int time) {
this.time = time;
this.company = company;
}
}
class Person {
int age;
String name;
Work work;
public Person(String name, int age, Work work) {
this.age = age;
this.name = name;
this.work = work;
}
}
2017-7-4
对象引用 方法传参 值传递 引用传递 易错点 MD的更多相关文章
-
javascript的函数传参(没有引用传递只有值传递)
var v1 = [] var v2 = {}; var v3 = {}; function foo(v1, v2, v3){ v1 = [1]; v2 = [2]; v3 = {a ...
-
微信小程序的页面跳转==编程式导航传参 和 标签的方法传参==以及如何过去传递过来的参数
小程序导航传参接收传递过来的参数 在onload中 实例
-
vue 使用 axios 时 post 请求方法传参无法发送至后台
axios 时 post 请求方法传参无法发送至后台报错如下 Response to preflight request doesn't pass access control check: No ' ...
-
【转】java方法参数传递方式--按值传递、引用传递
java的方法参数传递方式有两种,按值传递和引用传递 1.按值传递 参数类型是int,long等基本数据类型(八大基本数据类型),参数传递的过程采用值拷贝的方式 代码片段1: public class ...
-
再谈Java方法传参那些事
把一个变量带进一个方法,该方法执行结束后,它的值有时会改变,有时不会改变.一开始会觉得--“好神奇呀”.当我们了解java内存分析的知识后,一切都是那么简单明了了--“哦,这么回事呀”.但是今天的上机 ...
-
java的方法传参,最容易混淆的问题!!!
参数传递 参数传递,可以理解当我们要调用一个方法时,我们会把指定的数值,传递给方法中的参数,这样方法中的参数就拥有了这个指定的值,可以使用该值,在方法中运算了.这种传递方式,我们称为参数传递. 在这里 ...
-
MyBatis dao层 方法传参
MyBatis dao层 方法传参有三种方法. 1. 以下标的方法获取参数. <update id="insertSuccessKilled"> INSER ...
-
EasyUI queryParams属性 在请求远程数据同时给action方法传参
http://www.cnblogs.com/iack/p/3530500.html?utm_source=tuicool EasyUI queryParams属性 在请求远程数据同时给action方 ...
-
javascript 字符串方法传参
javascript 字符串方法传参由于嵌套的单引号,双引号过多.有点混乱.. 正确方法如下: ' <td align="left"><input type= ...
随机推荐
-
Less的简单使用
在浏览器中使用LESSCSS 浏览器端使用是在使用LESS开发时最直观的一种方式.如果是在生产环境中,尤其是对性能要求比较高的场合,建议使用node或者其它第三方工具先编译成CSS再上线使用. 浏览器 ...
-
C89标准库函数手册(待整理)
http://zh.cppreference.com/w/c 前言 ANSI C(C89)标准库函数共有15个头文件.这15个头文件分别为: 1.<assert.h> ...
-
EnCase v7 search hits in compound files?
I used to conduct raw search in EnCase v6, and I'd like to see if EnCase v7 raw search could hit key ...
-
android-exploitme(八):内存保护
如果一个手机被锁屏了,但是有个app还在后台运行,这个时候你想知道些app的信息,需要分析他的内存状态. 1. 首先运行模拟器,打开emm,使得模拟器返回锁屏状态 2. 打开ddms,下载内存文件
-
iOS基础 - Modal制作控制器
1.modal 1.modal推出控制器的代码 2.modal关闭当前控制器的代码 3.modal推出的动画效果 4.modal在ipad中应用 2.如何给控制器加上导航栏 3.modal和导航控制器 ...
-
IT经典书籍——Head First系列…
Head First 系列书籍是由 O'Relly 出版社发行的一系列教育书籍,中文一般翻译为"深入浅出",它强调以特殊的方式排版,由大量的图片和有趣的内容组合构成,而达到非疲劳的 ...
-
XML 与 XML Schema的使用教程
引言:我写本文的宗旨在于给需要使用XML,而又对XML不是很熟悉的人们提供一种使用思路,而不没有给出具体的 使用方法,至于下文中提到的使用方法,还未尝试过,都是从网上整理而来! 一.概述 什么 ...
-
【JS基础】类型转换——不同数据类型比较
小试牛刀 输出下列数据比较结果 [] == 0; //==============================================================true [] == ...
-
Unity 5.4版本 Application.systemLanguage 失效
最近在上线双语版本(一个包支持中文.英文二种语言)时,遇到一个坑点 if (ToolUtils.isAndroid()) { if (Application.systemLanguage == Sys ...
-
PAT 甲级 1126 Eulerian Path
https://pintia.cn/problem-sets/994805342720868352/problems/994805349851185152 In graph theory, an Eu ...