Data Structures and Java Class Library Flashcards
What is an annotation and what rules should be followed when using them?
Code annotations are metadata about code that can be evaluated by the compiler and later at runtime. They must start with an @ sign and a capital letter.
What are code conventions and what is their use?
Code conventions are a set of style rules (such as variable naming or source code formatting) that helps programmers write code that is easily understandable by other programmers, or by themselves in the future. These rules do not affect the functionality of the software and they are not checked by the compiler.
What rules must be followed when naming each of the following: packages, classes, interfaces and methods?
- A package’s name must be in lowercase letters.
- Class and interface names must be nouns and start with a capital letter, and if they consist of more than word, the subsequent words must be capitalized too (this is a variant of camel case).
- Methods must start with a lowercase letter and they should consist of verbs. Words after the first one should start with a capital letter (camel case).
Should required variables be initialized when declaring them? Where must required variables go within a code block?
True. They must go at the beginning of the block.
What is the function of blank lines in source code formatting?
They are used to separate groups of semantically related code statements.
What are bracketed comments?
They are multi-line comments and can be used by prepending the symbols /* to a block of code, and appending a */ at the end. The block of code within those two pairs of symbols is considered a comment and will not be executed.
What is Javadoc?
Javadoc is a documentation generator that relies on a special bracketed comments called Javadoc comments, put before classes and methods. They are like regular comments but they start with the symbols /**, end with **/, and have certain tags to mark important details about the documented class or method, for example: returns, throws.
In which order are the elements of a class typically arranged?
Static variables and methods, instance variables, constructors, getters and setters, instance methods.
Why is overriding the method for string representation of objects useful?
It is useful to centralize the textual representation of a class.
What’s the output of toString() if it’s not overridden?
It outputs the name of the class, followed by the hash code of the object (e.g., Customer@1c8697ce).
When should == and .equals() be used?
== is for comparing primitive data types, while .equals() is for comparing complex types.
What are some of the steps taken in a standard implementation of .equals()?
- Check if the comparison using == already returns true (in that case, the object references are the same)-
- Check if the given object is of the same type as this class, if it is, compare the relevant attributes, if it’s not, call the .equals() method of the superclass
What are the properties of a correctly implemented equals()?
- Reflexive: a.equals(a) always returns true
- Symmetric: a.equals(b) returns the same value as b.equals(a)
- Transitive: if a.equals(b) and a.equals(c) is true, then b.equals(c) must also be true
- Consistent: it must return the same value as long as relevant attributes remain unchanged
- Existent: a.equals(null) must return false
What requirements should a self-implemented hashCode() meet?
STABILITY-EQUALS-WIDE-IDENTITY
- Stability/consistency: two objects with identical content should generate the same hashCode.
- The calculation must use the same attributes as equals().
- The hash code should be as widely spread as possible. Only a few different assignments should generate the same hash code.
- Only identity-related attributes should be included.
What is the Comparable interface and what is its main method for?
Any class can implement the Comparable interface and its method compareTo() in order to define order between instances of the class.