I came across this issue when I was trying to form strings build with Integers. I used scala's Stringbuilder and see this weird behavior.
println(new StringBuilder(1).append(2).append(3))
>23
println(new StringBuilder(1.toString()).append(2).append(3))
>123
Am I doing something wrong or just implicit conversion not taking place in constructor argument and ignores it silently.
答案 0 :(得分:2)
The constructor takes an Int
for starting capacity, which is why you don't see it as output in your string:
def this(capacity: Int) = this(capacity, "")
答案 1 :(得分:1)
You are using 2 different constructors:
StringBuilder(int capacity)
: Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.StringBuilder(String str)
: Constructs a string builder initialized to the contents of the specified string.So new StringBuilder(1)
returns an empty new StringBuilder
of initial capacity 1
.