Android Question Passing strings into procedures

j_o_h_n

Active Member
Licensed User
Hi I feel like i am missing something here

In the B4x Basic language booklet it states

... 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.
This means that the data itself isn't duplicated.


B4X:
Sub sub1(sP As String)
    sP =  "b"
End Sub

Dim s As String

s = "a"

sub1(s)

Msgbox(s) 'displays "a"

I made a really simple example to try this out.
if no copy of "a" is made, just a copy of its reference, then vars sP and s are referencing the same data. So how is the value of s in the msgbox call is still "a".

Thanks for any guidance.
 

Star-Dust

Expert
Licensed User
Longtime User
Hi I feel like i am missing something here

In the B4x Basic language booklet it states

... 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.
This means that the data itself isn't duplicated.


B4X:
Sub sub1(sP As String)
    sP =  "b"
End Sub

Dim s As String

s = "a"

sub1(s)

Msgbox(s)

if no copy of "a" is made, just a copy of its reference, then vars sP and s are referencing the same data. So how is the value of s in the msgbox call is still "a".

Thanks for any guidance.
The passing of parameters is by value values, not by reference, so you can't do it.
Edit so:
B4X:
Sub sub1(sP As String) as String
    Return sP & "b"
End Sub

Dim s As String = "a"

S=sub1(s)

Log(s)  'see ba
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
All declared variables are associated with a particular location in memory that serves to hold their value.

All primitive types are Value types, except Strings. This means that their location in memory actually holds their value. When passed to a procedure by value a copy of the contents of their memory location are passed.

Strings, Arrays and other Objects, are actually Reference types. This means that their location in memory actually contains a reference (pointer) to another location in memory where their actual value is located. For Strings this is a sequence of characters, for Arrays this is a sequence of values of the declared types, for Objects it is a structure holding the object data.

When a Reference type is passed by value the contents of their memory location are also passed but in this case it is a pointer to where the actual data for the type is stored. Normally this would mean the passed value could be to be used to modify the actual value and the caller would see any changes to that value, and in fact this is true for Arrays and other Objects.

However in B4x/Java (and C#, Python and many other languages) Strings are given special treatment by the language compiler to give them "value semantics". What this means is that Strings behave like they are value, rather than reference types. The compiler achieves this by ensuring that String values are immutable - they cannot be changed. Any change to a String results in a new String being built rather than the existing String being modified. The pointer to the location of the new String is then stored as the reference to the String. This means that in the case of a passed String reference the passed reference is changed in the memory location of the passed parameter variable but the original String variable location still contains a pointer to the original String value.
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
All declared variables are associated with a particular location in memory that serves to hold their value.

All primitive types are Value types, except Strings. This means that their location in memory actually holds their value. When passed to a procedure by value a copy of the contents of their memory location are passed.

Strings, Arrays and other Objects, are actually Reference types. This means that their location in memory is actually a reference (pointer) to another location in memory where their actual value is located. For Strings this is a sequence of characters, for Arrays this is a sequence of values of the declared types, for Objects is is a structure holding the object data.

When a Reference type is passed by value the contents of their memory location are also passed but in this case it is a pointer to where the actual data for the type is stored. Normally this would mean the passed value could be to be used to modify the actual value and the caller would see any changes to that value, and in fact this is true for Arrays and other Objects.

However in B4x/Java (and C#, Python and many other languages) Strings are given special treatment by the language compiler to give them "value semantics". What this means is that Strings behave like they are value, rather than reference types. The compiler achieves this by ensuring that String values are immutable - they cannot be changed. Any change to a String results in a new String being built rather than the existing String being modified. The pointer to the location of the new String is then stored as the reference to the String. This means that in the case of a passed String reference the passed reference is changed in the memory location of the passed parameter variable but the original String variable location still contains a pointer to the original String value.
In fact it is more precise to say that the variables of primitive type (int, byte, float, String, ..) are passed by value. The others for reference.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
What I state is exactly true and precisely what occurs. Strings are not primitive types as you suggest. Other types are not passed by reference as you state. All types are passed by value. For value types it is an actual value, for reference types the value is a reference to the actual data. Strings have special treatment to make them behave like a value type even though they are reference types under the hood.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
What I state is exactly true and precisely what occurs. Strings are not primitive types as you suggest. Other types are not passed by reference as you state. All types are passed by value. For value types it is an actual value, for reference types the value is a reference to the actual data. Strings have special treatment to make them behave like a value type even though they are reference types under the hood.
I just simplified by saying that they are considered primitives.
But it's correct what you wrote ... maybe a bit verbose but correct :D
 
Upvote 0

j_o_h_n

Active Member
Licensed User
Thanks guys,
Agraham's answer wasn't verbose at all, it was comprehensive, clear and exact.
I think I understand how it works now.
 
Upvote 0
Top