Java Interview Question
Categories: Java 8(JDK1.8)
How many objects will be created in the following code?
String s = new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).
What is the output of the following Java program?
public class Test
public static void main (String args[])
{
String a = new String("Sharma is a good player");
String b = "Sharma is a good player";
if(a == b)
{
System.out.println("a == b");
}
if(a.equals(b))
{
System.out.println("a equals b");
}
}
Output
a equals b
What is the output of the following Java program?
public class Test
{
public static void main (String args[])
{
String s1 = "Sharma is a good player";
String s2 = new String("Sharma is a good player");
s2 = s2.intern();
System.out.println(s1 ==s2);
}
}
Output
true
What are the differences between String and StringBuffer?
The differences between the String and StringBuffer is given in the table below.
No.StringStringBuffer
1)The String class is immutable.The StringBuffer class is mutable.
2)The String is slow and consumes more memory when you concat too many strings because every time it creates a new instance.The StringBuffer is fast and consumes less memory when you cancat strings.
3)The String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.The StringBuffer class doesn't override the equals() method of Object class.
What are the differences between StringBuffer and StringBuilder?
The differences between the StringBuffer and StringBuilder is given below.
No.StringBufferStringBuilder
1)StringBuffer is synchronized, i.e., thread safe. It means two threads can't call the methods of StringBuffer simultaneously.StringBuilder is non-synchronized,i.e., not thread safe. It means two threads can call the methods of StringBuilder simultaneously.
2)StringBuffer is less efficient than StringBuilder.StringBuilder is more efficient than StringBuffer.
How can we create an immutable class in Java?
We can create an immutable class by defining a final class having all of its members as final. Consider the following example.
public final class Employee{
final String pancardNumber;
public Employee(String pancardNumber){
this.pancardNumber=pancardNumber;
}
public String getPancardNumber(){
return pancardNumber;
}
}
Why CharArray() is preferred over String to store the password?
String stays in the string pool until the garbage is collected. If we store the password into a string, it stays in the memory for a longer period, and anyone having the memory-dump can extract the password as clear text. On the other hand, Using CharArray allows us to set it to blank whenever we are done with the password. It avoids the security threat with the string by enabling us to control the memory.
Write a Java program to count the number of words present in a string?
Program:
public class Test
{
public static void main (String args[])
{
String s = "Sharma is a good player and he is so punctual";
String words[] = s.split(" ");
System.out.println("The Number of words present in the string are : "+words.length);
}
}
Output
The Number of words present in the string are : 10
Name some classes present in java.util.regex package.
There are the following classes and interfaces present in java.util.regex package.
a) MatchResult Interface
b) Matcher class
c) Pattern class
d) PatternSyntaxException class