数组和String调用方法时,值是否改变(包含可变参数)

package com.wh.encapsulation;

import java.util.Arrays;

/**
 * @author 王恒
 * @datetime 2017年4月5日 上午9:33:39
 * @description 
 * 类和对象 
 *   理解面向对象的概念 
 *   类的定义
 *   声明类、属性、方法 
 *   创建、使用对象 
 *   对象的生命周期 
 *   匿名对象 
 * 信息的封装和隐藏 
 * 构造方法
 * 方法的重载
 */
public class Demo {

	public static void main(String[] args) {
		fun(1,2,3,4,5,6);
		int[] arr={1,2,3};
		arr(arr);
		System.out.println(Arrays.toString(arr));//数组值改变了,是引用传递(值会发生改变)
		
		String str="test and good";
		str(str);
		System.out.println(str); //String值未改变,和基本数据类型的参数传递一样,属于值传递(值不会发生改变)
	}

	public static void str(String s){
		s="hello world";
	}
	
	public static void arr(int[] arr){
		arr[0]=10;
	}
	
	
	
	public static void fun(int...x){
		System.out.print("可变参数:"); 
		for (int i : x) {
			System.out.print(i+" ");
		}
		System.out.println("");
	}
}


运行结果:

        可变参数:1 2 3 4 5 6 
        [10, 2, 3]
        test and good
            

  

原文地址:https://www.cnblogs.com/1020182600HENG/p/6667556.html