-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Page 93, fine point
PostPosted: Thu Mar 17, 2005 10:53 am 
Newbie

Joined: Sat Sep 13, 2003 12:45 am
Posts: 12
Location: Rochester, NY
Section 3.5.1 states that "...all class instances are passed by reference. Only primitive types are passed by value". This isn't strictly true. In reality all method arguments in Java are passed by value. When classes are passed, a copy of the reference to the object is made and that copy of the reference is passed by value. In most cases the behavior you get is the same as passed by reference, but not all. A good explanation of this is contained in http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html. I have to believe that the authors really know this and thought that it would just confuse the point they were trying to make. I felt compelled to point this out because in my mind this is one of the top trick Java interview questions that people who like to do this type of thing try and trip you up with.

_________________
Jim


Top
 Profile  
 
 Post subject: Re: Page 93, fine point
PostPosted: Sun Mar 27, 2005 5:27 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
jcurry wrote:
"...all class instances are passed by reference. Only primitive types are passed by value"


I dont have the book in front of me, but I think the quote from the book is generally true. Im not trying to say that you are incorrect, or the article you posted, but if you are discussing Java with people who know the difference between value and reference and values of references (or just how the method stack works and assembly language), they probably wont correct this. If the authors got to the minute level of precision in the text where they need to explain this concept in a page instead of a sentence, then the focus of the book is not Hibernate anymore... I think it is fine. Chris

ps. I think the confusion is there are multiple interpretations of "by reference" and "by value".

Generally "by value" means a method can't operate on the caller's scope.
Generally "by reference" means a method can operate on the caller's scope.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 02, 2005 2:25 am 
Newbie

Joined: Sat Apr 02, 2005 2:17 am
Posts: 11
While there may be multiple interpretations of the meaning of "pass by reference" in common parlance, it's fairly well defined in terms of formal parameters and actual parameters.

Java does *not* have pass by reference, just like C doesn't (even though you can achieve similar semantics in C by passing a pointer, that's passing a pointer by value rather than passing the variable by reference). Claiming that instances are passed by reference is simply untrue. Objects aren't passed at all - for them to be passed either way, there would have to be some expression whose type is an object itself, and there is no such expression in Java.

Yes, many people will understand the difference or not care, but I've seen plenty of people get confused by this, expecting (reasonably) that when someone says that something is passed by reference, they really mean it in the formal sense. Conflating the two concepts also has the unfortunate side-effect that when people start using a language which *does* have pass-by-reference available (like C#) they often think that passing a reference type variable by reference is useless.

I agree that this isn't the topic of the Hibernate book, but I don't think it was necessary for it to be inaccurate. Even a footnote saying "Actually, references are passed by value, which gives similar semantics to pass by reference, even though they're not entirely the same" or something similar would have been okay.

I love the rest of the book, but this imprecision when it comes to parameter passing terminology is one of my bugbears :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 02, 2005 5:35 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I find the obsession of the typical Java programmer with pointer chasing quite interesting :)

I guess we'll just add a footnote in the second edition so everyone is happy. After all it was just an anology to help readers understand entities and value types.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 03, 2005 2:44 am 
Newbie

Joined: Sat Apr 02, 2005 2:17 am
Posts: 11
Oh it's definitely a helpful paragraph in general, once you understand the tiny inaccuracy.

I'm sure most people couldn't care less, to be honest - it's just something that I've had to explain to people several times on the newsgroups, so I'm more sensitive to it than most :)

Put it this way - if adding a footnote comes at the cost of anything else in the second edition, I wouldn't worry about it. If it's no problem at all though, a footnote would be great.

It's good to see authors being active on forums - I'm sure I'll be back with questions when I've finished the book. (There's a lot to take in for an ORM-newbie!)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 07, 2005 1:11 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
Yeah, I agree. I would prefer just to make it more precise (while keeping it concise) so you dont need a footnote, but either way it is fine with me.

From:

"...all class instances are passed by reference. Only primitive types are passed by value"

To:

"...all object instances are passed as a copy of the object reference. Only primitive types are passed as a copy of the data"

Kind regards,
Chris

ps. I changed "class" to "object" since the "class instance" (to me) refers to the singleton instance of the class object. Splitting hairs? :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.