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?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.
Passing strings into procedures
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...www.b4x.com
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
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.
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.Just a "fun fact" and a thing to remember: a variable declared as Object is passed by value.
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 like this experience. Isn't this good if it is also available in B4X?
Yes, that is what I remember.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?
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.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.
Now I learn that there is a shortcoming in Java missing this useful feature.It is not possible as Java does not support pass by reference
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.
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.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?
Do we want to "cut the bull's head" (bad way of speaking, I don't know if only Italian)?"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.
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.Do we agree on this?
I hope you don't mean that the statement above (post #3) is wrong.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.