I have this code snippet
import java.util.ArrayList;
import java.util.List;
public class AssertTest {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
assert(list.add("test")); //<-- adds an element
System.out.println(list.size());
}
}
Output:
0
Why is the output list empty? How does assert behave here? Thank you in advance!
add()returns a boolean. something I didn't know and I still don't want to know. such return type is just silly. if a subtype ofCollectionneeds to provide such function, the subtype can add a new method.add()returns a boolean might be useful when usingHashSet<?>. If you try to add a value having the same hash code as one contained in the set, theadd()method will return false. You can also think ofCollection<?>s allowing onlynvalues. So the return value is fully justified.