DAY 5:
1. String functions, 2. Literal String, 3. Non Literal String, 4. Mutable String, 5.
Immutable String
QUESTIONS(Theory)
1. What is mean by string?
A string is an object that represents a number of character values.
2. How to find length of the string?
String length() method in Java returns the length of the string.
3. How to find particular character in string?
indexOf() - It searches for the first occurrence of a character from the beginning of the string and
returns its index.
lastIndexof() - Itstarts searching backward from the end of the string and returns the index of
specified characters whenever it is encountered.
indexOf(char c, int fromIndex) - It starts searching forward from the specified index in the string.
lastIndexOf(char c, int fromIndex) - It starts searching backward from the specified index in the
string.
4. How to split the string?
String split() method is used to split a string into an array of substrings based on matches of the
given regular expression or specified delimiter. This method returns a string array containing the
required substring.
5. What is difference between literal String and non literal string?
Literal String - A series of characters in your code that is enclosed in double quotes.
Non literal String – It will always take more time to execute than string literal because it will
construct a new string every time it is executed.
6. What is mutable and immutable string?
The mutable strings are those strings whose content can be changed without creating a new object.
StringBuffer and StringBuilder are mutable versions of String in java.
Immutable objects have a fixed state, meaning their properties cannot be modified, and any
operation that appears to modify the object actually creates a new object with the updated state.
7. Difference between stringbulider and stringbuffer?
StringBuffer StringBuilder
StringBuffer is synchronized. This means that StringBuilder is asynchronized. This means
multiple threads cannot call the methods of that multiple threads can call the methods of
StringBuffer simultaneously. StringBuilder simultaneously.
Due to synchronization, StringBuffer is called Due to its asynchronous nature, StringBuilder
a thread safe class. is not a thread safe class.
Since there is no preliminary check for
Due to synchronization, StringBuffer is lot
multiple threads, StringBuilder is a lot faster
slower than StringBuilder.
than StringBuffer.
Use in single-threaded or non-concurrent
Use in multi-threaded environments.
environments.
8. Method name to identify memory location?
System. identityHashCode(java. lang. Object)
9. What are the string functions available in java?
String functions in Java are – substring(), toLowerCase(), toUpperCase() and trim().
10. What is difference between charAt() and contains() method?
The char At () method of the string class returns the char value at the specified index.
The contains () method is used to check if a sequence of characters is present inside a given string or
not.
11. What is the return type of compareTo()?
Compares this instance to a specified object and returns an integer that indicates whether the value
of this instance is less than, equal to, or greater than the value of the specified object.
12. Where the Literal String and non literal String stores?
String pool is a storage space in the Java heap memory where string literals are stored. It is also
known as String Constant Pool or String Intern Pool. It is privately maintained by the Java String
class.
13. What is mean by ASCII value?
ASCII (American Standard Code for Information Interchange) is a character encoding system used to
represent text in computers and other devices. It assigns a unique numerical value to each character,
including letters, numbers, punctuation marks, and other symbols.