Java Basics Part 1 Flashcards
What is Java?
Explain some features of Java
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.
Some features are as followed:
Object Oriented
In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
Platform Independent
Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform-independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
Simple
Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
What is JDK / JRE / JVM?
JDK (Java Development Kit) is a software development kit.
JRE (Java Runtime Environment) is a software bundle that allows Java program to run.
JVM (Java Virtual Machine) is an environment for executing bytecode.
What is the difference between an object and a class?
A class is a template used for the creation of objects. An object is an instance of a class.
What is the root class from which every class extends?
Every Java class extends the class Object, which means that every class we create has at its disposal all the methods defined in the Object class.
What are the primitive data types in Java?
Primitive data types - includes
byte , short , int , long , float , double , boolean and char.
Where are Strings stored?
In Java Strings are stored on the heap area in a separate memory location known as String Constant pool.
Explain stack vs heap
Stack is a linear data structure whereas Heap is a hierarchical data structure.
Stack memory will never become fragmented whereas Heap memory can become fragmented as blocks of memory are first allocated and then freed.
Stack accesses local variables only while Heap allows you to access variables globally.
Are variable references stored on the stack or heap? What about the objects they refer to?
All objects in Java are stored on the heap. The “variables” that hold references to them can be on the stack or they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also.
What is a stack frame? When are these created?
A stack frame is a memory management technique used in some programming languages for generating and eliminating temporary variables. Stack frames are only existent during the runtime process.
What are annotations?
Annotations are used to provide supplemental information about a program. Annotations start with ‘@’ character.
What is a POJO? How is it different from a bean?
POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program.
All JavaBeans are POJOs but not all POJOs are JavaBeans.
A JavaBean is a Java object that satisfies certain programming conventions:
-the JavaBean class must implement either Serializable or Externalizable;
-the JavaBean class must have a public no-arg constructor;
-all JavaBean properties must have public setter and getter methods (as appropriate);
-all JavaBean instance variables should be private.
Can you force garbage collection in Java? When is an object eligible for GC?
Unfortunately, this desire to immediately free up memory must go unrequited, as there is no way to force garbage collection (GC) in Java. An object is eligible to be garbage collected if its reference variable is lost from the program during execution.
Why are strings immutable in java? How would you make your own objects immutable?
The String is immutable in Java because of the security, synchronization and concurrency, caching, and class loading. You can make an object immutable by declaring a class as final or setting fields to private.
What is the difference between String, StringBuilder, and StringBuffer?
String is immutable whereas StringBuffer and StringBuilder are mutable classes.
StringBuffer is thread-safe and synchronized whereas StringBuilder is not.
What are the different variable scopes in Java?
There are three variable scopes:
Instance
Class
Local
Instance variables have a scope throughout the class except in static methods.
Class variables have a scope throughout the class.
Local variables have a scope within the block in which it is declared.