传值和传引用

When you’re passing primitives into a method ,you get a distinct copy of the primitive. When you’re passing a reference into a method , you get a copy of the reference.

以上引自《Thinging in Java》。总结一下就是无论Java參数的类型是什么。一律传递參数的副本。

在Java中,变量分为以下两类:

  1. 对于基本类型变量(int、long、double、float、byte、boolean、char),Java是传值的副本。

  2. 对于一切对象型变量,Java都是传引用的副本,事实上穿引用副本的实质就是复制指向地址的指针。

一、传值

例1:传Int类型的值

程序

    @Test
public void passInt(){
int testInt = 0;
System.out.println("Before operation, testInt is : " + testInt);
intOperation(testInt);
System.out.println("After operation, testInt is : " + testInt);
} public void intOperation(int i){
i = 5;
System.out.println("In operation, testInt is : " + i);
}

结果

Before operation, testInt is : 0
In operation, testInt is : 5
After operation, testInt is : 0

总结

结果不难看出来。虽说intOperation()方法改变了传进来的參数值,但对这个參数源本身并没有影响。參数类型是简单类型的时候。是按值传递的。以參数类型传递简单类型的变量时,实际上是将參数的值作为一个副本传进方法函数的,那么在方法函数中无论怎么改变值,其结果都是仅仅改变了副本的值,而不是源值

二、传引用

例2:传对象型变量

代码

@Test
public void passClass(){
Person person = new Person(175,140); System.out.println("Before classOperation, the person height is " + person.height +
" and the weight is " + person.weight); classOperation(person); System.out.println("After classOperation, the person height is " + person.height +
" and the weight is " + person.weight);
} public void classOperation(Person person){
person.height = 190;
person.weight = 160; System.out.println("In classOperation, the person height is " + person.height +
" and the weight is " + person.weight); } public class Person{ int height;
int weight; public Person(int hei, int wei){
this.height = hei;
this.weight = wei;
}
}

结果

Before classOperation, the person height is 175 and the weight is 140
In classOperation, the person height is 190 and the weight is 160
After classOperation, the person height is 190 and the weight is 160

总结

从结果不难看出,在经过classOperation()处理之后。person的值发生了变化。

传引用和之前的传值是有一些差别的。能够这样理解:

  • 传值:当前的值好比是一个苹果A。在Java中的传值就相当于是把这个苹果复制了一个苹果B,实际上传递的事实上是苹果B,也就是说无论对苹果B做出如何的改动,苹果A是不变的。

  • 传引用:能够这样理解,当前的person是一个仓库,由于这个仓库(对象型变量)不像基本类型变量那么小,就好比一个仓库比一个苹果大的多的多一样。聪明的Java不会每次传递这个仓库的时候就又一次复制一份仓库传过去,它的做法是把钥匙A复制一把钥匙B(钥匙就相当于引用。引用指向仓库),传递的时候把钥匙B传递过去,和传值相似的地方在于钥匙B怎么改变都不影响钥匙A这个引用的值,可是钥匙A和钥匙B都指向同一个仓库,也就是说通过钥匙B来改变了这个仓库的值(比方偷取走一万吨粮食)。那么这个仓库的值就确确实实改变了,假设再通过钥匙A訪问这个仓库。得到的结果和B訪问时一样的(少了一万吨粮食)。

例3:传String类型

代码

    @Test
public void passString(){
String testString = "Hello";
System.out.println("Before operation, testString is : " + testString);
stringOperation(testString);
System.out.println("After operation, testString is : " + testString); } public void stringOperation(String s){
s = "World";
System.out.println("In operation, testString is : " + s);
}

结果

Before operation, testString is : Hello
In operation, testString is : World
After operation, testString is : Hello

总结

String类型也是对象型类型,所以它是传引用副本。

可是问题来了,String类型也是对象型类型,那么为什么String类型的对象。经过stringOperation()后。值没有发生变化?

首先String类型是对象型变量,所以它是传引用的副本。

不要由于String在Java里面很易于使用并且不须要new,就觉得String是基本变量类型。

仅仅只是String是一个非可变类,使得其传值还是穿引用显得没什么差别。

然后来解决上面说的问题。事实上问题是出在String的创建上面了,在stringOperation()中。s = "World";这条语句相当于运行了String s = new String("World");。对,也就是说,在这种方法中的s相当于是一个新的String对象,当这个函数结束时,s作用消失。原来内存地址的值没有变化。

以下来具两个样例来说明这个问题:

例4:传String类型——使用new来创建新对象

代码

    @Test
public void passString(){
String testString = new String("Hello");
System.out.println("Before operation, testString is : " + testString);
stringOperation(testString);
System.out.println("After operation, testString is : " + testString); } public void stringOperation(String s){
s = new String("World");
System.out.println("In operation, testString is : " + s);
}

输出

Before operation, testString is : Hello
In operation, testString is : World
After operation, testString is : Hello

总结

例4和例3的差别就在于,创建String都使用了new,最后的输出结果是不一样的。

例5:传对象型变量——在classOperation()使用new创建

代码

public class Person{

        int height;
int weight; public Person(int hei, int wei){
this.height = hei;
this.weight = wei;
}
} @Test
public void passClass2(){ Person person = new Person(175,140); System.out.println("Before classOperation, the person height is " + person.height +
" and the weight is " + person.weight); classOperation2(person); System.out.println("After classOperation, the person height is " + person.height +
" and the weight is " + person.weight);
} public void classOperation2(Person person){ person = new Person(190,160); System.out.println("In classOperation, the person height is " + person.height +
" and the weight is " + person.weight); }

结果

Before classOperation, the person height is 175 and the weight is 140
In classOperation, the person height is 190 and the weight is 160
After classOperation, the person height is 175 and the weight is 140

总结

最后一个样例事实上就是为了说明一个情况,例3的stringOperation()中看到了字符串的赋值操作事实上就相当于例5中classOperation2()里面new出来的新对象。

三、总结

上面的分析主要是基于结果来推断的,可能不是特别准确。望批评指正。

文章来源:http://blog.csdn.net/zhaodedong

Java学习笔记:具体解释传值和传引用的更多相关文章

  1. JAVA方法传递参数:传值?传引用?

    先来看下面这三段代码: //Example1: public class Example1 { static void check(int a) { a++; } public static void ...

  2. java学习笔记 (8) —— Struts2 实现上传

    1.新建upload.jsp <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  3. GO学习笔记:函数传值与传指针

    当我们传一个参数值到被调用函数里面时,实际上是传了这个值的一份copy,当在被调用函数中修改参数值的时候,调用函数中相应实参不会发生任何变化,因为数值变化只作用在copy上. 为了验证我们上面的说法, ...

  4. 难道同事:Java 方法调用到底是传值还是传引用

    Java 方法调用中的参数是值传递还是引用传递呢?相信每个做开发的同学都碰到过传这个问题,不光是做 Java 的同学,用 C#.Python 开发的同学同样肯定遇到过这个问题,而且很有可能不止一次. ...

  5. 难住了同事:Java 方法调用到底是传值还是传引用

    Java 方法调用中的参数是值传递还是引用传递呢?相信每个做开发的同学都碰到过传这个问题,不光是做 Java 的同学,用 C#.Python 开发的同学同样肯定遇到过这个问题,而且很有可能不止一次. ...

  6. 关于Java对象作为参数传递是传值还是传引用的问题

    前言 在Java中,当对象作为参数传递时,究竟传递的是对象的值,还是对象的引用,这是一个饱受争议的话题.若传的是值,那么函数接收的只是实参的一个副本,函数对形参的操作并不会对实参产生影响:若传的是引用 ...

  7. Java实参和形参与传值和传引用

    实参和形参的定义: 形参出现函数定义中,在整个函数体内都可以使用,离开函数则不能使用. 实参出现在主函数中,进入被调函数后,实参变量也不能使用. 形参和实参的功能是做数据传送.发生函数调用时,主调函数 ...

  8. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  9. Java学习笔记(六):面向对象、接口和抽象类

    类和对象 Java是一门面向对象的语言,下面我们来了解一下Java中的面向对象. 方法和重载 Java中的方法格式如下: 访问修饰符 返回值类型 方法名(参数){ 方法主体 } Java的方法支持重载 ...

随机推荐

  1. MySQL 5.6学习笔记(函数)

    1. 数学函数 ABS(x)   返回x的绝对值BIN(x)     返回x的二进制(OCT返回八进制,HEX返回十六进制)CEIL(x)或CEILING(x)   返回大于x的最小整数值EXP(x) ...

  2. Lua中and、or的一些特殊使用方法

    Lua中的逻辑运算符:与(and).或(or)和非(not),与其它语言的逻辑运算符功能一致,这里不做赘述.仅仅说一点,全部的逻辑运算符将false和nil视为假,其它不论什么东西视为真.0也视为真. ...

  3. Android:UI 沉浸式体验,适合第一屏的引导图片、预览图片。

    链接:http://www.cnblogs.com/liushilin/p/5799381.html

  4. php获取图片RGB颜色值的例子

    php获取图片RGB颜色值的例子 很多图片站点都会根据用户上传的图片检索出图片的主要颜色值,然后在通过颜色搜索相关的图片. 之前按照网上的方法将图片缩放(或者马赛克)然后遍历每个像素点,然后统计处RG ...

  5. Atitit 图像处理类库 halcon11  安装与环境搭建attilax总结

    Atitit 图像处理类库 halcon11  安装与环境搭建attilax总结 正常安装软件,安装前请先退出其它一切正在运行的程序. 先安装halcon-10.0-windows.exe.安装完成后 ...

  6. [100]linux常用命令参数小结

    1.mkdir 2.ls -l -d 显示目录 -F 给文件夹结尾加/标识 -a 以.开头的都是隐藏文件 -rt 按照修改时间倒序排列(最新修改的在最下) ls -lhrt #最新的在下面 3.cd ...

  7. Egret入门了解

    0.前言 这个星期没有什么事做,就想找点技术了解一下.前段时间看过Egret,用来开发HTML5小游戏.一开始以为很麻烦的,但是经过这两天了解了一下,如果用这个游戏引擎来开发一些简单的游戏,还是蛮方便 ...

  8. at org.apache.catalina.webresources.CachedResource.validateResources

    "catalina-exec-659" #5239 daemon prio=5 os_prio=0 tid=0x00007fcba8099800 nid=0x581 waiting ...

  9. SQL Server 2008 R2:error 26 开启远程连接详解

    远程连接sql server 2008 数据库,出现下面的错误: <--在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.    未找到或无法访问服务器.请验证实例名称是 ...

  10. spark on alluxio和MR on alluxio测试(改进版)【转】

    转自:http://kaimingwan.com/post/alluxio/spark-on-alluxiohe-mr-on-alluxioce-shi-gai-jin-ban 1. 介绍 2. 准备 ...