Friday, February 14, 2014

String Interview Questions

1.Shall we consider String is a class or DataType or both?

You can consider string as a both class and datatype.

2.What is String constant pool?

It is a separate block of memory for strig objects and it will be assigned by JVM.
If you will use assignment operator, that wil be stored in constant pool only.

e.g String test="javahit";

3.Can you tell me the example for  ==  and equals()?

== does not compare the contet of the objects.
equals() - compare the contents.

e.g   Strig test = "java";
String check = "java";
if(test == check) {
System.out.println("Same");
else
System.out.pritln("Not same");
}

String t1 = new String("Test");
String t2 = new String("Test");

if(t1.equals(t2)) {
System.out.println("Same");
else
System.out.pritln("Not same");
}

4.Can you tell me some important methods in String class?

String concat(String t);
int length();
char charAt(int i);
int index(String s);
boolean equals(String s);
String substring(int i1, int i2);

No comments:

Post a Comment