Android Question [b4x] ByRef or Not ByRef - THAT is the question.

MrKim

Well-Known Member
Licensed User
Longtime User
My big takeaway from THIS post is (I think) that regular variables are passed by value and arrays by ref.
I did not know this.
So my question is: What about maps, lists, user defineds, etc.?
And is it always the same across B4A/i/J?
 

agraham

Expert
Licensed User
Longtime User
Actually everything is passed by value in Java and so in B4X. For a primitive that value is the quantity that the primitive represents. For a reference type the value is a reference to the object instance. This post tries to explain it.

 
Upvote 0

b4x-de

Active Member
Licensed User
Longtime User
It is easier to remember, if you distinguish between simple types that will always be passed by value (int, boolean, ...) and the rest, that is always passed by reference. The majority of the rest are objects (instances of classes), like the examples that you mentioned: maps, lits and "user defined" classes.

This Video on YouTube explains the details (unfortunately in German only):

 
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Actually everything is passed by value in Java and so in B4X. For a primitive that value is the quantity that the primitive represents. For a reference type the value is a reference to the object instance. This post tries to explain it.

Your statement seems contradictory to me. And this is coming from someone who is self-taught in the art of programming so I am probably missing some semantic subtlety here but why is a reference type considered passed by value when what is being passed is a reference?
(I read your link. It was helpful.)
 
Upvote 0

b4x-de

Active Member
Licensed User
Longtime User
When it comes to passing parameters of reference types vs. value types you might find information at 10:45 in this video:

 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
It's a concept thats hard to grasp.
If you want to try a few examples to reinforce your understanding try this.

1, Define an object (int, string, map, list, table ...)
2, Call a sub with the object( in #1 ) as the parameter.
3, Change the object in the sub (a = a + 1 etc)
4, Return the object from the sub
5, Compare the original to the returned value. If they are the same then it was passed by reference, if not it was passed by value.

B4X:
Dim a As Int = 1

if addOne(a) <> a then
  Log("passed by value")
else
  Log("passed by reference")
end if

Sub addOne(x As Int) As Int
   x = x + 1
   return x
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Read very carefully the post I linked to above. Every word is carefully chosen and no, reference types are NOT passed by reference in Java/B4X they are passed by value. The fact that a reference is being passed does NOT make it "pass by reference". Each word below is also carefully chosen..

Consider a variable holding an instance of a reference type.
A variable is an address in memory
The contents of that address hold a value
That value for a variable is the address in memory where the instance of that reference type is located
Pass by value passes the value of the variable - which happens to be a reference to the object instance for a reference type
That object instance can be manipulated by the receiver as it is a reference.

Pass by reference would pass a reference to the variable which would be its address, not the contents of that address
With the address of the variable you could not only access the value of that variable, the object instance, by using the value at that address but you could also change the value of that variable to reference a different instance of that reference type. This is usually undesirable as the caller, on return, would not know if that variable holds a reference to the same instance as when it was called or a different one. Therefore by default most languages pass by value, and in Java there is not even the capability to pass by reference.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I remember in VB6, we can use the keyword ByVal and ByRef or if the keyword is not specified, parameter is pass ByVal by default when declaring a function/method signature.
I like this experience. Isn't this good if it is also available in B4X?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Just a "fun fact" and a thing to remember: a variable declared as Object is passed by value.
ALL variables are passed by value as I explained above. An Object instance is no different to any other reference type in that the value passed is a reference to that Object location in memory. In fact there is no such thing as an instance of an Object, it is always a reference to a higher type instance that has been cast down to an Object which is the abstract base class of all reference types.

I like this experience. Isn't this good if it is also available in B4X?
It is not possible as Java does not support pass by reference as I have already been at some pains to point out above.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
I remember in VB6, we can use the keyword ByVal and ByRef or if the keyword is not specified, parameter is pass ByVal by default when declaring a function/method signature.
I like this experience. Isn't this good if it is also available in B4X?
Yes, that is what I remember.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
ALL variables are passed by value as I explained above. An Object instance is no different to any other reference type in that the value passed is a reference to that Object location in memory. In fact there is no such thing as an instance of an Object, it is always a reference to a higher type instance that has been cast down to an Object which is the abstract base class of all reference types.


It is not possible as Java does not support pass by reference as I have already been at some pains to point out above.
I am just not getting it. According to @Erel in the post motioned in my first post if you pass a plain variable (Dim A as int) it will not change if you modify it in the sub but if you pass an array (Dim A(1) as int) then it will change.

Let's try a different tack. Can you explain to me the difference between a reference type as you explained above being passed to a sub and a (hypothetical) passing of a value by ref?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm getting lost.

I know that @agraham is very expert and most likely he also fiddles ? with the Java sources generated by B4X and I trust him a lot. Most likely I don't understand exactly what he means.

If he means that of an object to a method the reference to the memory area that contains the value of the object is passed, i.e. a pointer, as in C, then I agree.
This, however, does not contradict this statement, which is useful to remember how things work:
It is easier to remember, if you distinguish between simple types that will always be passed by value (int, boolean, ...) and the rest, that is always passed by reference.
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
"Pass by value" and "Pass by reference" are strict computer science terms as to what is passed as a parameter to a method call. "Pass by value" passes the value of a variable and "Pass by reference" passes the address of the variable. What you are referring to as "passed by reference" is better expressed as "passed as a reference" which is what happens when a variable containing a class instance is "passed by value" in computer science terms. Otherwise you are confusing the definition of HOW the pass is done with the RESULT of how the pass is done.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I am just not getting it. According to @Erel in the post motioned in my first post if you pass a plain variable (Dim A as int) it will not change if you modify it in the sub but if you pass an array (Dim A(1) as int) then it will change.

Let's try a different tack. Can you explain to me the difference between a reference type as you explained above being passed to a sub and a (hypothetical) passing of a value by ref?
Agraham has already explained it very clearly in post#2, post#7, post#10. (Dim A as int) is the primitive type, (Dim A(1) as int) is the non-primitive type.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
"Pass by value" and "Pass by reference" are strict computer science terms as to what is passed as a parameter to a method call. "Pass by value" passes the value of a variable and "Pass by reference" passes the address of the variable. What you are referring to as "passed by reference" is better expressed as "passed as a reference" which is what happens when a variable containing a class instance is "passed by value" in computer science terms. Otherwise you are confusing the definition of HOW the pass is done with the RESULT of how the pass is done.
Do we want to "cut the bull's head" (bad way of speaking, I don't know if only Italian)?
VB - xxx ByRef
You call that method by writing the name of a variable that contains an object instance (more briefly: an object variable) but the pointer, the reference, the "address to the contents of the variable - of the instance" is passed.

Do we agree on this?

And in any case we would understand each other much better if you all started studying... Italian! ?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Do we agree on this?
There is no need for agreement. What I have written is 100% accurate. Just read it very carefully as all the words have been carefully chosen to say exactly what is happening. In programming it is important to understand what is actually happening as using wrong assumptions means that you will find it impossible to analyse why bugs are occurring.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It is easier to remember, if you distinguish between simple types that will always be passed by value (int, boolean, ...) and the rest, that is always passed by reference.
I hope you don't mean that the statement above (post #3) is wrong.
At most, terms were used that were not entirely precise but exactly as they are in the VB - ByRef declarations.
It is implied or intuited/understood that the reference is being passed.
 
Upvote 0
Top