Android Tutorial Using and understanding Pointers and References

Yes, you heard me right! It is possible to utilize the pointers and references (at least in terms of behavior) in B4X Java-based languages (B4A and B4J). :D

There is a lot to explain about pointers and references but if you allow me to summarize and simplify, I'll do it in the following way:

Imagine you went to your favorite a hotel and you asked for a room.
Since the room itself is a huge heavy object, it's completely unthinkable that you would be given "the room" itself.
What you get instead is a key to that room! :)

A pointer is just like the room key. You may copy it, destroy it or even modify it (so that it opens another room).

A reference is like the room number. If you trash room 303, then you effectively trash room number 3 on the third floor of your favorite hotel (and the manager won't be happy!).

C-Reference-vs-Pointers-1.jpg

Alright, now that you get the point, how can we use it in B4X(A/J)?

Simple! :) Converting things to Object works just like a pointer, while copying non-primitive object types works as a reference.
Which one to use, it's up to you and what your code demands.

Self-explanatory example:
B4X:
Dim myOriginalMap As Map
Dim myOriginalList As List

'References
'----------------------------------------------------------
    myOriginalList.Initialize
    myOriginalList.Add("A")
    myOriginalList.Add("B")
    myOriginalList.Add("C")

    myOriginalMap.Initialize
    myOriginalMap.Put("list", myOriginalList)

    For Each value As String In myOriginalList
        Log(value)
    Next 'No surprises here...
 
    Log("---")

    Dim myMapReference = myOriginalMap As Map
    Dim myListReference = myMapReference.Get("list") As List
    myListReference.RemoveAt(1) 'Removes "B" from the list

    For Each value As String In myOriginalList
        Log(value)
    Next 'The original list was modified!!!
 
    Log("---")

    myListReference.Clear
    For Each value As String In myOriginalList
        Log(value)
    Next 'Will this work???
    If myOriginalList.Size == 0 Then Log("Oops!! We have just destroyed the original list!")

    Log("===================================")


'Pointers
'----------------------------------------------------------
    myOriginalList.Initialize
    myOriginalList.Add("A")
    myOriginalList.Add("B")
    myOriginalList.Add("C")

    myOriginalMap.Initialize
    myOriginalMap.Put("list", myOriginalList)

    Dim myMapReference = myOriginalMap As Map

    Dim myListPointer   = myOriginalList As Object 'Creates the pointer.
    Dim myListReference = myListPointer  As List   'Dereferences the pointer back into a list

    For Each value As String In myListReference
        Log(value)
    Next 'So far so good...

    Log("---")

    'Let's destroy the pointer!
    myListPointer = Null 'Destroying the pointer affects neither the reference nor the original :)
    Log("myListPointer is Null (should be true): " & (myListPointer == Null))
    Log("myListReference.Size (should be 3): " & myListReference.Size)
    For Each value As String In myOriginalList
        Log(value)
    Next 'The original list was kept intact.

    Log("---")

    'Remember that destroying a reference is the same as destroying the original!
    myListReference.Clear
    Log("myOriginalList.Size (should be zero): " & myOriginalList.Size)
    For Each value As String In myOriginalList
        Log(value)
    Next 'The original was modified because we're dealing with a direct reference.

Happy coding!! :)
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
So... You trashed the man's hotel room!?
 
Top