10
Java String interview Question answers
String interview questions in Java is one of Integral part of any Core Java or J2EE interviews. No one can
deny importance of String and How much it in any Java
application irrespective of whether its core Java desktop application, web
application, Enterprise application or Mobile application. String is one of the
fundamental of Java programming language and correct understanding of String class is
must for any Java programmer. What makes String
interview questions in Java even more interesting is the special status of
String in terms of features and privileges it has, like + operator is kind of
overloaded to perform String concatenation despite the fact that Java does not support operator
overloading. There is a separate String pool to store String
literal etc. In this article we will some frequently asked question on String
in a Java interview which focuses on range of issues like immutability, thread-safety, Security etc.
Java String interview Question

1) What
is String in Java ? Is String is data type?
String in Java is not a primitive data type like int, long or
double. String is a class or in more simple term a
user defined type. This is confusing for some one who comes from C background.
String is defined in java.lang package and wrappers its content
in a character array. String provides equals() method to compare two
String and provides various other method to operate on String like toUpperCase() to convert
String into upper case, replace() to replace String contents, substring() to get
substring, split() to split long String into
multiple String.
2) Why
String is final in Java
String is final by design in Java, some of the points which makes sense
why String is final is Security, optimization and to maintain pool of String in
Java. for details on each of this point see Why String is final in Java.
3) What
is Difference between String and StringBuffer in Java
This is probably the most common question on String I have seen in Java
interviews. Though String and Stringbuffer are two different class they are
used in context of concatenating two Strings, Since String is
immutable in Java every operation which changes String produces
new String, which can be avoided by using Stringbuffer. See String vs StringBuffer for more details.
4) What
is difference in String on C and Java
If you have mentioned C in your resume, then you are likely to face this String interview question. Well C String
and Java String are completely different to each other, C String is a null
terminated character array while String
in Java is an Object. Also String is more feature rich in Java than C.
5) Why
char array is better than String for storing password?
This String interview question is debatable and you might not agree with
interviewer but this is also a chance to show that how deep and differently you
can think of. One of the reason which people give Why you should store password
in char array over String is related to immutability, since its
not possible to remove erase contents of String but you
can erase contents of char array. See Why char array preferred over String for
password for complete discussion.
6) How do
you compare two String in Java ?
This is another common String
interview question which appears on fresher level interviews. There are
multiple ways to compare two String like equals() method, equalsIgnoreCase() etc, You
can also see 4 ways to compare String in Java
for more examples. Main thing which interviewer checks is that whether
candidate mentioned equality operator or not "==", comparing String
with equality operator is common mistake which works in some case and doesn't
work in other. next String interview question is follow-up up of this.
7) Can we
compare String using == operator? What is risk?
As discussed in previous String question, You can compare String using
equality operator but that is not suggested or advised because equality
operator is used to compare primitives and equals() method should be used
to compare objects. As we have seen in pitfall of autoboxing in Java
that how equality operator can cause subtle issue while comparing primitive to
Object, any way String is free from that issue because it doesn't have
corresponding primitive type and not participate in autoboxing. Almost all the
time comparing String means comparing contents of String i.e. characters and equals() method is
used to perform character based comparison. equals() return
true if two String points to same object or two String has same contents while == operator
returns true if two String object points to same object but return false
if two different String object contains same contents. That explains why
sometime it works and sometime it doesn't. In short always use equals method in Java
to check equality of two String object.
8) How
substring method work in Java
This is one of the tricky Java question relate to
String and until you are familiar with internals of String class, its
difficult to answer. Substring shares same character array as
original String which can create memory leak if
original String is quite big and not required to
retain in memory but unintentionally retained by substring which is very small
in size and prevents large array from begin claimed during Garbage collection in Java.
See How Substring works in Java
for more details.
10)What
is String pool in Java
Another tough Java question asked in String interview. String pool is a
special storage area in Java heap, mostly located on PerGen space, to store
String literals like "abc". When Java program creates a new
String using String literal, JVM checks for that String in
pool and if String literal is already present in
pool than same object is returned instead of creating a whole new object.
String pool check is only performed when you create String as
literal, if you create String using new() operator,
a new String object will be created even if String with same
content is available in pool.
9) What
does intern() method do in Java
As discussed in previous String interview question, String object crated
by new() operator is by default not added in String pool as opposed to String
literal. intern() method allows to put an String object into pool.
11) Does
String is thread-safe in Java
If you are familiar with the concept of immutability and thread-safety you can easily
answer this String interview question in Java. Since String is immutable, it is
thread-safe and it can be shared between multiple thread without external
synchronization.
That's all on Java String interview
question. In Summary there are lot of specifics about String which
needs to be know for any one who has started Java programming and these String
question will not just help to perform better on Java Interviews but also opens
new door of learning about String. I didn't know many String related concepts
until I come across these question which motivated to research and learn more
about String in Java.
Other Java String tutorials from Learn About Linux Blog
No comments:
Post a Comment