1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question Passing strings into procedures

Discussion in 'Android Questions' started by j_o_h_n, May 9, 2019.

  1. j_o_h_n

    j_o_h_n 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.


    Code:
    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.
     
  2. Star-Dust

    Star-Dust Expert Licensed User

    The passing of parameters is by value values, not by reference, so you can't do it.
    Edit so:
    Code:
    Sub sub1(sP As Stringas String
        
    Return sP & "b"
    End Sub

    Dim s As String = "a"

    S=sub1(s)

    Log(s)  'see ba
     
    j_o_h_n and BillMeyer like this.
  3. agraham

    agraham Expert Licensed 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: May 10, 2019
    j_o_h_n likes this.
  4. Star-Dust

    Star-Dust Expert Licensed User

    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.
     
  5. agraham

    agraham Expert Licensed 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.
     
  6. Star-Dust

    Star-Dust Expert Licensed User

    I just simplified by saying that they are considered primitives.
    But it's correct what you wrote ... maybe a bit verbose but correct :D
     
  7. j_o_h_n

    j_o_h_n 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.
     
  8. agraham

    agraham Expert Licensed User

    As well it ought to be! After all I have over fifty :( years experience of doing this :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice