Campus and Interview preparation with Java
Java Interview Questions
Java is one
of the most used programming language in the world. Java is versatile and
covers most of the domains from desktop applications to cloud application.
Several IT companies requires java professionals (fresher or experienced). Here
we try to provide list of frequently asked java interview questions.
What is immutable object in Java?
Immutable classes are Java
classes whose objects can not be modified once created. Any modification in
Immutable object result in new object. For example is String is immutable in
Java. Mostly Immutable are also final in Java, in order to prevent sub class
from overriding methods in Java which can compromise Immutability. You can
achieve same functionality by making member as non final but private and not
modifying them except in constructor.source thanks to -www.mysirg.com for providing this materials
What is the difference among String, StringBuffer and StringBuilder in Java?
String
String is immutable ( once created can not be changed )object . The object created as a String is stored in the Constant String Pool .Every immutable object in Java is thread safe ,that implies String is also thread safe . String can not be used by two threads simultaneously.
String once assigned can not be changed.
String demo = ” hello ” ;
// The above object is stored in constant string pool and its value can not be modified.
// The above object is stored in constant string pool and its value can not be modified.
demo=”Bye” ; //new “Bye”
string is created in constant pool and referenced by the demo variable
// “hello” string still exists in string constant pool and its value is not overrided but we lost reference to the “hello”string.
// “hello” string still exists in string constant pool and its value is not overrided but we lost reference to the “hello”string.
source thanks to -www.mysirg.com for providing this materials
StringBuffer
StringBuffer is mutable means one can change the value of the object . The object created through StringBuffer is stored in the heap . StringBuffer has the same methods as the StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread safe .
Due to this it does not
allow two threads to simultaneously access the same method . Each method can be
accessed by one thread at a time .
But being thread safe has
disadvantages too as the performance of the StringBuffer hits due to thread
safe property . Thus StringBuilder is faster than the StringBuffer when calling
the same methods of each class.
StringBuffer value can be
changed , it means it can be assigned to the new value . Nowadays its a most
common interview question ,the differences between the above classes .
String Buffer can be converted to the string by using
toString() method.
String Buffer can be converted to the string by using
toString() method.
StringBuffer demo1 = new
StringBuffer(“Hello”) ;
// The above object stored in heap and its value can be changed .
demo1=new StringBuffer(“Bye”);
// Above statement is right as it modifies the value which is allowed in the StringBuffer
// The above object stored in heap and its value can be changed .
demo1=new StringBuffer(“Bye”);
// Above statement is right as it modifies the value which is allowed in the StringBuffer
StringBuilder
StringBuilder is same as the StringBuffer , that is it stores the object in heap and it can also be modified . The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe.StringBuilder is fast as it is not thread safe .
StringBuilder demo2= new
StringBuilder(“Hello”);
// The above object too is stored in the heap and its value can be modified
demo2=new StringBuilder(“Bye”);
// Above statement is right as it modifies the value which is allowed in the StringBuilder
// The above object too is stored in the heap and its value can be modified
demo2=new StringBuilder(“Bye”);
// Above statement is right as it modifies the value which is allowed in the StringBuilder
What is the difference between Array List and Vector in Java?
Array List vs Vector
Array List
|
Vector
|
Not Synchronized and so not thread safe
|
Synchronized therefore thread safe
|
Fast in performance
|
Slow as it is thread safe
|
Introduced first in java version 1.2
|
Introduced first in java version 1.0
|
does not provide method to set increment size
|
setSize() method in Vector can set increment size
|
Array List can only use Iterator for traversing
|
Vector uses both Enumeration and Iterator for traversing
|
source thanks to -www.mysirg.com for providing this materials
main method in java is static
This is necessary because
main() is called by the JVM before any objects are made. Since it is static it
can be directly invoked via the class.
What is the default value of variable in Java?
Default
value of variable in java
Variables declared inside a class are known as member
variables (static or non static). These variables are initialized with their
default value depending on the type of variable. For example variable of type
int contains 0 by default, double variable contains 0.0, etc. The variables of
primitive type contains 0 as a default value in a broader sense.
When variable is of any class type (non-primitive
type), then it is known as reference variable, and it contains null value as a
default value.
When member variables are final, they are blank until
initialized.
You can set default value of member variables in the
class definition. For example
class Box
{
private int length=10, breadth=10,
height=10;
...
}
Local variables (variables declared in a function or
block), are blank until initialized.
There is no concept of garbage value like C and C++ variables.
There is no concept of garbage value like C and C++ variables.
Comments
Post a Comment