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?
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?
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:
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.
anonymous class syntax
new () {
}
examle:
interface A {
void g();
}
class B {
void f() {
A a = new A() {
@Override public void g() {}
};
a.g();
}
}Is it possible to inherit from an inner class?
yes. as long as the inheriting class can be associated with a subtype of the outer class.
What are the advantages of java enums over pre c++11 enums?
Can an enum type inherit?
No. all enum types implicitly inherit from Class Enum.
Can implement interfaces.
Can you extend enum classes?
No. enums are implicitly final.
What’s the best way to implement a singleton in Java?
Enums.
public enum MySingleton {
INSTANCE;
advantage:
no need for lazy instantiation.
Do annotations affect the code?
mo. but can effect things that use the code: tools, code generation etc..
What is the annotation @FunctionalInterface?
asserts that the tagged interface has one and only one
method that is without implementation. So we can be
sure that it can be replaced with lambda.
What are the 3 types of annotations?
Marker annotations
– Have no attributes
– @Override, @Deprecated
Single value annotations
– Provide a single piece of data.
– Attribute type can be
primitive, string, Enum, or array of the previous.
• @Author(“Pazit”)
• @SuppressWarnings({“unchecked”,
“deprecation”})Multi valued annotations
– @MethodKind(composite=true, mutator=false)