对象引用 方法传参 值传递 引用传递 易错点 MD
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 ' ...
- C++之值传递&指针传递&引用传递详解
C++之值传递&指针传递&引用传递详解 目录 C++之值传递&指针传递&引用传递详解 1.函数基础 2.值传递 3.指针传递 4.引用传递 1.函数基础 一个函数由以下 ...
- 【转】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方 ...
随机推荐
- Xamarin iOS教程之页面控件
Xamarin iOS教程之页面控件 Xamarin iOS 页面控件 在iPhone手机的主界面中,经常会看到一排小白点,那就是页面控件,如图2.44所示.它是由小白点和滚动视图组成,可以用来控制翻 ...
- MOXA的Nport5600初始密码
今天第一次弄Nport,看了半天手册没找到初始密码,网上也搜不到,按照说明书上想打电话问问,发现根本是空号... 后来灵感一来试了一下,居然是:moxa
- 数据转换bug花了半天时间 Java.math.BigDecimal cannot be cast to java.lang.String
从数据库取出一个 Count函数 统计的值 在代码中要转成Integer类型的时候 Integer.parseInt((String)map.get("ID_")) 报了一下错误: ...
- 【bzoj3451】Tyvj1953 Normal 期望+树的点分治+FFT
题目描述 给你一棵 $n$ 个点的树,对这棵树进行随机点分治,每次随机一个点作为分治中心.定义消耗时间为每层分治的子树大小之和,求消耗时间的期望. 输入 第一行一个整数n,表示树的大小接下来n-1行每 ...
- BZOJ.4517.[SDOI2016]排列计数(错位排列 逆元)
题目链接 错位排列\(D_n=(n-1)*(D_{n-1}+D_{n-2})\),表示\(n\)个数都不在其下标位置上的排列数. 那么题目要求的就是\(C_n^m*D_{n-m}\). 阶乘分母部分的 ...
- php7 & lua 压测对比
内存:32G CPU:2个6核 接口数据deflate 压缩后 均不到10k, ==== php7 ==== Concurrency Level: 100 Time taken for tests: ...
- HDU 5842 Lweb and String 水题
Lweb and String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5842 Description Lweb has a string S ...
- HDU 3979 Monster (贪心排序)
Monster Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 视频监控系统:C/S & B/S
近期一直和视频监控打交道.也做了大量的工作.当中包括了监控系统的两种模式实现: 1.C/S架构 C/S架构,主要是Android系统下监控client设计,基于VLC实现,可直接观看IP摄像机RTSP ...
- CRC 详解
http://www.barrgroup.com/Embedded-Systems/How-To/Additive-Checksums CRC Series, Part 1: Additive Che ...