1. What are records in Java?
Answer:
Explanation:
Records in Java are a special type of class introduced to hold immutable data in a concise manner.
2. In which version of Java were records introduced?
Answer:
Explanation:
Records were introduced as a preview feature in Java 14 and became standard in Java 16.
3. How do you define a record in Java?
Answer:
Explanation:
A record is defined using the record keyword followed by the record name and a list of components.
4. Are record components final in Java?
Answer:
Explanation:
Record components are implicitly final, meaning their values cannot be changed once set.
5. Can records in Java have additional methods?
Answer:
Explanation:
Records can have additional methods besides the implicitly declared ones.
6. How are equals() and hashCode() methods handled in Java records?
Answer:
Explanation:
The equals() and hashCode() methods are automatically and appropriately implemented by the compiler for records.
7. Can records implement interfaces in Java?
Answer:
Explanation:
Records can implement interfaces, just like regular classes.
8. Can a record in Java extend another class?
Answer:
Explanation:
Records cannot extend any other class, although they implicitly extend java.lang.Record.
9. Which access level are record components in Java?
Answer:
Explanation:
Record components are implicitly public, and they also implicitly create corresponding public accessor methods.
10. What is the purpose of the canonical constructor in a record?
Answer:
Explanation:
The canonical constructor in a record is used to initialize its components.
11. Can records in Java be mutable?
Answer:
Explanation:
Records are designed to be immutable data carriers, so their components cannot be changed after construction.
12. How are record components accessed in Java?
Answer:
Explanation:
Record components can be accessed directly by their names or through automatically generated public accessor methods.
13. Can records be serialized in Java?
Answer:
Explanation:
Records can be serialized like regular classes if they implement the Serializable interface.
14. What is the primary use case for records in Java?
Answer:
Explanation:
The primary use case for records is to act as simple, concise data carriers with minimal boilerplate.
15. How do you create a custom toString() method for a record in Java?
Answer:
Explanation:
The toString() method can be overridden in a record to provide a custom string representation.
16. Are constructors mandatory in record declarations?
Answer:
Explanation:
Constructors are not mandatory in records. If not provided, a public canonical constructor is automatically generated.
17. Can records be generic in Java?
Answer:
Explanation:
Records can be generic, allowing type parameters to be used in their declaration.
18. How do you destructure a record in Java?
Answer:
Explanation:
While Java does not have a built-in destructuring feature, individual components of a record can be accessed directly.
19. Can records be annotated in Java?
Answer:
Explanation:
Records can be annotated just like classes and methods.
20. How does the record's equals() method compare objects?
Answer:
Explanation:
The automatically generated equals() method in a record compares objects by comparing all the record's components for equality.