Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Using Varargs in overloaded methods

Java Examples - Using Varargs in Overloaded Methods

Java Examples Java Examples

The following example demonstrates how to use variable arguments in overloaded methods:

Main.java File

public

class

Main

{

static

void

vaTest

(

int

...

no

)

{

System

.

out

.

print

(

"

vaTest(int ...):

"

"

Number of parameters:

"

no

.

length

"

Content:

"

)

;

for

(

int

n

:

no

)

System

.

out

.

print

(

n

"

"

)

;

System

.

out

.

println

(

)

;

}

static

void

vaTest

(

boolean

...

bl

)

{

System

.

out

.

print

(

"

vaTest(boolean ...)

"

"

Number of parameters:

"

bl

.

length

"

Content:

"

)

;

for

(

boolean

b

:

bl

)

System

.

out

.

print

(

b

"

"

)

;

System

.

out

.

println

(

)

;

}

static

void

vaTest

(

String

msg

,

int

...

no

)

{

System

.

out

.

print

(

"

vaTest(String, int ...):

"

msg

"

Number of parameters:

"

no

.

length

"

Content:

"

)

;

for

(

int

n

:

no

)

System

.

out

.

print

(

n

"

"

)

;

System

.

out

.

println

(

)

;

}

public

static

void

main

(

String

args

[

]

)

{

vaTest

(

1

,

2

,

3

)

;

vaTest

(

"

Test:

"

,

10

,

20

)

;

vaTest

(

true

,

false

,

false

)

;

}

}

The output of the above code is:



vaTest(int ...): 参数个数: 3 内容: 1 2 3 

vaTest(String, int ...): 测试: 参数个数: 2 内容: 10 20 

vaTest(boolean ...) 参数个数: 3 内容: true false false 

Java Examples Java Examples