PDA

View Full Version : Need help, Im not getting this and I should!


oneluvsdesire
12-12-2007, 08:04 AM
Thanks in advance. If I am allowed I need to post what the book says so someone can dumb this down for me. I think it would solidify a lot in my head if I understood this. Part 1:

When you pass in a variable of some primititive data type, it is passed by value, meaning the value is passed in, not the reference to where the value of the variable is stored. Any operations to the argument do not affect the original variable that was passed in. Here is an example:

public class Test {
public void add(int a, int b) {
a += b;
System.out.println(a);
}

public static void main(String args[]) {
Test t = new Test();
int x = 1, y = 2;
t.add(x, y);
System.out.println(x);
}
}

In the main()method, you declare variables int x = 1, y = 2, and then you pass their values to the add()method (t.add(x, y) ). Even though the add() method reassigns a new value to its first argument, a, x remains unaffected when the method returns control back to the main(). When you print the value of x, it is still 1.

1. Am I understanding the bolded statement to read that int a and b are added ( a += b) and assigned to a?

2. t.add(x, y); System.out.println(x); the value of x is still 1. Am I understanding this to mean, that no matter what happened in #1, in #2 the value of x will always be "1". If this is correct, what would be the value of "a"? Or does something have to be declared there before it will assume any value? When I read it, whatever "a" is and "b" is when they are added it goes into "a". Should I be concerned with what is in them at this point? If I shouldn't then I dont understand why the red bolded statement even exists if I dont know what is in the a's and b's.

3. Am I understanding in the first part, there is nothing declared and instantiated? and in the second part t is declared as a test and instantiated? This qualifies as creating an object?

Both parts are using an add method right?? So why would what happens with the a's and b's have anything to do with the x's and y's?

I could even understand if it is so simple a 10 yr old could get it...I just want to get it, so reading this stuff makes some sense to me.

liorean
12-12-2007, 01:19 PM
Thanks in advance.Give thanks when they're deserved, not ahead of time. Many consider that rude, as if you're taking them helping for granted.

1. Am I understanding the bolded statement to read that int a and b are added ( a += b) and assigned to a?Yes.

2. t.add(x, y); System.out.println(x); the value of x is still 1. Am I understanding this to mean, that no matter what happened in #1, in #2 the value of x will always be "1".If x is passed by value, yes. You can think of it like this: First the value is extracted from x. Second that value is sent to the method t.add. Third, t.add initiates a local variable a to that value. Fourth, the value of that local variable is changed.If this is correct, what would be the value of "a"? Or does something have to be declared there before it will assume any value? When I read it, whatever "a" is and "b" is when they are added it goes into "a".That's true. However, both a and b fall out of scope when t.add returns, so at this stage, they no longer exist.Should I be concerned with what is in them at this point? If I shouldn't then I dont understand why the red bolded statement even exists if I dont know what is in the a's and b's.The red bolded statement is important not because of the fact a was changed, but because the fact x is unchanged. And no, you shouldn't be concerned about the value of a at this stage, because a doesn't exist any longer.

3. Am I understanding in the first part, there is nothing declared and instantiated? and in the second part t is declared as a test and instantiated? This qualifies as creating an object?In fact, both a and b are declared in Test.add, and both are instantiated when t.add is called.

Both parts are using an add method right?? So why would what happens with the a's and b's have anything to do with the x's and y's?Because if the variables were sent by reference and not by value, then x would be the same variable as a, which means that the value of x would be changed when the value of a is changed.

I could even understand if it is so simple a 10 yr old could get it...I just want to get it, so reading this stuff makes some sense to me.I think you got it even before you asked :)

oneluvsdesire
12-12-2007, 06:08 PM
The more positive spin on "thanks in advance" is that I know you don't have to help me, but I appreciate you reading this....in advance although I will thank you profusely afterwards:)

If you say I got it, then why re-reading it confused me all the more? Or am I reading too much into it?

The second part to that, which I'm hoping I get, is this:

If you pass in an object, its reference is passed in, rather than a copy of the object. So if you change the object within the method, it changes the object variable outside of the method. Correct?

So then in the first post, I passed in a reference to an object...not the actual object. So x will always equal 1 because the change is only to the reference not the actual value.

I don't know, maybe you are right and I do get it...Im still wanting something to "click".

Thanks much for taking the time to explain it to me. I really do appreciate it.

Gox
12-12-2007, 07:54 PM
You can think of pass-by-value as passing a copy of the primitive/object. So in your first example when you do t.add(x,y) you're saying "Find the values of x and y and put them here, but don't actually put the x and y object here". In this sense, the add-method gets the parameters 1 and 2, but it doesn't actually get the variables x and y.

For pass-by-reference you can think of it as saying "Here's the location of the object. If you need to look at it or change it, you can do so by going to it's location." This is different in that it's not a copy of the object that's being passed as a parameter, but a reference to where the object is so that the method can use it. Any changes the method makes are then reflected in the object.

Here's an example:
Pass-By-Value: Your friend Jim has a front door on his house that is painted Black. I go to the store and buy an identical door in style/color and give it to you. You now know what type of door Jim has and what color it is, and may use this information however you please. But say you paint the door I gave you red, does Jim's door change to red as well? The answer is no. Although when I gave the door to you it was the same as Jim's door in all respects (style/color), they are not the SAME door, so the changes you make to the door I gave you will not be reflected in Jim's door. Anyone driving by Jim's house will still see a black door.

Pass-By-Reference: Your friend Jim has a front door on his house that is painted Black. This time I tell you, "Jim lives at 555 Birch St. and if you need to see his door you can go to his house to look at it". Now if you want to find out what style or color Jim's door is you can go to his house to find out that information. This time, if you go to Jim's house and paint the door red, does Jim's door change to red? The answer this time is yes. You went to Jim's house and changed some aspect of it, and anyone driving by Jim's house will now see a red door instead of a black door.

I'm not the best at coming up with examples, but I hope that helped.

oneluvsdesire
12-12-2007, 09:50 PM
Thank you, thank you, thank you. You have NO idea how much.