1. Java 11 String repeat() Method Overview
In this tutorial, We'll learn about Java String API repeat() Method with examples. repeat() method repeats the string given n number of times.
repeat() method works as it name suggests.
1.1 Syntax
public String repeat(int count)
This is a public method and can be accessed on string instance directly.
1.2 Parameters
count - number of times to repeatIf we pass count as 3 then string repeats 3 times.
1.3 Returns
String
A string composed of this string repeated count times or the empty string if this string is empty or count is zero
1.4 Throws
IllegalArgumentException - if the count is negative.
1.5 Since
Java 11
2. String repeat() Method Example
Below program is on repeat() method. Taking string str1 "Hello" and invoking repeat() method on str1 like str1.repeat(3). Now this method adds "Hello" three times to str1.String str1 = "Hello"; String repeatedString = str1.repeat(3); System.out.println(repeatedString);
Output:
See the output of above program now.
HelloHelloHello
3. repeat() Method Internal Code
As this method name looks very simple. But, take a look at its internal code. Before that just guess the number of lines inside the method.
See the below internal code. You will really get surprised after seeing it's code.
Basically, This code is fully optimized for all types of value of count.
public String repeat(int count) { if (count < 0) { throw new IllegalArgumentException("count is negative: " + count); } if (count == 1) { return this; } final int len = value.length; if (len == 0 || count == 0) { return ""; } if (len == 1) { final byte[] single = new byte[count]; Arrays.fill(single, value[0]); return new String(single, coder); } if (Integer.MAX_VALUE / count < len) { throw new OutOfMemoryError("Repeating " + len + " bytes String " + count + " times will produce a String exceeding maximum size."); } final int limit = len * count; final byte[] multiple = new byte[limit]; System.arraycopy(value, 0, multiple, 0, len); int copied = len; for (; copied < limit - copied; copied <<= 1) { System.arraycopy(multiple, 0, multiple, copied, copied); } System.arraycopy(multiple, 0, multiple, copied, limit - copied); return new String(multiple, coder); }
Step 1: First, It does validation for count. Count value should be positive. So, We have to make sure validation is in place. If count value is negative then it throws immediately IllegalArgumentException.
Step 2: If count value is 1 then we no need to repeat the string. So, Just need to return the current string.
Step 3: If count = 0 or string length is 0 then it returns empty string "".
Step 4: If string length is 1 then uses Arrays.fill() method repeat the string given count times. After that converts array to string.
Step 5: In case, all above steps are false then it uses System.arraycopy() method to copy the values to repeat the string.
How String.repeat was implemented and why?
4. Conclusion
In this article, We've learn what is String API repeat() method. Which scenario we should go for this method and example programs on it.
Further more, discussed how repeat() method works internally. Explanation given step by step.
Example code snippets shown in this article is available on GitHub.
No comments:
Post a Comment
Please do not add any spam links in the comments section.