Difference Between Function(String… args) and Function(String[] args) (#13473)

This commit is contained in:
Iniubong LA 2023-02-15 07:07:51 +00:00 committed by GitHub
parent cde92e09f9
commit e173c125f0
2 changed files with 43 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}