Difference Between Function(String… args) and Function(String[] args) (#13473)
This commit is contained in:
parent
cde92e09f9
commit
e173c125f0
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.argsVsvarargs;
|
||||
|
||||
public class StringArrayAndVarargs {
|
||||
public static void capitalizeNames(String[] args) {
|
||||
for(int i = 0; i < args.length; i++){
|
||||
args[i] = args[i].toUpperCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String[] firstLetterOfWords(String... args) {
|
||||
String[] firstLetter = new String[args.length];
|
||||
for(int i = 0; i < args.length; i++){
|
||||
firstLetter[i] = String.valueOf(args[i].charAt(0));
|
||||
}
|
||||
return firstLetter;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.argsVsvarargs;
|
||||
|
||||
import static com.baeldung.argsVsvarargs.StringArrayAndVarargs.capitalizeNames;
|
||||
import static com.baeldung.argsVsvarargs.StringArrayAndVarargs.firstLetterOfWords;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class StringArrayAndVarargsUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCheckingArgumentClassName_thenNameShouldBeStringArray() {
|
||||
String[] names = {"john", "ade", "kofi", "imo"};
|
||||
assertNotNull(names);
|
||||
assertEquals("java.lang.String[]", names.getClass().getTypeName());
|
||||
capitalizeNames(names);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCheckingReturnedObjectClass_thenClassShouldBeStringArray() {
|
||||
assertEquals(String[].class, firstLetterOfWords("football", "basketball", "volleyball").getClass());
|
||||
assertEquals(3, firstLetterOfWords("football", "basketball", "volleyball").length);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue