Basics Flashcards
What is Java?
Java is a high level, object oriented programming language. Java is considered highly robust, secure, simple, and high performing
- What is the JRE?
The JRE is the Java Runtime Environment, which is part of the JDK and is responsible for running a java program. It contains the class libraries and other resources a specific java program needs to run. Contains the JVM and is inside the JDK
What is the JDK?
The Java Development Kit, it contains the compiler and developer tools needed to develop a java application. Contains the JRE and JVM
What is the JVM?
JVM stands for JVM, it is designed to execute the bytecode in .clss files which runs the java application. Lives inside the JDE and JDK
What is the difference between an object and a class?
An object is an instance of a java class, it has its own identity, behavior, and state whereas a class is a user designed blueprint by which an object is created. It has a set of properties or methods that define its specifications
What is the root class from which every class extends?
The object class is the root of the class hierarchy
What are the primitive data types in Java?
boolean, char, double, float, long, int, short, byte
Where are Strings stored?
String is a class and strings are treated as objects, so they’re located in the heap. Specifically in the String Constant Pool
Explain Stack vs Heap
Stack is used to store method invocations and local variables, works based on Last in First Out; Heap is where all objects are stored, created on startup
Are variable references stored on the stack or heap? What about the objects they refer to?
Variable references can be stored in the stack, all java objects are stored within the heap
What is a stack frame and when are they created?
A stack frame contains the state of one java method invocation. When a function is called, a stack frame is created in the stack segment
What are annotations?
They are used to provide supplementary information about a program, they do not change the actions of a program, but they help associate metadata to the program elements (@Override)
What is a POJO vs a Bean?
- A POJO is a Plain Old Java Object, it is a java object that does not have any special restrictions and is used for increasing readability and reusability in a program
- A Bean is a special POJO with restrictions such as private fields that require getters and setters
Can you force garbage collection in Java? When is an object eligible for garbage collection?
By either setting a variable to null, redirecting a variable’s reference, or calling the finalize() method in the object class.
- Objects that no longer have a variable reference are eligible for garbage collection
Why are strings immutable in java? How would you make your own object immutable?
Strings are immutable because string objects are cached in the string pool and since all the strings in the pool are shared between multiple clients, there is a risk that a change would effect other clients
- In order to make an object immutable, you would need to add the final keyword to ensure there are no changes
What is the difference between String, StringBuilder, and StringBuffer?
- A string is a n object representation of char values which is immutable, internally, strings are a special type of char array
- StringBuilder and StringBuffer both create mutable strings objects, but the difference between the two is that StringBuffer is thread safe, whereas StringBuilder is not
What are the different available scopes in Java?
- Local - Declared inside the body of the method and not shared with any other methods
- Instance - Declared inside the class, but outside the methods. This is shared among each method but not among each instance
- Static - Declared when the static keyword is used and can be shared among all classes
What are some access modifiers in Java? Explain them
- Private - We can only access private members within the same class
- Public - We can access public members from anywhere
- Protected - We can access the protected members within the same package and within child classes.
- Default - We can access these members only within the same package. If no access modifier is given, they are set to default
What are the non-access modifiers in Java?
- Static - Used to create variables/methods that will exist independently of any instances of the class
- Final - Used to declare that the variable/method with this keyword cannot be changed or overriden
- Abstract - Used to declare that a class is abstract and allows a method to be declared without implementation
- Synchronization - Used to indicate that a method can only be accessed by one thread at a time
What is the difference between static and final variables?
Static is used to define that a class member can be used independently of any other object in the class
Final is used to declare that the class member cannot be overridden
What are the default values for all data types in Java?
char - \u000 or null double - 0.0d float - 0.0f long - 0 int - 0 short - 0 byte - 0 boolean - false
What is a wrapper class? List them
The wrapper class in java provides the mechanism to convert primitive data types to objects and vice versa
Boolean, Character, Byte, Short, Integer, Long, Float, Double
What is Autoboxing/Unboxing?
Autoboxing is the automatic conversion of primitive data types into its corresponding object wrapper class, whereas Unboxing is the reverse
Is Java pass-by-value or pass-by-reference?
Java is pass-by-value; confusingly, a reference in java is a reference to a value, so all arguments are passed by value
What makes a class immutable?
The final keyword when declaring a class makes it immutable which prevents it from being extended by another class
If two objects are equal, do they have the same hashcode? What about if they’re not equal?
If two objects are equal, they must have the same hashcode, but just because they have the same hashcode, that does not mean they are equal
What data types are supported in switch statements?
int, byte, short, char
List all non-access modifiers
static, final, abstract, synchronized, transient, volatile, strictfp
How do you pass multiple values with a single parameter into a method
You would need to pass all your values into an array structure or collection and pass an array or collection as the parameter
What is a static block?
A way to statically initialize a class, a static block is a set of instructions that is run only once when a class is loaded into memory
What are static imports?
Static import allows us to statically import a class, allowing us to access a method of a class directly without having to a class name
What methods are available in the object class?
clone() equals() finalize() hashCode() toString()
What is the difference between == and .equals()?
The == operator compares the reference or memory location, not the value of the objects being compared, whereas the .equals() method compares the values of the objects
What is an enhanced for loop and what is a forEach() loop?
An enhanced for loop is a simpler way of iterating through all the elements of a collection or array. The forEach() method however can be used on classes that extend the iterable interface
What are the 3 usages of the “super” keyword?
- Used with variables in order to reference data members that have the same name in both the parent and child class, super means you’re referring to the parent class
- You can use it to invoke the parent class methods
- You can use it to invoke the parent class constructors
What is the first line of any constructor?
The first line must be a call on another constructor in the same class using “this” or a call on the superclass constructor using “super”
How would you perform constructor chaining?
By using "this" keyword for chaining constructors in the same class By using "super" keyword for chaining constructors from the parent class
What happens if you don’t define a constructor for a class?
A default constructor will be applied by java