Intermediate Flashcards
What are arrays in java?
An array is an object containing a fixed number of values of the same type
What are varargs?
- Varargs are used to pass an arbitrary number of arguments to a method but we can also pass an array directly as the argument
- Each method can only have one varargs parameter and the varargs parameter needs to be the last argument
What is the disadvantage of varargs?
They can lead to Heap Pollution
What does the static keyword mean?
The static keyword means that the particular member belongs to a type itself, rather than to an instance of that type
What is important about static fields (class variables)?
A SINGLE copy of that field is created and shared among all instances of that class, this static variable is stored in the heap memory
When to use static fields?
- When the value of the variable is independent of objects
- When the value is supposed to be shared across all objects
What is important about static methods?
- Static methods belong to a class not an object and cannot be overwritten
- Static methods cannot use this or super keywords
When to use static methods?
Static methods are used to access or manipulate static variables and other static methods that do not depend upon objects; static methods are widely used in utility and helper classes
What is the benefit of static classes?
A static class allows to create a class within a class and provides a way of grouping elements that are only used in one place -> keeps the code more organized and readable
In what two types can nested class architecture be divided?
- Nested classes that are declared as static are called static nested classes
- Nested classes that are non-static are called inner classes
What is the difference between inner classes and static nested classes?
Inner classes have access to all members of the enclosing class, whereas static nested classes only have access to static members of the outer class
Why to use static nested classes?
- Grouping classes that will be used only in one place increases encapsulation
- Code is closer to the place where it will be used -> Increases readability, and maintainability
What are enums?
- The keyword enum denotes a special type of class that always extends the java.lang.Enum class
- Enum is short for enumerations
- Basic example for enum is days of the week
- Enum declaration looks similar to a class declaration
- Enum entries are typically written in uppercase
- Enums can have fields and a constructor
What are implementation design patterns using enums?
- Singleton Pattern
- Strategy Pattern
What does the final keyword mean?
- The final keyword allows to set limitations on extensibility -> classes marked as final cannot be extended
- The final keyword in a class declaration does not mean that the objects of this class are immutable, we just cannot extend it
- Methods marked as final cannot be overwritten
- Variables marked as final cannot be reassigned -> once a final variable is initialized, it cannot be changed
- A final reference variable cannot be reassigned but that does not mean that the object it refers to is immutable -> the properties of this object can be changed freely
What is the aim of generics?
The aim of generics is to reduce bugs and add an extra layer of abstraction over types
When are generics useful?
Generics are useful if you would run into a code duplication like e.g. an IntegerPrinter class or a StringPrinter class where all classes would hold the exact same code only the types of the variables would be different -> What we want is one class that is flexible for many different types
What are generic wildcards?
Todo is to print a list containing any type of thing -> Use a ? as a type parameter in the angle brackets
What is one restriction of generics in java?
One restriction is that the type parameter cannot be a primitive type, because generics are a compile-time feature, meaning the type parameter is erased and all generic types are implemented as type object
How to read and write user input in java?
- Reading from system.in
- Writing to system.out
- Using the console class for input and output