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.