Thursday, April 16, 2015

Java Interview Questions on String

1. How many objects are created in below code statement- +Java String  :

String obj = new String("Test");

Ans:

In this case, two objects are created :
One in Java heap.
Another in String Pool.

2. Please give output of the below piece of code :


public class Test{

public static void main(String args[]){

String test1 = "GOPESH";
String test2="GOPESH";

if(test1.equals(test2)){
System.out.println("Hello");
}

if(test1==test2){
System.out.println("Hi");
}

}
}

Ans:

Output of the above program would be :

Hello
Hi

This is because on initializing test1 with GOPESH, then a reference exists on String pool with value of GOPESH, now when test2 is initialized with the same value of GOPESH then the test2 start pointing to the same reference as of test1, and hence both equals (which compares value) and == (which compares references) returns true and hence both Hello and Hi prints.

3. How many objects are created in below lines of code?


String test1=new String("Mykates");
String test2=test1.concat("Discussion");

Ans:

This will create 4 objests.
1 - Mykates in String Pool
2 - Mykates in Heap
3 - Discussion in String Pool
4 - MykatesDiscussion (after concatenation) in String Pool.

4. Why String prominently used as HashMap key?

Ans:

Because, the String is immutable - so the value of a String reference could not be changed. Also, because of immutability the hashCode value could be cached which makes it even faster.

5. What is the difference between String, StringBuffer and StringBuilder?

Ans:

1. String - Literal stored in constant string pool, reference in Heap.
StringBuilder and StringBuffer - Stored in Heap.

2. String - Immutable (Not modifiable)
StringBuilder and StringBuffer - Mutable (Modifiable)

3. String - Is Synchronized and hence Thread-safe.
StringBuffer - Is Synchronized and hence Thread-safe.
StringBuilder - Non-Synchronized and hence not Thread-safe.

4. String is fast on performance.
StringBuffer - is Slow on performance.
StringBuilder - Is fast on performance.

6. +Core Java interview questions  Why String causes Memory leaks?

Ans:

String is backed up by character array and this behavior of String may lead to memory Leaks when same character array is shared between source String and substring which can prevent garbage collection to be invoked on String.
Substring method, in itself calls constructor for String(int offset, int count, char[] value)
So if original string stores 100 characters then substring will also have capacity to hold 100 characters  and it will prevent original string from being garbage collected if it does not have any Live references.
This issue is addressed in JDK 7.

7. Character Array or String for storing Passwords (or sensitive information) in Java?

Ans:

It is always advisable to use character array for storing passwords in Java rather than Strings. Also, encrypted password should be stored rather than plain text.
String is immutable, so the the password stored in string cannot be changed or masked. Also, the String value remains in String pool, so the password would remain in String pool until garbage collected and so possess a threat to security if someone has access to pool.
In character array, the text for the password could be changed.

8. Can we use String for switch() statement?

Ans :

Prior to JDK 7, we could not use String with switch statement, now we with JDK 7, we can use String object with switch statements.
switch stament uses the String object to compare the values as mentioned in the case statements using the String.equals() method. This would also mean that the comparison of String object in switch is also case sensitive.

9. What is intern() method in +Java String in Java?

Ans :

String class of Java class maintains a pool of strings where String literals are stored. String class in +Java  has a public method as intern(). The intern() method returns the canonical representation of the string object. This means that when the intern() method is called on a String object, it first searches the string contained by this object in the String Pool (as determined by @String equals() method) and if it is found then this string from the pool is returned, else this string object is added to the pool and a reference to this String object is returned.

String s1= new String("Hello Gopesh");
String s2= new String("Hello Anubha");
System.out.println(s1.intern()); ==> Hello Gopesh
System.out.println(s2.intern()); ==> Hello Anubha

As per Java docs reference,
So, if two Strings say x and y x.intern()==y.intern() if and only if x.equals(y) is true. Also, all literal strings and string-valued constant expressions are interned.
To summarize, String intern() method returns a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

10. Why is String is Thread - safe in +Java ?

Ans :

String being immutable could be safely used in multi-threaded environment and hence it could be called as Thread Safe.

 11. What is the difference between String, StringBuffer and StringBuilder?

Ans :

String : Is immutable, thread-safe, fast , stored in Pool and Heap and is used where the initial string will not be changed and could be used in multi-threaded environment.

StringBuffer : Is mutable, thread-safe and slow. Stored in Heap and used where the initial string could be changed and is required to be used in multi-threaded environment.

StringBuilder : Is mutable and NOT thread safe and is fast. Stored in Heap and is used where the initial string could be changed and is required to be used in single threaded environment.

Saturday, February 28, 2015

What is Runtime Polymorphism in JAVA?

Runtime polymorphism, often referred to as Dynamic Binding in OOP (Object Oriented Paradigm) concepts of Java where the method to be invoked is decided at the runtime.
This is a case of Overriding where a method say display() is overridden in the child class, and this method is called by an object variable of Parent class containing the reference of the Child class.

Eg :
class Base{
public Base(){}
public void display(){
System.out.println("Base");
}
}

class Child extends Base{
public Child(){}
public void display(){
System.out.println("Child");
}
}

class Test{
public static void main(String args[]){
Child obj = new Base();
obj.display();
}
}

Output of above program would be ==> Child

This is the case of dynamic binding where you see that when the display() is called from the base class object having reference of child class is called, child class method is called.
This is decided by JVM during the runtime.