Java String is Immutable.
String Builder and String Buffer are not Immutable.
Other Immutable Classes -
All Wrapper class like Integer, Long,Short,Byte,Double,Long ,Char etc.
Advantage of being Immutable-
1.Immutable classes are inherently thread safe. No worry in Multi threaded environment.
2.Security Reasons - Database URL , username and password are String.If they can be altered it the problem in app security.
How to create Immutable Class ->
1.Declare class Final so that class can not be overridden.
2.Make all fields Private so that direct access not allowed.
3.Do not provide setter method
4.Make all fields Final so that their value can be assigned only once.
5.Initialize all fields in constructor using deep copy.
String Object Creation -
1.Using Literal --> "abc" - creates 1Obj and 1 reference - stored in String Pool
2.Using new --> new String("abc") - creates 2Obj and 1 reference - each stored in Pool and Heap
3.Using concatenation -
String Concatenation -
1.Using + operator
2.Using concat()
+ Operator - compiler uses StringBuilder to add Strings using append()
eg. String s = "Tushar"+"Patil"
Compiler internally handles as below -
String s=(new StringBuilder()).append("Sachin").append(" Tendulkar).toString();
Also,
In Concatenation once String Literal occurs all next + are considers as concat.
eg. String s = 30+40+"Tushar"+50+30;
Ans-> s = 70Tushar5030.
Concat and +Operator creates new Object as seen in above.
Hence == with same literal is always false.
Eg. String s1 = "abc";
String s2 = "a";
String s3 ="bc";
String s4 = s2+s3;
Now- s1== s4 -> False. (Becauase + creates new Object of string at compilation)
String Builder and String Buffer are not Immutable.
Other Immutable Classes -
All Wrapper class like Integer, Long,Short,Byte,Double,Long ,Char etc.
Advantage of being Immutable-
1.Immutable classes are inherently thread safe. No worry in Multi threaded environment.
2.Security Reasons - Database URL , username and password are String.If they can be altered it the problem in app security.
How to create Immutable Class ->
1.Declare class Final so that class can not be overridden.
2.Make all fields Private so that direct access not allowed.
3.Do not provide setter method
4.Make all fields Final so that their value can be assigned only once.
5.Initialize all fields in constructor using deep copy.
String Object Creation -
1.Using Literal --> "abc" - creates 1Obj and 1 reference - stored in String Pool
2.Using new --> new String("abc") - creates 2Obj and 1 reference - each stored in Pool and Heap
3.Using concatenation -
String Concatenation -
1.Using + operator
2.Using concat()
+ Operator - compiler uses StringBuilder to add Strings using append()
eg. String s = "Tushar"+"Patil"
Compiler internally handles as below -
String s=(new StringBuilder()).append("Sachin").append(" Tendulkar).toString();
Also,
In Concatenation once String Literal occurs all next + are considers as concat.
eg. String s = 30+40+"Tushar"+50+30;
Ans-> s = 70Tushar5030.
Concat and +Operator creates new Object as seen in above.
Hence == with same literal is always false.
Eg. String s1 = "abc";
String s2 = "a";
String s3 ="bc";
String s4 = s2+s3;
Now- s1== s4 -> False. (Becauase + creates new Object of string at compilation)
No comments:
Post a Comment