Java Flashcards
What is Java? / Explain some features of Java
Java is an OOP language that supports different programming paradigms such as functional programming(using only functions to complete tasks). It’s is a portable and independent platform due to the JVM.
What is JRE / JDK / JVM?
JDK(Java Development Kit)
Provides an environment to develop and execute a Java Program
Contains the compiler, tools for development
Contains the JRE and JVM
JRE(Java Runtime Environment)
All that’s needed to RUN an application
Contains the JVM
Contains the core libraries for Java
JVM(Java Virtual Machine)
Takes the COMPILED code (bytecode) and executes it from the main method
What is the difference between an object and a class?
A Class is a blueprint for an object that define variables and methods. Where as Objects are representations of real life objects that can be interacted with in Java.
What is the root class from which every class extends?
Object class, it contains the toString(), hashcode(), and .equals()
What are the primitive data types in Java?
Primitives - are stored in the stack: Int Long Double Short Byte Boolean Char
Where are Strings stored?
Strings in Java are arrays of characters that are immutable. Strings are stored in the string pool within the heap that allow you to reuse the same String object for repeated instances. You can make it so a string isn’t in the string pool by declaring a string using the new keyword.
Explain stack vs heap
Stack:
- Data Structure that follows a LIFO pattern (Last In First Out) which is naturally managed
- Holds primitives and reference variables
- Once the scope of the method is over, the variable “pop” off
Heap:
- Object storage
- String Pool for string literals
- Managed by the Garbage collector
Are variable references stored on the stack or heap? What about the objects they refer to?
Reference variables are stored on the stack but the objects they refer to reside in the heap
What are annotations?
A form metadata that can be added to Java code that provide additional information about the program
Why are strings immutable in java? How would you make your own objects immutable?
Strings are immutabile in Java because caching, security, easy reuse without replication. You can make objects immutable by Declaring class final, Make fields private, or not use setters/getters.
What is the difference between String, StringBuilder, and StringBuffer?
String:
- arrays of characters that are stored in the string pool within the heap
- immutable: the value of strings cannot be changed
StringBuffer:
- Mutable, alternatives that allow for manipulation
- Thread safe, slower
StringBuilder:
- Mutable, alternatives that allow for manipulation
- Not thread safe, faster
What are the different variable scopes in Java?
Variable scope refers to the lifetime of a variable some types of variable scopes are:
- Class Level
- Method Level
- Block Level
- Instance Level - lifetime of an obj
What are the access modifiers in Java? Explain them.
public - least restrictive, all of the classes within the application have access
protected - all of the classes within the same package and all of the
subclasses/children have access
default - all of the classes within the same package have access, absence of an access modifier
private - only the class itself has access
What are the non-access modifiers in Java?
abstract - that cannot be used to create objects (to access it, it must be inherited from another class static - static members do not require an instance and are initialized when the class is loaded into memory
default
- allows to provide a default implementation to an interface
final
- prevents the class from being extended, cannot be inherited
- for variables cannot be Reassigned once initialized
- for methods cannot be overridden by a child class
What is the difference between static and final variables?
Static is used to define a class member that can be used independently of any object of the class
Final is used to declare a constant variable or method that cannot be overridden
What are the default values for all data types in Java?
Boolean - false Char - \u0000 byte - 0 short - 0 Int - 0 long - 0L float - 0.0f double - 0,0d
What makes a class immutable?
Do not write setter functions Include all-argument constructor Make a class final
What data types are supported in switch statements?
Data types supported: Byte Short Char Int String
How to pass multiple values with a single parameter into a method?
Creating an array with multiple values and pass that into your method
Var args: allows methods to support an arbitrary number of parameters of one type (must be at the end of the argument list)
What methods are available in the Object class?
toString(): Returns the string representation of this object
equals(): Compares the given object to this object
hashcode(): Returns the hashcode number for this object
What is the difference between == and .equals()?
“==” checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects
What is an enhanced for loop?
a simpler way to iterate through all the elements of a Collection
for (int i = 0; i < myArray.length; i++) {
to:
for (int myValue : myArray)
What are the 3 usages of the “super” keyword?
super keyword in java is used to refer parent class objects
- Use of super with variables:
- Use of super with methods: This is used when we want to call parent class method
- Use of super with constructors: super keyword can also be used to access the parent class constructor.
What is the first line of any constructor?
The first line in any constructor is a call to the parent’s constructor: super();
How would you perform constructor chaining?
Can be done using the this(). To chain constructors you utilize the same signature or argument structure as the methods you are trying to chain.
What happens if you don’t define a constructor for a class? Can you still instantiate it?
the compiler creates a default constructor(with no arguments) for the class and yes you can instantiate it.
What are the four pillars of OOP
Abstraction
Encapsulation
Inheritance
Polymorphism
What is Abstraction
Abstraction
Process of only showing the essential/necessary features of an entity/object to the outside world and hiding irrelevant information. And having users use an interface to interact with your program.
Let’s say you have an abstract class called Animal and inside of it you have an abstract method called animal sound. Since you cannot create a new object of the Animal class, you can extend those methods to a child class dog. Then fill in that abstract animal sound method with what a dog says: bark.
Encapsulation
The ability for an object to protect its states and behavior
- can achieve by defining the accessibility of class members using access modifiers set variables to private, only the class itself has access - setters/getters methods to interact with the property
Ex:
If you have a Private String called name, you can make a getter function called public String getName() returning name and a setter function called public void setName(having one argument newName){} and the function will use the this keyword .name = newName; that way you can acquire what name is and set the value to something different.
Inheritance
when subclasses classes adopt the states/behaviors from its parent class, using the extends keyword
deriving classes allows for code reusability or the DRY concept
this/ super
this(used for constructor chaining) is a reference to the instance
super is a reference to the parent
Ex: if you have a class vehicle and it contains a string variable containing the brand of a car, for example ford. Then you can utilize inheritance via the extend keyword in the car class that contains a string variable model name
Polymorphism- “many forms”
the ability of objects and methods to adapt their behaviors in different contexts. This can be done with method overloading/ overriding and upcasting/ downcasting.
Ex:
Let’s say you have two separate employee objs. Which have the same id and name. If we were to compare each of these objs using the .equals method currently it would return false. This is where the concept of polymorphism comes in, we need to override the .equals method if we want to check the objects based on the properties bc currently only the references to the same object will return true. After overriding the method it would return true.
What is the difference between an abstract class and an interface?
Abstract class:
Allows you to create functionality that sub-classes can implement or override.
Interface:
Only allows you to define functionality, not implement it.
Can abstract classes have concrete methods? Can concrete (non-abstract) classes have abstract methods?
Concrete classes cannot have abstract methods
Abstract class can have no abstract methods
Can static methods access instance variables? Can non-static methods access static variables?
Static methods can’t access instance methods and instance variables directly
non-static methods can access any static method and static variable
What are the implicit modifiers for interface variables? Methods?
Variables have to be public static and final
Methods are implicitly abstract, default, and static