String Flashcards

1
Q

What is the meaning of Immutable in the context of String class in Java?

A

An Immutable object cannot be modified or changed in Java. String is an Immutable class in Java.

Once a String object is created, it cannot be changed. When we assign the String to a new value, a new object is created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
How many objects does following
code create?
Code:
String s1="HelloWorld";
String s2=" HelloWorld ";
String s3=" HelloWorld ";
A

The above code creates only one object. Since there is only one String Literal “HelloWorld” created, all the references point to same object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many objects does
following code create?
Code:
String s = new String(“HelloWorld”);

A

The above code creates two objects. One object is created in String constant pool and the other is created on the heap in non-pool area.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is String interning?

A

String interning refers to the concept of using only one copy of a distinct String value that is Immutable.

It provides the advantage of making String processing efficient in Time as well as Space complexity. But it introduces extra time in
creation of String.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why Java uses String literal concept?

A

Java uses String literal concept to make Java more efficient in memory. If same String already exists in String constant pool, it can
be reused. This saves memory usage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the basic difference between a String and StringBuffer object?

A

String is an immutable object. Its value cannot change after creation.
StringBuffer is a mutable object. We can keep appending or
modifying the contents of a StringBuffer in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How will you create an immutable class in Java?

A
  1. Add final modifier to class to prevent it from getting
    extended
  2. Add private modifier to all the fields to prevent direct
    access
  3. Do not provide any setter methods for member variables
  4. Add final modifier to all the mutable fields to assign value
    only once
  5. Use Deep Copy to initialize all the fields by a constructor
  6. In clone method, return a copy of object instead of the
    actual object reference
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Arrange the three classes String,
StringBuffer and StringBuilder in the
order of efficiency for String
processing operations?

A

StringBuilder is the most efficient class. It does not have the overhead of Synchronization. StringBuffer is a Synchronized class.
It has better performance than String but it is slower than StringBuilder. String is the slowest for any String processing operations, since it is leads to creation of new String literal with
each modification.

So the decreasing order of efficiency is: StringBuilder, StringBuffer,
String

How well did you know this?
1
Not at all
2
3
4
5
Perfectly