java为什么在非主方法这样写只会打印地址,而主方法却打印值

public class KeBian {
    public static void main(String[] args) {
        String a = "123456789";
        show(a);
        System.out.println(a);
    }

    public static void show(String ... str1) {
        
        System.out.println(str1);
        
    }
}

为什么一个打印地址,一个打印值

而把

System.out.println(str1);

改为

System.out.println(Arrays.toString(str1));

又可以了呢?

main方法中你是直接打印的a这个字符串

show方法 String ...str1 表示你是用数组接收的  直接打印出来的是对象,遍历打印就行了   就可以正常显示

你把String ... str1改成 String str1;String ... str1是接收字符串或者数组的,按照数组解析了;你main中a是字符串

搞清楚String ... str1等价于String[] str1,数组打印的是地址。

可变参数底层是用数组接收的,你直接打印参数名就是和直接输出数组一样肯定是地址值啦