Is Java pass by value or pass by reference?

In C++ when should you pass by reference or by value?

  • Answer:

    You pass by value if the value is not to be modified by the function and the value itself may be calculated. So you would tend to pass by value the numbers that go into mathematical functions, for example sqrt(a*a+b*b). You pass by reference when the value is an output or both an input and output from a function, or when the value is very large (in which case a const reference will be used). For example a function that orders two parameters would necessarily need to have them passed by reference. It is not true that classes have to be passed by reference, or though it is normal. Everything is a class, so the fact you can pass somethings by value kind of proves the point. However you do need things like copy constructors to pass by value and when classes get large passing them by value both involves much effort to copy them and much stack space to store them.

Thinker at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

You should pass by reference when you are going to change a variable and by value when you are just going to read or use it in some sort of calculation. It's that simple For example if you have a sum(int , int ) function you can do a cout << sum(a,b); but you can also do a myvar=sum(a,b); You certainly don't have to send the sum back as a separate variable.

You pass by value if the value is not to be modified by the function and the value itself may be calculated. So you would tend to pass by value the numbers that go into mathematical functions, for example sqrt(a*a+b*b). You pass by reference when the value is an output or both an input and output from a function, or when the value is very large (in which case a const reference will be used). For example a function that orders two parameters would necessarily need to have them passed by reference. It is not true that classes have to be passed by reference, or though it is normal. Everything is a class, so the fact you can pass somethings by value kind of proves the point. However you do need things like copy constructors to pass by value and when classes get large passing them by value both involves much effort to copy them and much stack space to store them.

peteams

You should pass by reference when you are going to change a variable and by value when you are just going to read or use it in some sort of calculation. It's that simple For example if you have a sum(int , int ) function you can do a cout << sum(a,b); but you can also do a myvar=sum(a,b); You certainly don't have to send the sum back as a separate variable.

jplatt39

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.