[英]Passing string as a parameter in method overloading
package chapter3;
class MatchClass
{
int x=0,y=0,z=0,res=0;
float a=0,b=0,c=0,ress=0;
String str=null;`enter code here`
void add(int x,int y,int z)
{
res=x+y+z;
System.out.println("Addition of integers="+res);
}
void add(float a, float b, float c)
{
ress=a+b+c;
System.out.println("Addition of float values="+ress);
}
void add(String...str)
{
System.out.println("Concatenation of string="+str);
}
public static void main(String ar[])
{
MatchClass ob=new MatchClass();
ob.add(10,20,30);
ob.add(3.5F,4.5F,6.0F);
ob.add("Mountain","sick");
}
}
這是我編寫的代碼,我為傳遞的字符串參數獲得的輸出是非常模糊的。 有人可以幫我快速解決問題。 PS-我想使用varagrs概念傳遞字符串值。
我認為您正在談論的問題是字符串函數的輸出。 就像是 :
Concatenation of string=[Ljava.lang.String;@6d06d69c
在生產線上
System.out.println("Concatenation of string="+ str);
您正在打印變量“ str”,但“ str”不是字符串,它是一個數組,而數組是一個對象。 因此,為了進行打印,函數.toString()在“ str”上被隱式調用。 這里的問題是Object.toString()方法不會打印數組的內容,而是在內存中打印對象的類型和引用。
要打印數組的內容,將執行以下操作(至少需要JDK8)
System.out.println("Concatenation of string="+ String.join(",",str));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.