java Flashcards
JDK vs JRE vs JVM
- Java Development Kit contains compiler and debugger tools to compile the Java source code, also includes Java Runtime Environment and Java Virtual Machine
- The JRE provides an environment and in that environment the JVM runs the compiled byte code. It contains both the library and the JVM.
- The JVM creates a layer of abstraction from the Java Code and the computer environment. It is used to convert the java bytecode into machine readable code.
Class vs Object
- An object is an instantiation of a class
- The class is the blueprint that provides the states and behaviors its instantiated objects will have
What are the class members? (JAVA)
- Instance variables
- Methods
- Constructors
- Inner class
- static/instance blocks
What are the types of variables? (JAVA)
- Instance variable
- Local variable
- Static variable
What are the scopes of the variables? (JAVA)
- Class - inside class
- Method - inside method
- Block - inside for or if statements
What are the Access Modifiers? (JAVA)
- Public - globally accessible
- Protected - accessible within the package and its subclasses
- Default - accessible only within the package
- Private - accessible only within that class
What are the Non-Access Modifiers? (JAVA)
- Static - variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves
- Final - define an entity that can only be assigned once. Once a final variable has been assigned, it always contains the same value.
- Abstract - purpose of extending
- Synchronized - used for threads; accessed by only one thread at a time
- Volatile - used for threads; lets JVM know that a thread accessing a variable must merge its own private copy with the master copy in memory
- Transient - used in serialization; field that is transient will not be sent at all and will appear as null on the other end
What is a Constructor? (JAVA)
A method that creates an instance of a class based on the parameters of the constructor
What are the types of Constructors? (JAVA)
- Default (created by the JVM for you if no constructor is specified)
- No args
- Other
What are the 4 pillars of Object Oriented Programming?
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
. What is Abstraction? How to achieve it? (JAVA)
- Abstraction is focused on the concept of reusability which is achieved using generalization or specialization
ACHIEVE WITH:
- Abstract class
- Using a framework, such as Spring
Abstract Class vs Interface (JAVA)
- Can only extend one abstract class
- Can implement as many interfaces as you would like
- If a programmer wants to have method definitions or contain variables that are not static and final, they would use an abstract class. The methods in an abstract class can also be overridden, while in an interface, all the methods must be implemented by the class that implements the interface
What is Polymorphism? How to achieve it?
- Polymorphism is the concept that in Java, objects can have the same states and behaviors, so they are interchangeable
ACHIEVE WITH:
- Method overriding
- Method overloading
What is Inheritance? How to achieve?
- Inheritance is the concept that classes can share states and behaviors through extension.
- Achieve by extending a class
What is Encapsulation? How to achieve?
- Encapsulation is a technique to control an object’s accessibility to its states. You can achieve this control using access modifiers. The access modifiers are public, protected, default, and private.
What is a hashcode? (JAVA)
- A hashcode is an identifier for an object that has been converted to an integer.
Final vs Finally vs Finalize (JAVA)
- Final is a non-access modifier that marks your variable as the value that it is initialized
- Finally refers to the finally clause in a try, catch, finally block. Finally will always
execute, even if there is an exception. - Finalize - a method that the garbage collector invokes when the JVM figures out
that a particular instance should be garbage collected.
What does the keyword “Static” mean? (JAVA)
- Static is a non-access modifier that means you can use that method or variable from a class without instantiating that class.
What is an Exception? What class does it extend?
- Exception extends Throwable, which has Exception and Errors as subclasses and RuntimeException, which is a subclass of Exception.
String vs StringBuilder vs StringBuffer
- String - Immutable (cannot be changed or extended); stored in string pool; thread
safe - StringBuilder - Mutable; for strings changing rapidly; do not extend string because string is final
- StringBuffer - Mutable; synchronized, much slower; do not extend string because string is final; thread safe, useful for threads/strings being manipulated by multiple
threads