Java and java

Source: Internet
Author: User

Java and java

When calling a function, the passed parameter may have been a value or an address. If it is a value transfer, the operation inside the function does not affect the value of the parameter; if it is an address transfer, the operation inside the function is to operate on the memory pointed to by the parameter, it will affect the value of the parameter.

Is Java A value or an address? Use the following example to perform an experiment:

package test;import java.util.ArrayList;import java.util.List;public class ValueAndAddress {        public static void f1(int x){        x = x + 5;        return;    }        public static void f2(String s){        s = new String("10000");        return;    }        public static void f3(List<Integer> list){        list.remove(0);        list.remove(1);        list.remove(2);        return;    }        public static void main(String[] args) {        int x = 5;        String s = new String("abcdef");        List<Integer> list = new ArrayList<Integer>();        list.add(0);        list.add(1);        list.add(2);        list.add(3);        list.add(4);                f1(x);        f2(s);        f3(list);                System.out.println("x=" + x);        System.out.println("s=" + s);        for (int i=0; i<list.size(); i++){            System.out.println("list[" + i + "]=" + list.get(i));        }            }}

Running result:

It can be seen that the parameter values of the int and String types are not changed, but the parameter values of the List type are changed.

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.