Ho provato questo e ottenere comportamenti strani da JAVA, qualcuno può spiegarlo per me?Comportamento di parametro (varargs) di Java 3 punti quando non viene passato alcun argomento o null
boolean testNull(String... string) {
if(string == null) {
return true;
} else {
System.out.println(string.getClass());
return false;
}
}
boolean callTestNull(String s) {
return testNull(s);
}
Poi ho test case:
@Test
public void test_cases() {
assertTrue(instance.testNull(null)); // NULL
assertFalse(instance.testNull()); // NOT NULL
assertFalse(instance.callTestNull(null)); // NOT NULL
}
La domanda è se chiamo testNull()
direttamente con il parametro null
, mi metterò true
indietro, ma se chiamata callTestNull()
con null
, che chiama testNull()
, è mi dice che il parametro non è nullo, ma array vuoto.
Come fai a sapere che l'array è vuoto? –
chiamato anche ellissi –
intendevo in termini di terminologia Java. Ma sì, il personaggio "..." è effettivamente chiamato ellissi. Maggiori informazioni sulla programmazione usate qui https: //en.wikipedia.org/wiki/Ellipsis_ (programming_operator) – eldris