String... args 和 String[] args 的区别

本文通过两个Java示例方法,展示了如何使用字符串数组作为参数以及如何使用可变数量的字符串参数。前者适用于固定长度的字符串数组,后者则允许传入任意数量的字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码  收藏代码
  1. public static void main(String[] args) {  
  2.         callMe1(new String[] { "a""b""c" ,"d"});  
  3.         callMe2("a""b""c" ,"d");  
  4.         // You can also do this  
  5.         // callMe2(new String[] {"a", "b", "c"});  
  6.     }  
  7.   
  8.     public static void callMe1(String[] args) {  
  9.         System.out.println(args.getClass() == String[].class);  
  10.         for (String s : args) {  
  11.             System.out.println(s);  
  12.         }  
  13.     }  
  14.   
  15.     public static void callMe2(String... args) {  
  16.         System.out.println(args.getClass() == String[].class);  
  17.         for (String s : args) {  
  18.             System.out.println(s);  
  19.         }  
  20.     }  

   测试 结果:

  

Java代码  收藏代码
  1. true  
  2. a  
  3. b  
  4. c  
  5. d  
  6. true  
  7. a  
  8. b  
  9. c  
  10. d  

  

   

    通过这个小测试,应该可以看出来。

    方法一是传统的参数类型:字符串数组类型;

    方法二是可以传递一个或多个string类型的参数,不限制参数个数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值