java没有引用传递只有按值传递,没有引用传递只有按值传递,值传递。
因为Primitive类型的值不能改变,所以method不能更改调用方传的primitive 值。因为method更改的是Primitive变量的copy,所以method的更改,调用方是不知道的

因为Reference Data Type的内存地址不能被method中操作更改,所以调用方中Reference Data 对象中的状态 也发生变化。因为method操作的对象,与调用方是相同的,即调用方是知道method中的更改的

只不过Primitive类型传的是实际的数据,Reference Data Type 传的是内存地址。

入参为Reference Data Type的方法中,可以更改这个对象状态的原因,就是因为入参对象的内存地址是不能改变,这样在method中更改这个对象的属性值,调用方的该对象的相关属性值也发生变化

Passing Reference Data Type Arguments

Reference data type parameters, such as objects, are also passed into methods by value.

This means that when the method returns, the passed-in reference still references the same object as before.
However, the values of the object's fields can be changed in the method, if they have the proper access level.

import org.junit.Test;

public class CircleTest {
@Test
public void moveCircle() throws Exception {
Circle circle=new Circle(10,10);
circle.print(circle);
circle.moveCircle(circle, 20, 20);
circle.print(circle);
} }

输出:

moveCircle:Circle{x=10, y=10},hash code:1252585652
moveCircle:Circle{x=0, y=0},hash code:2036368507
moveCircle:Circle{x=30, y=30},hash code:1252585652

Let the method be invoked with these arguments:

moveCircle(myCircle, 20, 20)

Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 20 and 20, respectively. These changes will persist when the method returns.
Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change.
Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

public class Circle {
private int x;
private int y; public Circle(int x, int y) {
this.x = x;
this.y = y;
} public void moveCircle(Circle circle, int deltaX, int deltaY) {
// code to move origin of circle to x+deltaX, y+deltaY
circle.setX(circle.getX() + deltaX);
circle.setY(circle.getY() + deltaY); // code to assign a new reference to circle
circle = new Circle(0, 0); //自这行后,moveCircle方法就丢失入参circle---即不能再操作入参circle。就像连续对一个primitive 变量赋值,后一次操作会覆盖前面的
print(circle);
}
public void print(Circle circle) {
System.out.println("moveCircle:"+circle+",hash code:"+circle.hashCode());
} public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
} @Override
public String toString() {
return "Circle{" +
"x=" + x +
", y=" + y +
'}';
}
}

Passing Primitive Data Type Arguments

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost. Here is an example:

public class PassPrimitiveByValue {

    public static void main(String[] args) {

        int x = 3;

        // invoke passMethod() with
// x as argument
passMethod(x); // print x to see if its
// value has changed
System.out.println("After invoking passMethod, x = " + x); } // change parameter in passMethod()
public static void passMethod(int p) {
p = 10;
}
}

When you run this program, the output is:

After invoking passMethod, x = 3

http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

通过下面代码解释:

 public class Test {
public static void main(String[] args ){
int var = 1 ;
f(var) ;
System.out.println(var) ;
}
public static void f(int newVar ){
newVar = 2 ;
}
}

执行结果:

1

分析:

当执行 int var = 1 时,jvm在栈中开辟一块空间存放值---1,同时var变量指向值1所对应的内存空间,也就是var变量也有自己的内存空间,不过它的空间里存放的是值1所对应的内存地址。

当执行到第七行,要将var的值传递进方法f中时,jvm执行的操作是创建一个新的变量newVar,并将var里存放的值(也就是值1的内存地址)复制一份给newVar。

当执行到第八行时,jvm重新开辟值--2所对应的存储空间来存储值2,并修改了newVar里存放的地址值。

方法返回,执行到第五行,输出var所指向的值,当然是值1,因为var中的地址值根本没有变化。

上面示例用的是int,一个基本数据类型,对于引用数据类型同样适用,因为java中没有引用传递,只有值传递。例如自定义的类UserClass:

 class MyClass {
int var1 ;
}
public class Test {
public static void main(String[] args ){
MyClass myClass = new MyClass() ;
myClass.var1 = 1 ;
f(myClass) ;
System.out.println(myClass.var1) ;
}
public static void f(MyClass newMyClass ){
myClass.var1 = 100 ;
}
}

执行结果:

100

分析:

第六行:在栈中创建一个变量指向在堆中创建的对象。关于堆中对象属性的存储可以理解为每创建一个对象就会在堆中分配一块内存空间给改对象,这块内存空间中记录了该对象属于哪个类,剩下就是存储该对象的属性值。

第七行修改对象属性值:

下面就是关键的第八行值的传递了,jvm会在栈中在开辟一块空间newMyClass,然后把myClass里的内容拷贝进newMyClass,这样在方法f中对newMyClass的修改就是对MyClass所对应内容的修改,因为newMyClass也是指向与myClass同样的一块内存空间。

第十二行中对newMyClass属性的修改同样会影响myClass所指的内容,因为他们俩指的是同一块内存空间。

最后方法返回myClass所指内容同样被修改。

下面说一个比较特殊的类----String,这是java中的一个特殊的引用类型,使用String传递时会发现在方法中修改不会影响到方法外的值,例如下面这段代码:

 public class Test {
public static void main(String[] args ){
String var = "Hello" ;
f(var) ;
System.out.println(var) ;
}
public static void f(String newVar ){
newVar = "World" ;
}
}

运行结果:

Hello

在分析这段代码之前需要知道String的底层是怎样实现的,其实String是通过char数组来完成其功能,简单的说String就是对char[]的一个封装。还要明白直接“字符串内容”和使用new String(“字符串内容”)效果是一样的,只不过jvm对这两种创建的字符串存储的地方不同而已。对上面这段代码的分析当然还要使用对引用数据传值方法分析。

第三行jvm在静态存储区存放创建的“Hello”字符串,并在栈中开辟var指向Hello的地址(var中存储的值是Hello字符串的地址)。当将var作为参数传递时,jvm会在栈中新创建一个变量newVar,这时newVar是指向"Hello"内存区的,但是在第八行 newVar = “World”却把newVar中存储的“Hello”地址改为一个新创建的“World”String对象的地址,注意,var内存储的地址仍然是“Hello”,所以在方法返回时输出的仍然是Hello。

终于写完了~~~ 发现自己想和写完全是两码事‘(*>﹏<*)′,其中一定有错误,在内存分配地方没有说清楚,因为我也不太清楚,还有就是关于对象中属性的内存分配,这个我真不知道,哪位知道别忘告诉我~~

http://www.cnblogs.com/caiyao/p/4964176.html

passed into methods by value java专题的更多相关文章

  1. Using Headless Mode in the Java SE Platform--转

    原文地址: By Artem Ananiev and Alla Redko, June 2006     Articles Index This article explains how to use ...

  2. Thinking in Java——笔记(15)

    Generics The term "generic" means "pertaining or appropriate to large groups of class ...

  3. Effective Java 29 Consider typesafe heterogeneous containers

    When a class literal is passed among methods to communicate both compile-time and runtime type infor ...

  4. Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses

    5.1. Classes, Superclasses, and SubclassesLet's return to the Employee class that we discussed in th ...

  5. java 并发官方教程

    http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html Concurrency Computer users t ...

  6. Using JAAS Authentication in Java Clients---weblogic document

    The following topics are covered in this section: JAAS and WebLogic Server JAAS Authentication Devel ...

  7. Java 8 Features – The ULTIMATE Guide--reference

    Now, it is time to gather all the major Java 8 features under one reference post for your reading pl ...

  8. java按值传递理解(转)

    ava没有引用传递只有按值传递,没有引用传递只有按值传递,值传递. 通过下面代码解释: 1 public class Test { 2 public static void main(String[] ...

  9. Java 脚本化编程指南

    Java 脚本化编程指南 Java脚本化API为谁准备? 脚本语言的一些有用的特性是: 方便:大多数脚本语言都是动态类型的.您通常可以创建新的变量,而不声明变量类型,并且您可以重用变量来存储不同类型的 ...

随机推荐

  1. 利用C#与AE调用GP工具

    转自原文 利用C#与AE调用GP工具 第一,首先要明确自己需要调用arctoolbox里面的什么工具,实现什么样的功能. 第三,编写command或tool工具,编写自己要的功能工具. 1)首先创建一 ...

  2. uml精粹——11.活动图(及整个读书笔记分享)

    11.活动图activity diagram   活动图是描写叙述过程化逻辑procedural logic.业务过程business process和工作流work flow的技术. 他和流程图fl ...

  3. DOM中Event 对象如何使用

    DOM中Event 对象如何使用 一.总结 一句话总结: 1.将event作为参数传递进来,然后就可以调用event对象的各种属性和方法了. <body onmousedown="wh ...

  4. tcp长连接和短连接

    tcp长连接和短连接 TCP在真正的读写操作之前,server与client之间必须建立一个连接, 当读写操作完成后,双方不再需要这个连接时它们可以释放这个连接, 连接的建立通过三次握手,释放则需要四 ...

  5. js获取计算后的样式表

    在编写html时,使用dom对象的style属性可以获取标签里的style属性,但是不能获取单独css样式文件或者style标签的属性值 <div style="width:10px& ...

  6. bat文件从@含义起

    今天看到一个批处理文件,内容很简单,执行很方便,学习了一下才知道就是一条条的dos命令, 掌握其中的几个常用命令能看懂别人的文件就行了 1.@ 一般紧随其后 类似@echo off 其作用类似于ech ...

  7. HDU 1816, POJ 2723 Get Luffy Out(2-sat)

    HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...

  8. TensorFlow 学习(七) — 常用函数 api、tf.nn 库

    0. 四则运算 平方:tf.square(),开方:tf.sqrt() tf.add().tf.sub().tf.mul().tf.div().tf.mod().tf.abs().tf.neg() 1 ...

  9. SVM明确的解释1__ 线性可分问题

    笔者:liangdas 出处:简单点儿,通俗点儿,机器学习    http://blog.csdn.net/liangdas/article/details/44251469 引言: 1995年Cor ...

  10. Java类、实例的初始化顺序

    今晚是阿里巴巴 2013 校园招聘的杭州站笔试.下午匆忙看了两张历年试卷,去现场打了瓶酱油. 题目总体考察点偏基础,倒数第二题(Java 附加题)比较有趣,考察了 Java 初始化机制的细节,在此摘录 ...