Java Basics Flashcards
Learn Java Basics
What is Java?
A common Object Oriented Programming language.
What are advantages of Java?
- Write once, run anywhere
- Large developer community
- Huge amount of documentation
- Great exception handling
What does a Java file compile into?
bytecode
What is the difference between JDK, JRE, 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.
State vs Behavior
- States are properties of an object.
- Behavior are actions an object can take
What is a class?
A class is the blueprint that provides the states and behaviors its instantiated objects will have.
What is an object?
An object is an instantiation of a class.
What are the class members?
- Instance variables
- Methods
- Constructors
- Inner class
- static/instance blocks
What are the types of variables?
- Instance variable
- Local variable
- Static variable
What are the variable scopes?
- Class - every object shares the class variable
- Instance - every object has their own variable
- Method - inside method & passed in as parameters
- Block - inside for or if statements
What are the access modifiers and what access do they allow?
- 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 non-access modifiers and what do they do?
- 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 won’t be sent at all and will appear as null on the other end
What is a constructor?
A method within a class that tells an instantiated object what its default values and methods will be. Can have parameters passed into it.
What are the types of constructors?
- Default
- No args
- Args
When do you get a default constructor?
If you don’t explicitly create your own constructor for the class.
What are the 4 pillars of OOP?
A PIE - Abstraction, Polymorphism, Inheritance, Encapsulation
What is Abstraction?
Its main goal is to handle complexity by hiding unnecessary details from the user.
What is the difference between an abstract class and an interface?
- Typically, a programmer would use an abstract class to denote a kind of hierarchy between the abstract class and the classes that extend it. An interface is just implemented, so the interface can be used across a variety of classes that may not be related at all.
- 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 aren’t 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?
Objects of different types can be accessed through the same interface, each type can provide its own, independent implementation of this interface
What are the types of polymorphism?
- Compile time(static) using method overloading
- Runtime(dynamic) using method overriding
What is inheritance?
A new class is derived from an existing class by acquiring the properties and methods of other classes.
How do you inherit?
extend a class
What is encapsulation?
The mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
What is a POJO?
A plain old java object
What is a bean?
A bean is like a pojo but it must have a no args constructor, must have private instance variables, must have getters and setters
What is the stack?
Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Stack memory is always referenced in LIFO (Last-In-First-Out) order.
What is the heap?
The heap is the runtime data area from which memory for all class instances and arrays is allocated. The heap is created on virtual machine start-up.
What is the difference between final, finally, and finalize?
- Final is a non-access modifier that marks your variable as the value that it is initialized at forever, so a constant. Can also be applied to a class to make it so that no one can extend that class or a method to prevent subclasses from overriding that method.
- 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. The main purpose of using finalize is to release resources used by objects before they’re removed from the memory.