I'm a little bit confused about this piece of code (I didn't create it : I've taken it from a tutorial):
public String findLongestName(String[] names){
int size = names.length;
String longestName = names[0];
for(int i=1; i<size; i++) {
if(names[i].length() > longestName.length()) {
longestName = names[i];
}
}
return longestName;
}
Maybe this is a very trivial question, but I'm still a beginner in Java programming language. In particular, why is "names.length" written without parentheses when storing the length into the variable "size" ? Why should we use parentheses, instead, inside of the "if" function?
In other words, why is it incorrect to write this:
int size = names.length(); instead of "int size = names.length;"
or this:
if(names[i].length > longestName.length) instead of "if(names[i].length() > longestName.length())"
?
Aucun commentaire:
Enregistrer un commentaire