-1

I'm new to programming. In layman terms, what does it mean when somebody says "Pass a value"?

I was told something like:

public Money (int cost)

Needs to pass a value and I don't understand what is meant.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ODor
  • 9
  • 3

3 Answers3

3

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.

take the example of that code :

public void Money (int cost)
{
 cost = cost + 2;
}

Note that the cost variable has 2 added to it. Now, suppose that we have some code which calls the method Money:

public static void main(String [] args)
{
int passing = 3;
Money (passing);
System.out.println("The value of passing is: " + passing);
}

In the main method, we set the variable passing to 3, and then pass that variable to the Money method, which then adds 2 to the variable passed in.

What do you think the System.out.println call will print out? Well, if you thought it would print out a 5 you are wrong. It will actually print out a 3. Why does it print out a 3 when we clearly added a 2 to the value in the Money method?

The reason it prints out a 3 is because Java passes arguments by value – which means that when the call to Money is made, Java will just create a copy of the passing variable and that copy is what gets passed to the Money method – and not the original variable stored in the main method. This is why whatever happens to cost inside the Money method does not affect the passing variable inside the main method.

orvi
  • 3,142
  • 1
  • 23
  • 36
0

Passing a value is sending a specified value to be used by a method or a class.

In the line you displayed, the value of an integer called cost is passed to the class object Money (I assume it is a class, since classes are usually capitalized) by another object.

The parameters of a method are the values that must be passed by whatever object calls it.

Passing is just sending a value to that object from another object. Whatever is in the parentheses when you call a method or a class is what is being passed to it (though when passed to a class, a constructor must be present).

Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
Lupus Nox
  • 138
  • 8
  • 1
    @New.Programmer *please* read about the language, do tutorials for a few days or weeks and if you have *specific* question then come back. Asking what the difference between a parameter and a local variable is is as basic as your original question and is as off-topic. – luk2302 Jan 29 '17 at 22:03
-1

In Java, from the outcome, you will see that:

  • when you pass a primitive type of value, like int, double, float, etc, however you modify the value in the accepting method, the value of the original variable won't change.

  • when you pass an object, if you modify the object data in the accepting method, you're modifying the original object.

Knowing this, you know what outcome you are expecting when you are writing code.

As to the terminology, please refer to other answers/comments for more details.

Kevin Wang
  • 182
  • 1
  • 9
  • what is that down vote for? mind explain? or you just make a random click? – Kevin Wang Jan 29 '17 at 22:00
  • 1
    NO NO NO NO, OP does not know much yet, don't add false informations to the that. It is **always** pass-by-value https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1 – luk2302 Jan 29 '17 at 22:01
  • what part is false? care to elaborate? – Kevin Wang Jan 29 '17 at 22:02
  • Oh! That makes a little more sense. So what would be the difference between public Money (int cost) and -- public Money () {int cost;} ? – ODor Jan 29 '17 at 22:02
  • 1
    What is there to elaborate, you make two statements and one them is plain and 100% false. *"And pass by reference is when you pass an object."* is just **absolutely not true** - please read the answer I linked and change your answer as soon as possible / or delete it. – luk2302 Jan 29 '17 at 22:05
  • say you have aCost=8, you pass it to Money(int cost), however you change the "cost", the value of aCost won't change. so it is passed by value, not the reference to the variable aCost itself. about public Money() {int cost;} that's a constructor, in the example you gave, "int cost" has no value? – Kevin Wang Jan 29 '17 at 22:07
  • @luk2302 this is why I get confused so many people tell me different things! – ODor Jan 29 '17 at 22:09
  • @luk2302, if you know C++ and Java, you would know they work just like that, the two languages work differently, there is no one-to-one map. don't be obessed with terminology and fail to see the fundamental truth. – Kevin Wang Jan 29 '17 at 22:10
  • 1
    Why are you introducing C++ in this mess, leave it out - it **is** a completely different language, no need to reference it at all. Key is: Java is pass-by-value, don't first introduce terms like pass-by-reference, introduce them incorrectly and then say "it does not matter". Where are you going with this? *Please* stop. – luk2302 Jan 29 '17 at 22:12
  • @New.Programmer, that's the rule of thumbs to help you distinguish the difference. there are people who always needs a guide book to follow, for which I cannot help. programming is about understand how the language works, not words in textbook/dictionary. – Kevin Wang Jan 29 '17 at 22:12
  • @KevinWang yes I'm trying my hardest to learn it, I mean I'm not exactly the fastest person at learning something, but I believe with the right attitude and persistance I'll get there in time, so thanks for the help! – ODor Jan 29 '17 at 22:17
  • 2
    Java never passed through reference . Java Object references are always passed by value. – orvi Jan 29 '17 at 22:17
  • 1
    @New.Programmer I *beg* you not to listen to him, look at the answer I linked if you want to learn about the terms "pass-by-value" and "pass-by-reference". If anything, look at my reputation and realize that I in fact do know what I am talking about as opposed to Kevin. I am referencing facts, Kevin is just crawling to weak arguments as soon as he is wrong. *If* you want to use widely used and understood terms, use them correctly, not as Kevin does. Thanks orvi. – luk2302 Jan 29 '17 at 22:19
  • @orvi, that's why I said, this pass-by-value and pass-by-reference does not hold much meaning in Java as in C++. The only way to map this in the two languages is by behavior, as in C++, you pass-by-value so original variable will not be modified, by pass-by-reference the original variable can be modified in the accepting method/function. I value knowing how it works much more than how it's named. – Kevin Wang Jan 29 '17 at 22:21
  • @luk2302, and no, I'm not arguing to win. I'm simply saying the know-how-and why is more important then terms. – Kevin Wang Jan 29 '17 at 22:25
  • 1
    I give up - will you leave your answer in this desolate state? – luk2302 Jan 29 '17 at 22:26
  • @KevinWang Every language has its own structure. That's why we use different language for our purposes and there's some terminology we always to remember. :) I don't want to argue. But it's the fact bro. If in your board someone ask you do java pass by reference or value at that time you will realise does it hold any emaning in java :p Till then cheers – orvi Jan 29 '17 at 22:29
  • 1
    @orvi, it's OK. no problem :) I do think your explanation is helpful for someone inexperience. But just my personal preference is with pragmatic approach than theory and terms. – Kevin Wang Jan 29 '17 at 22:32
  • 1
    @luk2302 is absolutely correct. "Pass by reference" has a specific meaning, and that meaning does not apply to how Java passes pointers. Even in C++, which is irrelevant, pointers are not passed by reference unless the reference notation is used. Java simply does not pass by reference, and Kevin Wang's answer diminishes "know-how-and why [sic]", not increases it. I know he keeps trying to say that by giving a wrong answer he's supposedly trying to aid understanding, but that's never the way to go. Giving _correct_ answers is the right way. Ignore Kevin Wang. Listen to luk2302. – Lew Bloch Jan 29 '17 at 23:44
  • Good lock with your "pragmatic approach" in interview question, good luck getting a job with that "knowledge" you have there. Inteviewer: "Is java pass-by-value or pass-by-reference" - You: "Well, i think bla bla bla" - Inteviewer: "Thanks, we will 'contact you'". Either don't use the terms pass-by-reference/value at all OR use them correctly, both are fine, but using them incorrectly is going to get you into trouble. And who the hell upvoted this trash? – luk2302 Jan 30 '17 at 10:03
  • @luk2302, now you're being provocative calling someone else's view "trash" just because it is different from yours. You have my sympathy in your inability to see things in its true nature, inability to think without finding existing reference in a book or someone's words. Please stop commenting, and write your own "great" answer, and let others who agree with you upvote you up high. bye-bye! – Kevin Wang Jan 30 '17 at 10:35
  • *facepalm*, facts are facts, there is nothing to be "viewed" in regards to the two terms, they are clearly defined. But go ahead and stay with your #alternativefacts – luk2302 Jan 30 '17 at 10:39