Java Basics Flashcards
What is Java?
Java is a C-like, multiparadigmed, compiled programming language used for back-end development projects.
What are the advantages of Java? (7 are listed here)
It: 1) is a mature language, 2) is maintained by Oracle, 3) is portable, 4) is platform independent, 5) supports multithreading, 6) is compiled, 7) has automatic garbage collection
What is the JDK?
Java Development Kit. Provides an environment to develop and execute a Java program. Contains the compiler, JRE, and JVM.
What is the JRE?
The Java Runtime Environment. It is all that’s needed to run an application. Contains the core library and JVM.
JVM
Java Virtual Machine. Takes the byte code and executes it. Different operating systems can have different implementations of the JVM.
What is a class?
A class is the blueprint of an object. It defines the states (fields), and behaviors (methods) that its instances may have.
What is an object?
An object is an instance of the class. It is the representation of a real-world entity and has its own implementations of states and behaviors that are defined in its class.
What is the root class from which every class extends.
The object class. Every other class extends the object class and inherits methods from it.
What are some methods from the object class?
Equals(), getClass(), hashCode(), toString()
What are the 8 primitive types in Java?
There is: byte, short, int, long, float, double, boolean, and char.
Where are Strings stored?
Strings are stored in the heap. Strings declared literally are specifically stored in the string pool.
Explain stack vs heap?
Stack is short term storage used for methods currently running while the heap is long term storage used throughout the application.
What is the stack?
Short term storage that holds primitive and reference variables. It follows a LIFO (last in First out) pattern.
What is the heap?
It is long term memory storage. Holds objects and is managed by the garbage collector
What are annotations?
Annotations start with the ‘@’ and are used to provide supplemental information about a program. They can change the way a program is treated by the compiler. They help to associate metadata to the program elements.
Why are strings immutable in Java?
Strings in Java are immutable, meaning they cannot be changed at a later time. This is because when a string is created, it is stored as a new object in the string pool, or it points to a string object of the same value. This helps the program save memory.
What is the difference between String, StringBuilder, and StringBuffer?
String is immutable. StringBuilder and StringBuffer are mutable alternatives with methods that allow for manipulation.
Difference between StringBuilder and StringBuffer?
StringBuilder is not thread safe, but faster. StringBuffer is thread safe, but slower.
What are different variable scopes in Java?
There is class scope, instance scope, method scope, and block scope. Each one defines different “lifetimes” or the variables declared in them.
What are the access modifiers in Java?
Public, default, protected, and private.
Public classes are accessible where?
Throughout the program
Default classes are accessible where?
Throughout the same package.
Protected classes are accessible where?
Throughout the same package and subclasses.
Private classes are accessible where?
Only within the same class.