Java Flashcards
What are the concepts of OOP, describe them all. What are the related Java concepts
Encapsulation - Isolate some logic from the outside world, maintain functionality through an internal implementation. For example, a you have a method that finds a value based on an id. There is a private instance variable that is a list, the method implementation searches the list. It is possible to change the implementation to use a hash map without breaking the API
Abstraction - See above
Inheritance - The ability to extend from another class, keeping the features/functionality from your parent but adding your own.
Polymorphism - Means many forms, refers to the idea that an object can be defined as a number of different things, a Square can be a Shape, which is itself an object.
What is method overriding. Compare and contrast with overloading.
Override - When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class
Overload - When a method of the same name as another method within the class (or its hierarchy) has a “different” set of parameters (be they vary by type, number, or both). A method cannot overload another by simply returning a different type.
final, finally, finalize keywords
final:
- Marks a class as being unable to be extended
- Marks a method as being unable to be overridden
- Marks a variable as being unable to change. (must be initialized)
finally:
- Used in try/catch statements to execute some logic after the try/catch block regardless of its outcome.
finalize:
- A method you can define to perform cleanup on an object, which is invoked when the object is garbage collected. It is a method defined on java.lang.Object, non-static
Describe some of the significant differences in Java versions
Java 0 - 7 Steadily added important features
- JDBC
- Swing
- Collections
- Web Start
- Generics (5)
- Enum
- for each
Java 8, Major release with LTS, many companies are still using
- Lambdas and general functional programming language support
- Better date/time api
Java 9
- Java Modules
- JShell
Java 11 (LTS)
- HTTP Client
- Licensing change (requires switching to OpenJDK for most)
Java 14
- Improved switch statements (with expressions)
Java 16
- Records (love)
Java 17 (LTS)
- Sealed classes
What is Java, what is JRE, vs JVM vs JDK
Java is not compiled into a platform-specific machine; instead, it is compiled into platform-independent bytecode. This code is interpreted by the Virtual Machine on which the platform runs.
JDK - development kit which includes JRE, Java, compiler and some other tools for creating java applications
JRE - Is to execute a java program, this means JVM, and core classes
What is the JIT compiler
Just in time compiler, compiles to platform specific bytecode at runtime which speeds things up
String vs StringBuilder vs StringBuffer
String - Each string is stored in memory after initialized, every new string is “saved”
StringBuilder - Avoids the above. new StringBuilder(“hello”).append().toString();
StringBuffer - Same as above except it is thread safe (with a performance hit)
Give a brief overview of inner classes, how do they work and what is their visibility. What are they used and how do you use them.
What is a vector
Basically an array list
Read from a file line by line
String file =”src/test/resources/fileTest.txt”;
BufferedReader reader = new BufferedReader(new FileReader(file)); String currentLine = reader.readLine(); reader.close();
Heap vs Stack memory
Stack memory is the amount of memory allocated to each individual program.
Heap memory - not assigned but willl be available for use by the Java code when it is required, which is generally during the program’s runtime.
What is JPA and Hibernate, compare the 2.
JPA - Jakarta (or Java) Persistence API, is a standard for handling the persistence of java objects (essentially an ORM feature). Standards for determining how to map a java object into a persisted location (usually a database). Contains descriptors for how to handle data types, relationships, and CRUD operations. Developers interact with this using annotations.
Hibernate - A Java ORM solution which actually predates JPA. As a result, Hibernate can solve most of the problems as JPA specifies without using JPA at all. However, most now would use hibernate as a JPA implementation. That is useful since our code can comply with a widely accepted standard while using hibernate (a fully-fledged stable solution) as the underlying implementation. Hibernate is specific to relational databases, but that’s not necessarily true of JPA and there are implementation of JPA that communicate with NoSQL databases.