0

So in class my teacher was saying that both lines 2 and 3 pass in a reference to an object.
I asked her why isn't line 3 passing in an object directly and I wasn't very convinced by her argument.
Can you please tell me whether she was right or wrong, and provide some sort of proof for either case?

JFrame frame = new JFrame();        //line 0
JLabel label = new JLabel("hello"); //line 1
frame.add(label);                   //line 2
frame.add(new JLabel("goodbye"));   //line 3
Kacy Raye
  • 1,312
  • 1
  • 11
  • 14

3 Answers3

4

Java only passes Object references, that said, the references are actually passed by value, which might be confusing. There is no such thing as "passing in an object directly" in Java.

What is being passed in is a value that represents the reference, in this case line 3 is unnamed or anonymous, in the scope of the method, its name is the name of the method parameter that represents that reference.

  • Okay I think I get what you're saying. So the variable "label" is just a name for the reference. And in line 3, my reference is just unnamed. Just so I'm 100% clear, are you saying that line 3 has no value? I'm slightly confused by the term. Is the value the variable name? – Kacy Raye Apr 17 '13 at 17:37
  • @KacyRaye Imagine you write `JLabel label = new JLabel()` and that the new JLabel is created and placed in memory at address 123456. Then `label` really holds the value 123456. And when you call `someMethod(label)`, it really is a call to `someMethod(Address:123456)`, meaning that someMethod has no access to `label` itself, only to the Object at Address 123456. If that makes sense... – assylias Apr 17 '13 at 17:42
  • Oh okay that totally makes sense. Pass by reference is kind of like saying pass by "address" lol... – Kacy Raye Apr 17 '13 at 17:44
3

Everything in Java is passed by value, even references. On line 2, when you pass in label, a new reference is created which points to the same object as the one label points to. On line 3, something similar happens; it's just that it's not explicit.

Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
  • It was down-voted because of semantics (not mine by the way). In general everything in Java is "passed by reference" meaning which you would think means `void example(Object & ref)` however this really isn't the case for primitives which are passed by value. Or simply put-> `void example(Mutable x){ //you can change x}` but `example(Immutable y){ //You can't change y because the contract doesn't allow it};` The above statement is misleading. Objects are always passed by a copy of the reference meaning you can't swap x/y. – Daniel B. Chapman Apr 17 '13 at 17:29
  • Daniel, there is no problem with semantics here. Please [read this](http://javadude.com/articles/passbyvalue.htm). – Sanjay T. Sharma Apr 17 '13 at 17:34
  • @DanielChapman Huh no - the accepted semantics is that everything in Java is passed by value (whether the variable is a primitive or a reference). – assylias Apr 17 '13 at 17:35
  • I hate when I have to pick between two good answers. I still gave it an upvote because it definitely helped the concept "click." – Kacy Raye Apr 17 '13 at 17:46
  • http://stackoverflow.com/questions/40480/is-java-pass-by-reference it is the same question and I raise the same complaint. If you mean a C++ "reference" then it is absolutely pass by "value". If you're talking "references" in this sense `http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ref/WeakReference.html` the semantics are different. – Daniel B. Chapman Apr 17 '13 at 17:51
0

It can be done either way:

frame.add(new JLabel("hello"));     //line 2
frame.add(new JLabel("goodbye"));   //line 3

Or:

JLabel label = new JLabel("hello");
JLabel label2 = new JLabel("goodbye");
frame.add(label);    //line 2
frame.add(label2);   //line 3

Either way, the reference to the object is passed. In the first version, the object reference is created as an argument. You just don;t have to store the reference in a variable, it can be passed right away. I think your teacher was right. Better bring her an apple.

penner
  • 2,707
  • 1
  • 37
  • 48