optimization - length() instead of equals() to check empty string in java -
is there difference in code optimization if use use length() instead of equals() check empty string in java??
public boolean isempty(string str) { return str.equals(""); //never } public boolean isempty(string str) { return str.length()==0; //correct way check empty }
is true??
you can use str.isempty()
, internally checks length.
here's implementation of string
class :
public int length() { return count; } public boolean isempty() { return count == 0; }
you can see isempty()
compares length 0.
Comments
Post a Comment