Open Side Menu Go to the Top

07-17-2013 , 01:55 PM
Quote:
Originally Posted by tyler_cracker
you (potentially/arguably) left off foreach, goto, and duff's device.
Yeah I'm a programming fish
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
07-17-2013 , 02:33 PM
Quote:
Originally Posted by jjshabado
My old company asked "Does Java do variable passing by reference or by value?"

Hilarious arguments came out of that. It's actually a great meta-interviewing question. Before someone should be allowed to interview they should be asked what a good answer to the above is. Anyone that feels strongly about one or the other answer - without listening to the explanation given - shouldn't be an interviewer.
I'm a Java programmer and this still confuses the hell out of me.

I know if the variable is a primitive then it gets passed by value.

However, if the variable is an object I always believed it was passed by reference, but I guess it's a little trickier than that. Supposedly, Java objects are passed as references passed by value.

Any pointers on understanding that would be appreciated as I'm also honing up to interview.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 02:35 PM
Quote:
Originally Posted by tyler_cracker
based on your explanation, i'll upgrade that to a poor example of inheritance. your mental model is flawed here as you are conflating an instance with a subclass.
What about one of those combo KFC/pizza hut stores? Although I suppose you could have gone super literal and used children getting traits from their parents.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 03:00 PM
Quote:
Originally Posted by Jbrochu
I'm a Java programmer and this still confuses the hell out of me.

I know if the variable is a primitive then it gets passed by value.

However, if the variable is an object I always believed it was passed by reference, but I guess it's a little trickier than that. Supposedly, Java objects are passed as references passed by value.

Any pointers on understanding that would be appreciated as I'm also honing up to interview.
You've got it.

Primitives are passed by value.
Objects are passed by reference.

But if you want to get fancy you can talk about how passing the object really works by passing the reference of the object by value. This is 'important' in that it explains why you can't change a method parameter to point to a new object outside of the method scope.

So if I did something like:

Code:
Object foo = new Integer(1)
someFunc(foo);

private void someFunc(Object foo) {
  foo = new Integer(2);
}
foo is still going to point to the Integer(1) after the someFunc call is finished.

But if I had modified foo (instead of creating a new object) the changes would have been visible after someFunc was called.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 03:03 PM
Quote:
Originally Posted by Jbrochu
I'm a Java programmer and this still confuses the hell out of me.

I know if the variable is a primitive then it gets passed by value.

However, if the variable is an object I always believed it was passed by reference, but I guess it's a little trickier than that. Supposedly, Java objects are passed as references passed by value.

Any pointers on understanding that would be appreciated as I'm also honing up to interview.
Everything is passed by value in Java.

Primitives are passed by value
Object references are passed by value

There is no concept of having a handle on the actual object in Java. You can only access objects through references. This is conceptually different than C++ where it's possible to manipulate objects through both references (and pointers) and direct object variables. Object reference is more or less a pointer, so the pointer is passed by value to functions.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 03:17 PM
Quote:
Originally Posted by kerowo
What about one of those combo KFC/pizza hut stores?
Problematic imo and better modeled as a collection of the individual stores. If you really want to force it, is the KFC/PH a KFC extended with PH behavior, or a PH extended with KFC behavior? Just not a natural fit.

A car with some kind of special add on like a turbo might be an answer, but honestly object inheritance and real world inheritance just aren't great analogs. Even parents / children don't fit well. Class inheritance is a different story, but in that case the analog is our mental model of the world, where we really do classify things using is-a relationships.

But with real world objects, when one has all the behavior of another, plus some of its own, I usually find the decorator pattern, or template pattern, are better analogs than object inheritance.

Last edited by gaming_mouse; 07-17-2013 at 03:32 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 03:25 PM
Thanks guys.

Still confused a bit by what "object references are passed by value" really means. I've never worked with pointers so maybe that's part of my issue.

I'll have to make some time in the next few days to write a little bit of code and examine the objects with the debugger. That will probably resolve it for me.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 03:29 PM
Jbrochu,

Essentially, you are passing an identifier which contains the location of the object in memory. That's why, in Java, if you pass an object to a method, and that method makes changes to the object, the changes persist even after you've returned from the method. The method was using the reference to find and make changes to the one and only copy of the object in memory.

In contrast, if you pass in an int, or a double, a copy is made, and any changes only affect that copy.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 03:44 PM
Quote:
Originally Posted by gaming_mouse
Jbrochu,

Essentially, you are passing an identifier which contains the location of the object in memory. That's why, in Java, if you pass an object to a method, and that method makes changes to the object, the changes persist even after you've returned from the method. The method was using the reference to find and make changes to the one and only copy of the object in memory.

In contrast, if you pass in an int, or a double, a copy is made, and any changes only affect that copy.
Thanks. That part totally makes sense to me.

But then I would say primitives are passed by value and objects are passed by reference. But the Java literature says both primitives and objects are passed by value.

The "object references are passed by value" bit is the part that gives me trouble. It's like technically yes,they're both passed by value; but in reality it's as if objects behave (for the most part) like they were passed by reference.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 03:57 PM
Quote:
Originally Posted by Jbrochu
The "object references are passed by value" bit is the part that gives me trouble. It's like technically yes,they're both passed by value; but in reality it's as if objects behave (for the most part) like they were passed by reference.
jj explained it above. it's sort of semantics. so lets say I pass an object "o" into a method. further assume that o is located at memory location "23059209564". So what actually happens is that a copy of "23059209564" is passed into the method. That copy is then used to lookup the object when you use the variable inside the method.

You might say it like this:

All objects are passed by reference. But the implementation of reference passing involves passing by value the memory location.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 04:12 PM
There is a piece of what jj wrote that I still need to figure out but my browser keeps freezing... I'll try again later.

The console reports this: Blocked a frame with origin "http://axp.zedo.com" from accessing a frame with origin "http://forumserver.twoplustwo.com". Protocols, domains, and ports must match.

Originating from: aceUAC.js

Anyone else's browser freezing? It's only happening in this thread for me not the rest of 2+2.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 04:18 PM
I'm getting the same problem.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 04:21 PM
Quote:
Originally Posted by Jbrochu
Thanks. That part totally makes sense to me.

But then I would say primitives are passed by value and objects are passed by reference. But the Java literature says both primitives and objects are passed by value.

The "object references are passed by value" bit is the part that gives me trouble. It's like technically yes,they're both passed by value; but in reality it's as if objects behave (for the most part) like they were passed by reference.
Read muttiah's answer - it's very good.

Another way to think about this is: what would count as objects behaving like they were passed by value, holding all other Java semantics equal?

Also, what does a = b do in Java?

If a and b are both object types?

If a and b are both primitive types?

What does this say about the value of variable x where x is an object type?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 04:22 PM
Quote:
Originally Posted by gaming_mouse
jj explained it above. it's sort of semantics. so lets say I pass an object "o" into a method. further assume that o is located at memory location "23059209564". So what actually happens is that a copy of "23059209564" is passed into the method. That copy is then used to lookup the object when you use the variable inside the method.

You might say it like this:

All objects are passed by reference. But the implementation of reference passing involves passing by value the memory location.
Creating, atleast in C#, the situation where if you do something like o = new object(); nothing happens to the original object, it remains unchanged upon return. since o is a local variable.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 04:23 PM
I don't really have a problem with the Java passes everything by value statement, but I think its a weird way of saying what happens. In Java whenever we talk about Objects we're generally talking about the object itself - the methods and variables that make up the instance.

So when we talk about how Objects are passed in Java it seems reasonable to think about how is that collection of methods/variables passed. Which is by reference - that is the address of the collection of those things is passed.

Saying that everything is passed by value means that we're now considering Objects as a bunch of stuff at a specific memory location. But everywhere else in Java the idea of this memory location is hidden from us - so why start using this way of thinking about it?

And, just to be clear, when I asked this question I didn't care about the high level phrasing used. I cared much more about seeing an understanding of how it actually works.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 04:23 PM
Quote:
Originally Posted by jjshabado
I'm getting the same problem.
which browser? Firefox working fine for me
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 04:25 PM
Chrome. It happens on and off - there's a slow js script that is probably associated with a specific ad (or ad server).
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 05:07 PM
Would talking about the stack and the heap help with the conversation on reference vs value or just add more complexity?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 05:11 PM
When I use to cover this with students I always found it helped to draw out the stack and heap and use box and arrow diagrams to show the difference between copying a box and copying an arrow.

But that was with people that already had at least some understanding of how the stack/heap worked.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 05:16 PM
I would be interested in knowing more about stack/heap allocation in Java, although it isn't clear to me why it would be directly relevant to value vs reference passing. Also I would assume that pretty much any object reference generated with new would be on the heap, but I have no idea
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 05:34 PM
question to any BS CompSci folks - did you have to take calculus at college? if so, how much? also if so, now that you are gainfully employed, how much does your calc knowledge come into play during your programming duties?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 06:24 PM
This entire value vs reference conversation is exactly why I recommended to learn C first (cs50) a few pages back because that entire concept is flawlessly explained and could be applied to java.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 06:31 PM
no one's arguing that learning C first has *no* advantages, so their existence doesn't mean much. the argument is that all things considered it's not the optimal way to learn. and it's the reason that few intro to programming courses are taught in C anymore.

EDIT: although in fairness that particular course does look pretty good
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 06:36 PM
Quote:
Originally Posted by Low Key
question to any BS CompSci folks - did you have to take calculus at college? if so, how much? also if so, now that you are gainfully employed, how much does your calc knowledge come into play during your programming duties?
Still in school. Had to take Calc I and II, along with a statistics course that used a bunch of calculus.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
07-17-2013 , 06:48 PM
Quote:
Originally Posted by jjshabado
Chrome. It happens on and off - there's a slow js script that is probably associated with a specific ad (or ad server).
Chrome for me too. It's definitely an ad or ad server.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

      
m