Just curious to know which among the two snippets below is the most efficient?
String outStr = inputStr.substring(20);
(or)
String outStr = inputStr.substring(20, inputStr.length());
Does the first snippet internally invoke a inputStr.length() in case the second argument is not present?
Not exactly – it invokes
substring(beginIndex, count), wherecountis an internal field holding the number of chars.length()also returnscount, however, and so it’s +1 method invocation. Virtually the same.But prefer the one-argument version – it is cleaner to read.