Are Strings Primitive and Stringbuilders Non-Primitive types?

Dimitris999

New Member
In BeginnersGuide, on page 161 says:

'All other types including arrays of primitives types and strings are categorized as non-primitive types.
When you pass a non-primitive to a sub or when you assign it to a different variable, a copy of the reference is passed'.


I try to see what happen to strings with the following code:

B4X:
Dim Stra As String
Dim Strb As String
Stra = "a"
Strb = Stra
Strb = "b"
Log (Stra)
Log (Strb)

Stra prints "a" and Strb prints "b". The value of Stra not changed after changing Strb. They Remain two separate variables.

I try the above code using Stringbuilder instead of String

B4X:
Dim Stra As StringBuilder
Dim Strb As StringBuilder
Stra.Initialize
Strb.Initialize
Stra.Append("a")
Strb = Stra
Strb.Append("b")
Log (Stra)
Log (Strb)

I see that both two variables are print "ab".

So, the String variable types are primitive and when assign to a different variable or a sub copy their contents (pass by value) while Stringbuilder is non-primitive and copy the reference and not the content (pass by reference) ?

Is this true?
 

Dimitris999

New Member
I think I understand now.

If I do understand correctly, in my first example code:
B4X:
Strb = Stra
'After this line both Stra and Strb references to the same String Object that contains “a”



B4X:
Strb = "b"
'Because the strings are immutable and so it is not possible the modifying of the object that contains "a", an new object is created that contains “b” and Strb references to this new object. The Stra still references to the String Object that containing “a”

Is this correct?

Thank you in advance.
 
Upvote 0
Top