Jump to content

Pass by value, by reference

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
I am very confused about this thing in java about pass by value and pass by reference , I know that all the primitive data types are passed by value and objects are passed by reference , but can anyone please explain it with a bit of example please?

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
     Cat firstCat = new Cat()
     
     YourMethod(firstCat); // Cat's reference is being passed here
     Cat secondCat = firstCat; // Cat's reference is being passed here


     int i = 5;
     
     anotherMethod(i); // primitive data type is passed by value
     int e = i; // also passed by value.

If you pass something by reference then you are just passing the address, and object stays as one, so you can't change it without changing it for others.

If you pass value, it will be copied, and results are independent from each other.

#3
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
I know about the concepts but just messed up cause I am studying Java course in my university so sucks to switch from C++ to Java in the beginning ,So what about the double data type in java , it's passed by value others are passed by reference , why?


Solved the problem understood it ;) thanks anyways

Edited by ahmed, 27 October 2009 - 02:06 AM.


#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
When you pass a primitive variable, it is passed by copy. So modifying the copy in the method will not change the variable outside the method.

When you pass an object, it is passed by reference. Thus if you modify the object in the method, it will be updated in the method that called it as well.

#5
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
one question , if i want to pass a primitive variable by reference how can I do it? I can't seem to do it with & operator cause it's not in Java i think

#6
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
That would require pure pointers to memory, but java doesn't allow that for primitives(as well as i know...)

The reason you can't pass primitives by reference, is that you don't need that...ever. Java has static variables for that, and in case you need to change the original primitive, you can always return the value from a function..

#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Also, Java has all of the primitives as objects, so if you really need to pass primitives by reference, simply box them into an object and pass them into the function. :)
Wow I changed my sig!