pass by reference in java

is there a facility of "pass by reference" in java

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

47 Replies - 10490 Views - Last Post: 01 September 2009 - 03:37 PM Rate Topic: -----

#1 k.abhinav1301  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-April 09

pass by reference in java

Post icon  Posted 01 September 2009 - 02:46 AM

:rolleyes:
respected sir/madam,
i just want to know that according to some of the renowed authors there is the concept of "pass by reference in java" but according to SUN MICROSYSTEMS there i no concept of "pass by reference in java".
so please help me to go to the real concept behind this.

Is This A Good Question/Topic? 0
  • +

Replies To: pass by reference in java

#2 ladyinblack  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 9
  • View blog
  • Posts: 419
  • Joined: 08-April 09

Re: pass by reference in java

Posted 01 September 2009 - 03:00 AM

Hi,

I've never really delved much into this concept but then I guess you would need to the theory stuff when it comes to studying.

Have a look at this little piece of info, hope it clarifies some things for you.
Was This Post Helpful? 0
  • +
  • -

#3 ivey.eli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 196
  • Joined: 11-April 09

Re: pass by reference in java

Posted 01 September 2009 - 03:08 AM

I think Java is strictly pass by value, but because the values of some things (structured objects) are references, it's kinda like passing by reference (at least with objects)

There are some good write-ups of this stuff on the net so I recommend you use Google to find a good explanation :)
Was This Post Helpful? 0
  • +
  • -

#4 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: pass by reference in java

Posted 01 September 2009 - 03:16 AM

Id say its a hybrid of pass by value and reference.
Was This Post Helpful? 0
  • +
  • -

#5 ivey.eli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 196
  • Joined: 11-April 09

Re: pass by reference in java

Posted 01 September 2009 - 03:19 AM

Well.... it isn't :)

" Java is strictly pass-by-value, exactly as in C. Read the Java Language Specification (JLS). It's spelled out, and it's correct. In http://java.sun.com/...sses.html#8.4.1 "
Quote from http://javadude.com/...passbyvalue.htm

This post has been edited by ivey.eli: 01 September 2009 - 03:21 AM

Was This Post Helpful? 1
  • +
  • -

#6 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: pass by reference in java

Posted 01 September 2009 - 03:27 AM

Im not saying its not pass-by-value because it is, yet it does implement pass-by-reference concepts as well, does it not? Thats what i meant by hybrid. Yet its hard to divide each programing language in categories and thats what were trying to do. Its just like stereotyping people dividing them into groups.

Anyways as long as you can program properly in this language it doesnt really matter that much.
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: pass by reference in java

Posted 01 September 2009 - 03:53 AM

there's no pass by reference in java, everything is pass by value, even if you pass a reference type to a method, passing is done by value because the reference itself is not passed in the method but a copy of the reference is passed...
Was This Post Helpful? 0
  • +
  • -

#8 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: pass by reference in java

Posted 01 September 2009 - 04:24 AM

I just give up on this one, i think the whole structure of language as it is: is just a reference to something. Word just references an object either in life or virtual world.
Its just an address, a tag.

But well, i guess you guys probably know better whats the case here...
Was This Post Helpful? 0
  • +
  • -

#9 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,170
  • Joined: 12-May 09

Re: pass by reference in java

Posted 01 September 2009 - 05:24 AM

I'm actually kind of surprised by these responses. Try passing an object such as an ArrayList into a method in java, and then add an item to it. After the method is exited, check what's in the ArrayList. There's your answer.
Was This Post Helpful? 0
  • +
  • -

#10 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: pass by reference in java

Posted 01 September 2009 - 06:44 AM

It is not strictly pass by value, when you pass by value its going to copy any object you pass.

I believe it is called passing the reference by value.

It's pretty much passing a C/C++ pointer by value. You can assign a different value to the pointer itself, but since it was passed by value it won't change the atual object. If you derefrence it (can't actually do this in java : /) and set that ( or in java set the properties) then it will change because the pointer in the calling method still points to the same place in memory.
Was This Post Helpful? 0
  • +
  • -

#11 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,170
  • Joined: 12-May 09

Re: pass by reference in java

Posted 01 September 2009 - 07:22 AM

The best way to put it is that while Java works by value at the lower level, objects in Java act like they're passed by reference. The reference to the object is passed by value, and any changes made to the object referenced happen to the object that was the argument.
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: pass by reference in java

Posted 01 September 2009 - 07:31 AM

But (for the record) it isn't passed by reference. The object is passed by value, but since it still points at whatever sent it, changes (such as passing in an ArrayList) apply, thus it emulates passing by reference.
Was This Post Helpful? 0
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: pass by reference in java

Posted 01 September 2009 - 07:40 AM

View PostKYA, on 1 Sep, 2009 - 08:31 AM, said:

thus it emulates passing by reference.


That's the word I was wondering if anyone would use here, because it's perfect. :)
Was This Post Helpful? 0
  • +
  • -

#14 Guest_Neumann*


Reputation:

Re: pass by reference in java

Posted 01 September 2009 - 08:11 AM

View Postxclite, on 1 Sep, 2009 - 04:24 AM, said:

Try passing an object such as an ArrayList into a method in java, and then add an item to it. After the method is exited, check what's in the ArrayList. There's your answer.


Sorry bud, can't pass objects in Java.
Was This Post Helpful? 0

#15 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: pass by reference in java

Posted 01 September 2009 - 08:26 AM

Can you not? I thought you can. That is if i understand what passing means to you correctly. If passing means doing this:
void receiver(Object a)
{
}

void sender()
{
Object b = new Object();
receiver(b);
}



Then yes you can! I mean ive done this countless times with strings and other wrapper classes such as Integer or Character.

If you mean something else then please enlighten me.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »