2

I read some code here: Is Java "pass-by-reference" or "pass-by-value"?

public void foo(Dog d)
{
  d.getName().equals("Max"); // true
  d.setName("Fifi");
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Fifi"); // true

Can I perform the same with the Byte object. I am at this point in my code and wondering how to "set" the value of the byte object?

If i use the = assignment operator it seems to perform the new Byte() autoboxing?! and therefore the value is not passed back.

Any ideas? Regards.

Community
  • 1
  • 1
Danny Rancher
  • 1,923
  • 3
  • 24
  • 43

2 Answers2

6

Byte is immutable, which means its value cannot be changed. Assigning to it won't work in your case, since that would simply rebind the reference (which won't propagate back to the caller).

You could use MutableByte, a one-element byte/Byte array, or a custom class.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
-2

The previous answer is correct, but just to add - yes in this case the Byte object is passed by reference. However because the Byte object is immutable even though it is passed by reference there is no way to modify it.

To be more specific when you call a function the parameters into the function are passed by value, but when you pass an Object what you actually pass by value is a reference to that object.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • 3
    No, it's **not** pass by reference. Ever. – Brian Roach Jan 16 '14 at 21:46
  • 2
    Java is NOT pass by reference! – Roman Vottner Jan 16 '14 at 21:46
  • You pass a reference by value. You are still passing the Byte by reference. – Tim B Jan 16 '14 at 21:47
  • I'll reword if it makes you happier. – Tim B Jan 16 '14 at 21:47
  • The Byte is passed by reference. It's all a matter of terminology. The mechanics behind the scene may pass by value but from the point of view of someone calling a method with an object they are passing by reference. Yes I agree there are subtle differences when it comes right down to it but that's a big discussion of its own. http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Tim B Jan 16 '14 at 21:52
  • 1
    In particular this comment: I believe that much of the confusion on this issue has to do with the fact that different people have different definitions of the term "reference". People coming from a C++ background assume that "reference" must mean what it meant in C++, people from a C background assume "reference" must be the same as "pointer" in their language, and so on. Whether it's correct to say that Java passes by reference really depends on what's meant by "reference" – Tim B Jan 16 '14 at 21:53
  • 2
    @TimB The reason people are pedantic about this is because it leads to *exactly* the question the OP is posting. If it were call by reference you could assign a new object to the local alias and that would be visible in the calling block. Basically, allowing what the OP is describing they tried: assigning a new `Byte` to a local variable in a method and expecting it to change outside the method. – Brian Roach Jan 16 '14 at 21:59
  • (And yes, I know C++ doesn't allow reseating as I'm describing) – Brian Roach Jan 16 '14 at 22:00
  • Agree 100% with @BrianRoach. We need to be clear that Java is _always_ pass by value, whether it's passing primitives or references. – GriffeyDog Jan 16 '14 at 22:01
  • @TimB: What "pass by reference" means is separate from what "reference" means. Please clearly state your definition of "pass by reference" if you are claiming that you have some different definition of it than other people. Also, if you are claiming that this is pass by reference, then you must also view every time one passes a pointer to something in C++ (without `&`) and C as "pass by reference". – newacct Jan 17 '14 at 03:22
  • I already was specific in my answer so I don't know why you down-voted it again. Read the second paragraph, it is entirely clear and correct. The Object (in this case Byte) itself is passed by reference even though the reference is passed by value. After all are we or are we not passing a reference? – Tim B Jan 17 '14 at 09:44