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.
How can objects be copied?
Objects can be copied in mainly two ways: by implementing the Cloneable interface and its clone() method, or by using a copy constructor (a constructor that takes an instance of the class as parameter, and assigns its attributes to a new instance).
What are shallow and deep copies?
A shallow copy only copies the reference of an object, while a deep copy is a different instance of the object with the same attributes. The clone() method implemented by the Object class only does shallow copies, so any deep copy will have to be implemented by overriding the clone() method oneself.
How is the Java Class Library structured and what are some of the main packages it offers?
The Java Class Library is divided hierarchically in sub-packages, and some of the most important ones are: java.nio (for accessing filesystems, virtual or not), java.security (for certificates and cryptography), java.util (data structure classes), javax.swing (a GUI framework that provides lightweight UI components but is not thread-safe).
How does the compiler load libraries?
Libraries must be made known to the compiler by adding them to the build path.
What are arrays?
An array is a data structure that allows storing data of the same type in a sequential way, with a fixed capacity set at initialization.
How is an array stored and accessed in memory?
An array’s elements are stored sequentially in the main memory. The memory for an array is not reserved until the size is specified, so a declaration without initialization or capacity indication would reserve no memory.
Accessing array elements at specific indexes is fast and independent of the size of the array (O(1) complexity).
What values do array elements hold, if any, before being set?
Array elements are set to the default value corresponding to their data type at initialization. An int array would consist of all zeroes at initialization, a boolean one would have false values and complex data types would be null.
What is required for importing an external package or program library?
The compiled external library is required.
What does the Java Class Library not contain a sub-package for?
Programming of websites.