Java Basics Part 2 Flashcards
What is the difference between static and final variables?
The main difference between static and final is that the static is used to define the class member that can be used independently of any object of the class. In contrast, final is used to declare a constant variable or a method that cannot be overridden or a class that cannot be inherited.
What are the default values for all data types in Java?
byte - 0
short - 0
int - 0
long - 0L
float - 0.0f
double - 0.0d
char - ‘\u0000’
boolean - false
String (or any object) - null
What is a wrapper class? List them.
A Wrapper class is a class whose object wraps or contains primitive data types.
Boolean
Character
Byte
Short
Integer
Long
Float
Double
What is autoboxing / unboxing?
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.
Unboxing on the other hand refers to converting an object of a wrapper type to its corresponding primitive value. For example conversion of Integer to int.
Is Java pass-by-value or pass-by-reference?
Java is officially always pass-by-value.
What makes a class immutable?
Immutable class means once the object of the class is created its fields cannot be modified or changed.
If two objects are equal, do they have the same hashcode? If not equal?
If two objects have the same hashcode then they are NOT necessarily equal. Otherwise you will have discovered the perfect hash function.
But the opposite is true: if the objects are equal, then they must have the same hashcode.
What data types are supported in switch statements?
A switch works with the byte , short , char , and int primitive data types.
List all non-access modifiers.
static
final
abstract
synchronized
volatile
transient
native
How to pass multiple values with a single parameter into a method?
The varrags allows the method to accept zero or muliple arguments. Before varargs either we use overloaded method or take an array as the method parameter but it was not considered good because it leads to the maintenance problem.
What is a static block?
A static block in Java is a block of code that is executed at the time of loading a class for use in a Java application.
What are static imports?
Static imports allows fields and methods that have been scoped as public static to be used without specifying their class.
What methods are available in the Object class?
The following are methods for the java Object super class:
toString() : toString() provides String representation of an Object and used to convert an object to String. The default toString() method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object
hashCode() : For every object, JVM generates a unique number which is hashcode. It returns distinct integers for distinct objects. A common misconception about this method is that hashCode() method returns the address of object, which is not correct. It convert the internal address of object to an integer by using an algorithm.
equals(Object obj) : Compares the given object to “this” object (the object on which the method is called). It gives a generic way to compare objects for equality.
getClass() : Returns the class object of “this” object and used to get actual runtime class of the object. It can also be used to get metadata of this class. The returned Class object is the object that is locked by static synchronized methods of the represented class. As it is final so we don’t override it.
finalize() method : This method is called just before an object is garbage collected. It is called by the Garbage Collector on an object when garbage collector determines that there are no more references to the object.
clone() : It returns a new object that is exactly the same as this object. For clone() method refer Clone()
What is the difference between == and .equals()?
In general both equals() and “==” operator in Java are used to compare objects to check equality but here are some of the differences between the two:
Main difference between .equals() method and == operator is that one is method and other is operator.
What is an enhanced for loop and what is a forEach loop?
It provides an alternative approach to traverse the array or collection in Java. It is mainly used to traverse the array or collection elements.