The parameters of Java mythology are passed by way of address

Source: Internet
Author: User

In different Java newsgroups, whether parameters are passed or transmitted is always a topic of contention. The center of the misunderstanding is the following two facts:

object is a reference

parameter is a value-passing

Can these two be set up at the same time? One word: Yes! In Java, you never pass objects, you pass only the object's Reference! In a word, Java is a reference. However, when you pass a parameter, there is only one parameter passing mechanism: the value!

Typically, when programmers talk about values and references, they refer to the language's parameter-passing mechanism, and C + + supports both mechanisms, so programmers who have used C + + before are beginning to seem unsure how Java can pass parameters. The Java language simply supports the mechanism of parameter-passing for things to become simple.

There are two types of variables in Java: reference types and original types. When they are passed as arguments to the method, they are all passed values. This is a very important difference, as the following code example illustrates. Before continuing, it is necessary to define the values and references.

Passing a value means that when a parameter is passed to a method or function, the method or function receives a copy of the original value. Therefore, if the method or function modifies the parameter, only the copy is affected, and the original value remains unchanged.

The confusion about parameter passing in Java is that many Java programmers are transformed from C + +. C + + has variables of reference and unreferenced types, and is worth it by passing and passing references. The Java language has primitive types and object references, then logically, Java uses a pass value for the original type and a reference type, just like C + +. After all, you will think that if you are passing a reference, it must be a reference. It's a tempting idea, but it's wrong!

In C + + and Java, when the parameter of a function is not a reference, you pass a worthy copy (value). However, it is different for reference types. In C + +, when a parameter is a reference type, you pass a reference or a memory address (a reference), whereas in Java, the result of passing a reference type parameter is simply passing a copy of the reference (a value) rather than referencing itself.

This is a very important difference! Java does not consider the type of the parameter, all passes a copy of the parameter. Still don't believe it? If Java is a reference, the Swap method in the following example swaps their arguments. This method is not working as expected because it is a pass value.

class Swap
{
public static void main(String args[])
{
Integer a, b;
int i,j;
a = new Integer(10);
b = new Integer(50);
i = 5;
j = 9;
System.out.println(\"Before Swap, a is \" + a);
System.out.println(\"Before Swap, b is \" + b);
swap(a, b);
System.out.println(\"After Swap a is \" + a);
System.out.println(\"After Swap b is \" + b);
System.out.println(\"Before Swap i is \" + i);
System.out.println(\"Before Swap j is \" + j);
swap(i,j);
System.out.println(\"After Swap i is \" + i);
System.out.println(\"After Swap j is \" + j);
}
public static void swap(Integer ia, Integer ib)
{
Integer temp = ia;
ia = ib;
ib = temp;
}
public static void swap(int li, int lj)
{
int temp = li;
li = lj;
lj = temp;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.