数组和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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值