A Complete article on String class in Java
String var = new String("Google interview questions");
There is a high chance you came across the questions about String in most of the interviews.
What is a String?
We can say a string as a sequence of characters.
How a string implemented?
The string is implemented as an array of bytes that stores the character using a certain specific character encoding ( Say for example CP1144)
How this Character Encoding works?
Java uses UTF-16 character encoding internally to store a string.
There is a high chance you came across the questions about String in most of the interviews.
What is a String?
We can say a string as a sequence of characters.
How a string implemented?
The string is implemented as an array of bytes that stores the character using a certain specific character encoding ( Say for example CP1144)
How this Character Encoding works?
Java uses UTF-16 character encoding internally to store a string.
Java supports a wide range of encoding.
Sample Java code :-
String crackInterview = new String("Facebook interview questions");
byte[] elonmask= crackInterview.getBytes();
byte[] uber= crackInterview.getBytes("CP1044");
will return different results.
How to create a String in Java?
There are two ways to create a string in java. One by String literal and other by new keyword.
String var = "Amazon interview questions"; ( using literals )
String var = new String("Microsoft interview questions"); ( using new operator)
There will be some differences in creating in these ways, right?
yes, while using new operator a string will be created in the string pool as well as in heap memory.
on the other hand while using literals string will be created on spring pool
Then what is a string pool? why java using a String pool?
Simply a pool of string in heap memory.
Then what is a string pool? why java using a String pool?
Simply a pool of string in heap memory.
String pool is an implementation of String interning concept, developed on flyweight design pattern.
we can use the intern() method to put back the string created using a new operator to string pool.
Since the value of the string object does not change, we called String is immutable.
What is the flyweight design pattern?
Since the value of the string object does not change, we called String is immutable.
Why we kept the string in a pool?
Security and Immutable
What is the flyweight design pattern?
A creational software design pattern.
Minimizing memory usage by share similar data.
How String becomes mutable?
To become string a mutale one we need to implement either StringBuilder or StringBuffer class
@author : Konzernites
How String becomes mutable?
To become string a mutale one we need to implement either StringBuilder or StringBuffer class
@author : Konzernites
No comments:
Post a Comment
Your feedback may help others !!!