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.
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.