Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 11-09-2003, 11:41 AM   #16 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 287
technobard is on a distinguished road
Just another two cents. Java does only pass by value. In the case of an object, the end result is the same as if it passed by reference, so in practice it doesn't really matter. The only place this is important is on the certification exam (which I have not gotten around to taking). I did take the official Sun training a couple of years or so ago, and the instructor and the reference material pretty much made this point. (I've also just checked the certification study guide in case my memory was faulty. It's in agreement.)
technobard is offline   Reply With Quote
Old 11-11-2003, 08:38 PM   #17 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
http://forum.java.sun.com/thread.jsp...ilite=false&q=
__________________
direct entry file specification.
npa is offline   Reply With Quote
Old 11-13-2003, 01:46 AM   #18 (permalink)
DarkTwilkitri
Registered User
 
Join Date: Feb 2003
Location: Australia
Posts: 21
DarkTwilkitri is on a distinguished road
This brings up a different question -
If passing the value of a reference is not passing by reference, then what is? Passing the value of a reference to the reference?

This is irrelevant to Java, so I'm going to have to switch gears here... I hope nobody minds...

In C/C++ (disclaimer: I actually know hardly any C/C++ - I may and probably will get something wrong during this), when you pass something to a function, whether it's a primitive or a class, it creates a copy and uses the copy. The original stays exactly the same.
You can also pass a pointer, such as an int*. This will copy the pointer, which is a reference to a memory location - very much like the object names in Java.
You can also use &variable, which is the 'pass by reference' syntax. But how does it pass by reference if not by passing a copy of the value of the reference? As I said before, the only way I can think of passing a reference without copying it is to create a reference to the reference - but that doesn't seem like an efficient way of going about things.
DarkTwilkitri is offline   Reply With Quote
Old 11-13-2003, 11:04 AM   #19 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
argh!

let's try to clear this up.

first, a reference IS a pointer. say it with me. a reference is a pointer. the only difference is the way the language presents them to the user.

a pointer is an unsigned integer of some sort (depends on your machine / compiler / language / pointer type). it's a memory address.

in c, there are no references. to a function, you can pass pointers or you can pass copies. if you type (&c), that means "the address of c"

in c++, the reference syntax was added. this is an 'implicit pointer'. when you declare a function that takes a reference, that means that it implicitly grabs the address (a pointer) of the parameter passed to it, instead of making a copy. this is pretty much a convenience feature for the programmer.

java mangles the c++ reference / pointer / stack variable concept. it takes all three and merges them into one concept and removes explicit pointers, and does several things in the background that the programmer is not necessarily aware of.

Code:
void test()
{
  MyClass m;
  // this is a pointer!  on the stack of this function, it is a 32bit integer
  // in c, this would be "MyClass *m" (pointer to MyClass)

  m = new MyClass();
  // 1) a new MyClass object is allocated on the heap
  //    in c: m = malloc(sizeof(MyClass));
  // 2) MyClass constructor is called
  //    c: m->MyClass();
  // 3) m is assigned the value of the newly allocated memory (m is now a pointer to this memory)
  // 4) the new object's 'reference count' variable in incremented.
  //    c: ((Object *)m)->ref_count++;

  m.val = 1;
  // this is an implicit pointer calculation
  // ((address stored in m)(+offset of 'MyClass::val')) = 1
  // c: m->val = 1;
  
  foo(m);
  // m (a pointer) is pushed onto the call stack.  function foo can now use it to reference that memory.

  return;
  // the reference count of the object pointed to by m is decremented
  // if reference count reaches zero, the memory is ready for deallocation by the garbage collector
  // c: ((Object *)m)->ref_count--;
}
joe_bruin is offline   Reply With Quote
Old 11-15-2003, 02:35 AM   #20 (permalink)
DarkTwilkitri
Registered User
 
Join Date: Feb 2003
Location: Australia
Posts: 21
DarkTwilkitri is on a distinguished road
Quote:
Originally posted by joe_bruin
argh!

let's try to clear this up.

first, a reference IS a pointer. say it with me. a reference is a pointer. the only difference is the way the language presents them to the user.
a pointer is an unsigned integer of some sort (depends on your machine / compiler / language / pointer type). it's a memory address.
Yes, I already got that part. Which may be one of the reasons I said 'the pointer, which is a reference' in my previous post...

Quote:
in c, there are no references. to a function, you can pass pointers or you can pass copies. if you type (&c), that means "the address of c"
That contradicts the whole pointer is a reference thing (there are no references. You can pass pointers. ...). In any case, I know that already too and said as much... unless I worded it badly...
A pointer is the memory address of x object. Passing a pointer is passing a copy of the value of the pointer, which really seems to be the same as passing the address of the object anyway - since the value of the pointer _is_ the address of the object. Passing anything which isn't a pointer will copy the whole thing.

I knew all this already

I'm unsure as to whether you understood my question.

Quote:
in c++, the reference syntax was added. this is an 'implicit pointer'. when you declare a function that takes a reference, that means that it implicitly grabs the address (a pointer) of the parameter passed to it, instead of making a copy. this is pretty much a convenience feature for the programmer.
See, this is what I was asking. How exactly does it just 'grab' the address without copying it ? Because it's seeming more and more as if there is no actual 'pass by reference', just passing by values but calling it different names...

Quote:
java
We cleared up Java already, as far as I know...
DarkTwilkitri is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Developer Position in Kansas City justplaindoug Java 0 10-29-2004 11:52 AM
JAVA Hobbyists Wanted emteam Java 0 08-29-2004 11:27 AM
IBM urges Sun to make Java open source redhead Code Newbie News 0 03-01-2004 10:39 PM


All times are GMT -8. The time now is 01:21 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting