+ 5

How to pass by "reference" Java

I'm coming from C++ background programming and, it's easy to pass by reference a variable to change it inside the function. Just doing: void foo(int& n) { ++n; } But, in Java I can't find an easy way to do it without importing another things. I know that in java is pass by value so, any primitive type will be passed by value. I'm interested in primitive types. I googled and stack overflow offers some solutions: https://stackoverflow.com/questions/4319537/how-do-i-pass-a-primitive-data-type-by-reference My teacher used a Length 1 primitive array (check stackoverflow link) but, is dirty for me. The other solutions doesn't convinced me too much... Is there a fast and elegant way to pass a primitive data type by reference? And if not, for Java programmers veterans, what's the best way to pass by "reference" a primitive data type?

11th Mar 2021, 7:34 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
8 Antworten
+ 3
You simply can't. You always need to use a reference data type that is mutable. The best way to do this is using the AtomicInteger that is thread-safe and works with an inner primitive int. This way you can easily retrieve the value as int without ever passing an int. Note that you also have AtomicLong, AtomicDouble and other primitive specialisations. The class is in java.util.concurrent.atomic package. https://code.sololearn.com/cuwKKwqlBvuA/?ref=app
11th Mar 2021, 7:43 AM
Soumik
Soumik - avatar
+ 2
Adam McGregor Eduardo Perez Regin ♨ Soumik 🎶 000 Primitives wrappers like Integer, Long... can't be used because are inmutable. Attending to software design its better use atomics like AtomicInteger, AtomicLong, AtomicReference<T>... to early prevent that later unary operations will be thread-safe, although synchronization its also recommend... Here a case where I had a headache 😥: https://code.sololearn.com/cn9mN7Eo3CMe/?ref=app You also can mark the attribute as volatile and using double race condition guard software pattern to get right fresh value between Threads. Here some examples with singletons https://code.sololearn.com/cLCvHCdTR2bh/?ref=app https://code.sololearn.com/cq9I0LzVfGv2/?ref=app Your bible about this: https://www.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java This only covers basic cases like counters, single initialization... For more complex structures I recommend using other objects provided in java.util.concurrent package like Lock, Semaphore, Barriers...
15th Mar 2021, 9:05 AM
David Ordás
David Ordás - avatar
+ 1
There is only call by value in java, not call by reference. If we call a method passing a value, it is known as call by value. The changes being done in the called method, is not affected in the calling method.
11th Mar 2021, 9:28 AM
R YUVARAJ
R YUVARAJ - avatar
+ 1
Adam McGregor But how are you gonna change the value if you use wrappers?
11th Mar 2021, 11:48 AM
Soumik
Soumik - avatar
+ 1
https://code.sololearn.com/cVQ9SWwulnNo/?ref=app Well , may this could help you...GUY...
11th Mar 2021, 4:39 PM
000
000 - avatar
0
♨ Soumik 🎶 I was also thinking in using AtomicInteger, which I feel it a straightforward answer of doing a simple thing cleaner. Maybe because I'm used to do it in C/C++ style I was trying to find a cleaner way to do it. Thanks for answer!
11th Mar 2021, 7:59 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
0
Eduardo Perez Regin You're Welcome.
11th Mar 2021, 8:01 AM
Soumik
Soumik - avatar
0
Adam McGregor Yep! That's just what I was thinking of. I thought you meant the default wrapper classes in java because they are of no use when trying to pass by reference.
11th Mar 2021, 1:30 PM
Soumik
Soumik - avatar