Java Flashcards
Understanding the Java Programming Language
What is Java?
Java is an Object Oriented Program known for it’s “run once, write anywhere” because of the JVM. Garbage Collection, Secure, Simpler, Portable.
What is JRE
Java Runtime Environment - This contains the libraries that for the applications
What is JDK
Java Development Kit - This contains the compiler and is where applications are developed turns the code to bytecode
What is JVM
Java Virtual Machine - runs the bytecode on for the specific platform.
What’s the difference between an object and a class
An object is an instantiation of the class. Class is a blueprint for an object.
What is the root class from which every class extends?
Object: some of the methods for the object class are equals, getClass, hashCode, toString, thread stuff.
Where are Strings stored?
Primarily the String Pool.
Are variable references stored on the stack or heap? What about the objects they
refer to?
Stack and the object is in the heap.
What are annotations?
Annotations are a type of metadata that signal to the compiler information about the program
Java has a few built-in annotations you should be familiar with:
@Override - declares the method must override an inherited method (otherwise, a compilation error occurs)
@Deprecated - marks a method as obsolete (compilation warning if used anywhere)
@SuppressWarnings - instructs compiler to suppress compilation warnings
@FunctionalInterface - designates an interface to be a functional interface (covered in another module)
What is a POJO vs a bean?
Plain Old Java Object, has no special restrictions outside those enforced by Java.
A bean is a type of POJO which is Serialized, has private fields with getters, setters, or both, a
no-arg constructor, and fields which are only accessible by the constructor or getters/setters.
Can you force garbage collection in Java? When is an object eligible for GC?
No, when there are no reference variables pointing to that object.
Why are strings immutable in java?
To save space in the heap.
How would you make your own objects immutable?
The use of final and getters and setters. Private would be used in this case.
What are the different variable scopes in Java?
Class, or static, scope
Instance, or object, scope
Method scope
Block scope
Class, or static, scope?
Class scoped variables reside on the class definition itself. This means that when objects update a class-scoped variable, the change is reflected across all instances of the class. Class scope is declared with the static keyword. Methods can also be declared as class scope. However, static methods cannot invoke instance methods or variables (think about it: which specific object would they reference?). Static methods and variables should be referenced through the class directly, not through an object. For example: MyClass.myStaticMethod() or MyClass.myStaticVariable.
Instance, or object, scope
Instance scope means that the variable is attached to individual objects created from the class. When an instance-scoped variable is modified, it has no effect on other, distinct objects of the same class.
Method scope
Method scope is the scope of a variable declared within a method block, whether static or instance. Method-scoped variables are only available within the method they are declared; they do not exist after the method finishes execution (the stack frame is popped from the stack and removed from memory after execution).
Block scope
Block scoped variables only exist within the specific control flow block, of which there are several in Java: for, while, and do-while loops, if/else-if/else blocks, switch cases, or even just regular blocks of code declared via curly braces ({}). After the block ends, variables declared within it are no longer available.
What are the access modifiers in Java?
Access modifiers are keywords which define the ability of other code to access the given entity. Modifiers can be placed on classes, interfaces, enums, and class members. The access modifiers are listed below:
Modifier Access Level public Available anywhere protected Within the same package, and this class' sub-classes default Within the same package private Only within the same class
What are the non-access modifiers in Java?
static - denotes "class" scope, meaning the member resides on the class itself, not object instances. static variables can be accessed through the class, e.g. MyClass.staticVariable static methods can be called directly without needing an instance of the class, e.g. MyClass.someMethod()
final- when applied to a variable, it means the variable cannot be re-assigned when applied to a class, it means the class cannot be extended when applied to a method, it means the method cannot be overriden
abstract - when applied to a class, the class cannot be instantiated directly (instead, it should be inherited) when applied to a method, only the method signature is defined, not the implementation. Also, the class where the method resides must also be abstract. Concrete subclasses must implement the abstract method. synchronized - relevant to threads and preventing deadlock phenomena (discussed in a separate module)
transient - marks a variables as non-serializable, meaning it will not be persisted when written to a byte stream (discussed in another module)
volatile - marks a variable to never be cached thread-locally. Obscure, rarely-used keyword.
strictfp - restricts floating point calculations for portability. Obscure, rarely-used keyword.
What is the difference between static and final variables?
static - denotes "class" scope, meaning the member resides on the class itself, not object instances. static variables can be accessed through the class, e.g. MyClass.staticVariable static methods can be called directly without needing an instance of the class, e.g. MyClass.someMethod()
final when applied to a variable, it means the variable cannot be re-assigned when applied to a class, it means the class cannot be extended when applied to a method, it means the method cannot be overriden
What are the default values for all data types in Java?
boolean false char “” or \u0000 short 0 byte 0 int 0 long 0L float 0.0f double 0.0d
What is a wrapper class? List them
Wrapper classes are classes that let you treat primitives as Objects.
What is autoboxing / unboxing?
Turning primitives to wrapper classes and reverse.
Is Java pass-by-value or pass-by-reference?
Pass by value - This means that a copy of the value will be passed to a method. But the trick is that passing a copy of the value also changes the real value of the object.
What makes a class immutable?
An object is considered immutable if its state cannot change after it is constructed
The class must be declared as final (So that child classes can’t be created) Data members in the class must be declared as private (So that direct access is not allowed) Data members in the class must be declared as final (So that we can’t change the value of it after object creation) A parametrized constructor should initialize all the fields performing a deep copy (So that data members can’t be modified with object reference) Deep Copy of objects should be performed in the getter methods (To return a copy rather than returning the actual object reference) No setters (To not have the option to change the value of the instance variable)
If two objects are equal, do they have the same hashcode? If not equal?
Yes, if they do not equal the hashCode can be the same but not recommended. The hashCode() method returns a hash code - a number that puts instances of a class into a finite number of categories. There are a few rules that the method follows: You are expected to override hashCode() if you override equals() The result of hashCode() should not change in a program if .equals() returns true, the hash codes should be equal if .equals() returns false, the hash codes do not have to be distinct. However, doing so will help the performance of hash table
What data types are supported in switch statements?
byte, char, short, int, String, and the wrappers of the primitives
How to pass multiple values with a single parameter into a method?
Parse
List
Array
this needs more
What are static imports?
The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification
What is the difference between == and .equals()?
== checks hashcode and .equals() checks the actual value.
What is an enhanced for loop and what is a forEach loop?
forEach is specific to Collection API
Enhanced for loop can be used anywhere.
What are the 3 usages of “super” keyword?
Variables, methods, and constructors
How would you perform constructor chaining?
Calling constructor in other constructors
With the “this” keyword
What happens if you don’t define a constructor for a class? Can you still instantiate it?
A default constructor will be “injected” into the class by the compiler. Instantiation still occurs.
What is a constructor
When we use the new keyword in order to create an object, the JVM is invoking a special class member called a constructor. A constructor declares how an object is to be instantiated and initialized from the class “blueprint”. A constructor is declared like a method, except its method signature does not contain a return type, and a constructor always has the same name as the class. The new object returned from the constructor is always of the class in which the constructor is declared.
What is the “this” keyword?
“This” keyword refers to the object which is being instantiated - it is used to initialize instance variables, or - to call other constructors (within the same class) (this is called constructor chaining).
What is the “super” keyword?
the super keyword, which references the “super”, or parent, class. When invoked as a method (super()), the parent class constructor will be called. A super() call (or a this() call) must be the first line of any constructor. If not explicitly provided, the compiler will inject it on the first line