Os fields primitivos são passados como valor, mas para os objetos é passado uma cópia da referência por valor, ou apenas uma cópia da referência como queira. Qdo se tira uma cópia de um objeto, a referência original e a cópia feita apontam para o mesmo objeto, se vc usar um método que altera o objeto, as duas referências perceberão esta alteração, mas se na cópia vc atribuir um novo objeto, esta perderá a referência para o objeto original e começará a apontar para esta nova atribuição, existem vários exemplos a respeito disso, abaixo existem alguns retirados da própria página da sun, o endereço é http://developer.java.sun.com/developer/JDCTechTips/2001/tt1009.html, eu fiz um outro (PassRef3) tb que vcs poderão testar a alteração do mesmo objeto, abaixo ...class PassRef3 {
public static void main(String args[]) { int val; StringBuffer sb1, sb2; val = 10;
sb1 = new StringBuffer("PRIMEIRA STRING"); sb2 = new StringBuffer("LUIZ"); System.out.println("val: " + val); System.out.println("sb1: " + sb1); System.out.println("sb2: " + sb2); System.out.println(""); System.out.println("chamando a metodo
modify");
//todos os parâmetros passados por valor (by val) modify(val, sb1, sb2); System.out.println("retornado da modify"); System.out.println(""); System.out.println("val: " + val);
System.out.println("sb1: " + sb1); System.out.println("sb2: " + sb2); //r2 foi realmente alterado, pois foi usado um método //do próprio objeto, e todo método disparado de qq objeto //mesmo que este seje uma cópia, será alterado o objeto //de origem, ou seja, o mesmo objeto. } public static void modify(int a, StringBuffer r1, StringBuffer r2)
{
System.out.println("estou na modify..."); a = 0; r1 = null; //1 r2.append(" EH UM CARA LEGAL !"); System.out.println("a: " + a); System.out.println("r1: " + r1); System.out.println("r2: " + r2); } } http://developer.java.sun.com/developer/JDCTechTips/2001/tt1009.htmlHOW ARGUMENTS ARE PASSED TO JAVA METHODSSuppose you're doing some Java programming, and you have a simple program like this: public class CallDemo1 { static void f(int arg1) { arg1 = 10; } public static void main(String args[]) { int arg1; arg1 = 5; f(arg1); System.out.println("arg1 = " + arg1); } } In the main method, the variable What happens when you run this simple program? The method Why does it work this way? The answer has to do with the distinction between the pass-by-value and pass-by-reference approaches to passing arguments to a method. The Java language uses pass-by-value exclusively. Before explaining what this means, let's look at an example of pass-by-reference, using C++ code: // CallDemo1.cpp #include <iostream> using namespace std; void f(int arg1, int& arg2) { arg1 = 10; arg2 = 10; } int main() { int arg1, arg2; arg1 = 5; arg2 = 5; f(arg1, arg2); cout << "arg1 = " << arg1 << " arg2 = " << arg2 << endl; } Function So what does pass-by-value actually mean? To answer this, it's instructive to look at some JVM1 bytecodes that result from the following two commands: javac CallDemo1.java javap -c -classpath . CallDemo1 Here is an excerpt from the output: Method void f(int) 0 bipush 10 2 istore_0 3 return Method void main(java.lang.String[]) 0 iconst_5 1 istore_1 2 iload_1 3 invokestatic #2 <Method void f(int)> In the main method, the instruction Then the method What this means is that the parameters in a method are copies of the argument
values passed to the method. If you modify a parameter, it has no effect on the
caller. You are simply changing the copy's value in the stack frame that is used
to hold local variables. There is no way to "get back" at the arguments in the
calling method. So assigning to By contrast, a pass-by-reference parameter is implemented by passing the memory address of the caller's argument to the called function. The argument address is copied into the parameter. The parameter contains an address that references the argument's memory location so that changes to the parameter actually change the argument value in the caller. In low-level terms, if you have the memory address of a variable, you can change the variable's value at will. The discussion of argument passing is complicated by the fact that the term "reference" in pass-by-reference means something slightly different than the typical use of the term in Java programming. In Java, the term reference is used in the context of object references. When you pass an object reference to a method, you're not using pass-by-reference, but pass-by-value. In particular, a copy is made of the object reference argument value, and changes to the copy (through the parameter) have no effect in the caller. Let's look at a couple of examples to clarify this idea: class A { public int x; A(int x) { this.x = x; } public String toString() { return Integer.toString(x); } } public class CallDemo2 { static void f(A arg1) { arg1 = null; } public static void main(String args[]) { A arg1 = new A(5); f(arg1); System.out.println("arg1 = " + arg1); } } In this example, a reference to an Here's another example: class A { public int x; public A(int x) { this.x = x; } public String toString() { return Integer.toString(x); } } public class CallDemo3 { static void f(A arg1) { arg1.x = 10; } public static void main(String args[]) { A arg1 = new A(5); f(arg1); System.out.println("arg1 = " + arg1); } } What gets printed here is In Java programming, it's common to say things like "a String object is
passed to method What are the implications of pass-by-value? One is that when you pass objects or arrays, the calling method and the called method share the objects, and both can change the object. So you might want to employ defensive copying techniques, as described in the September 4, 2001 Tech Tip, "Making Defensive Copies of Objects" You can fix the case above, where the called method modifies an object, by making the class immutable. An immutable class is one whose instances cannot be modified. Here's how you to do this: final class A { private final int x; public A(int x) { this.x = x; } public String toString() { return Integer.toString(x); } } public class CallDemo4 { static void f(A arg1) { //arg1.x = 10; } public static void main(String args[]) { A arg1 = new A(5); f(arg1); System.out.println("arg1 = " + arg1); } } The printed result is Another implication of pass-by-value is that you can't use method parameters to return multiple values from a method, unless you pass in a mutable object reference or array, and let the method modify the object. There are other ways of returning multiple values, such as returning an array from the method, or creating a specialized class and returning an instance of it. For more information about how arguments are passed to Java Methods, see Section 1.8.1, Invoking a Method, and section 2.6.4, Parameter Values, in "The JavaTM Programming Language Third Edition" by Arnold, Gosling, and Holmes. Also see item 13, Favor immutability, and item 24, Make defensive copies when needed, in "Effective Java Programming Language Guide" by Joshua Bloch
|
- RES: [java-list] Como ? feita a passasgem de pa... ANDRE MOURA BARRETO
- Re: RES: [java-list] Como ? feita a passas... Sven van ´t Veer
- Re: RES: [java-list] Como ? feita a passas... luis . bodra
- Re: Re: RES: [java-list] Como ? feita ... Sven van ´t Veer
- Re: Re: RES: [java-list] Como ? fe... Leonardo Bueno
- Re: Re: RES: [java-list] Como ? fe... Leonardo Bueno
- Re: Re: RES: [java-list] Como ? fe... Emerson Santana Pardo
- Re: Re: RES: [java-list] Como ? fe... Lincolm Ferreira de Aguiar
- Luiz H - IEG