Use Strings carefully β οΈ
If two Strings are concatenated using β+β operator in a βforβ loop, then it creates a new String Object, every time.
This causes wastage of Memory and increases Performance time.
Also, while instantiating a String Object, constructors should be avoided and instantiation should happen directly. For example:
#memory #java #string
@ProgrammingTip
If two Strings are concatenated using β+β operator in a βforβ loop, then it creates a new String Object, every time.
This causes wastage of Memory and increases Performance time.
Also, while instantiating a String Object, constructors should be avoided and instantiation should happen directly. For example:
//Slower Instantiation β
String bad = new String("string object");
//Faster Instantiation β
String good = "string object";
#memory #java #string
@ProgrammingTip
Use String.valueOf() instead of toString() π
If obj needs to be converted to string then the result of obj.toString() and String.valueOf(obj) will be same.
But String.valueOf() is Null Safe, means it will never throw NullPointerException. π―
γ°γ°γ°γ°γ°γ°
#java #string
@ProgrammingTip
If obj needs to be converted to string then the result of obj.toString() and String.valueOf(obj) will be same.
But String.valueOf() is Null Safe, means it will never throw NullPointerException. π―
Test test = null;
// Below statement will not throw NPE
System.out.println(String.valueOf(test));
// Next statement will throw NPE
System.out.println(test.toString())
γ°γ°γ°γ°γ°γ°
#java #string
@ProgrammingTip