java Flashcards
What are the main methods of iterator?
next, hasNext and remove
is Iterator an interface?
yes
What is a Stream?
Sequence of elements supporting sequential and
parallel aggregate operations.
How to implement equals correctly?
- create a protected boolean eq(object o).
- 1 check if o is instance of class
- 2 check condition
- check if both objects agree on eq.
example: class Point { protected boolean eq(Object o) { if (!(o instanceof Point)) return false; return ((Point)o).x == x && ((Point)o).y == y; } public boolean equals(Object o) { return (this.eq(o) && ((Point)o).eq(this)); } }
If you override equals what other method should you override?
hashCode
What does compareTo?
compares to objects and returns an int indicating which one is bigger.
if return > 0 then this is bigger
otherwise parameter is bigger.
What’s the use of Comparator?
comparing objects that doesn’t implement Comparable interface (don’t have compareTo method).
What’s a tagging interface?
an interface without methods, used for specifying a purpose.
Why not make all classes Cloneable?
because then we can’t have singletons.
What are the pitfalls of clone?
Don't use constructors inside clone. because the dynamic type might be different from static type. for example field A a, actually holds an object of type B. therefore if we copy a using new A(); we will get wrong behaviour.
What are the 4 access modifiers in Java?
- public - everyone
- protected - only inheriting classes and same package
- default - same package
- private - same (toplevel) class. (if nested classes are involved).
What are static nested classes?
defined as member classes, can only access static members.
What are inner classes?
non-static nested classes. there are 3 types:
- non static member classes, regular
- local class
- anonymous class
What are local classes?
classes defined in a method.
can access parameters as well as local (final!) vars declared in the same method before the class
What are anonymous classes?
created inside a method.
definition is also invocation.