Sunday, March 30, 2014
Wednesday, March 19, 2014
Java: Pass By Value notes
The Java Spec says that everything in java is pass-by-value. There is no such thing as "pass-by-reference" in java.
Dog myDog;
is not a Dog; it's actually a pointer to a Dog.
What that means, is when you have
Dog myDog = new Dog("Rover");
foo(myDog);
you're essentially passing the address of the created Dog object to the foo method.
Suppose the Dog object resides at memory address 42. This means we pass 42 to the method.
If the foo Method were defined as
public void foo(Dog someDog) {
someDog.setName("Max"); // AAA
someDog = new Dog("Fifi"); // BBB
someDog.setName("Rowlf"); // CCC
}
the parameter someDog is set to the value 42
at line "AAA"
someDog is followed to the Dog it points to (the Dog object at address 42) that Dog (the one at address 42) is asked to change his name to Max
at line "BBB"
a new Dog is created. Let's say he's at address 74 we assign the parameter someDog to 74
at line "CCC"
someDog is followed to the Dog it points to (the Dog object at address 74) that Dog (the one at address 74) is asked to change his name to Rowlf then, we return.
What happens outside the method:
Did myDog change?
Keeping in mind that myDog is a pointer, and not an actual Dog, the answer is NO. myDog still has the value 42; it's still pointing to the original Dog.
It's perfectly valid to follow an address and change what's at the end of it; that does not change the variable, however.
Dog myDog;
is not a Dog; it's actually a pointer to a Dog.
What that means, is when you have
Dog myDog = new Dog("Rover");
foo(myDog);
you're essentially passing the address of the created Dog object to the foo method.
Suppose the Dog object resides at memory address 42. This means we pass 42 to the method.
If the foo Method were defined as
public void foo(Dog someDog) {
someDog.setName("Max"); // AAA
someDog = new Dog("Fifi"); // BBB
someDog.setName("Rowlf"); // CCC
}
the parameter someDog is set to the value 42
at line "AAA"
someDog is followed to the Dog it points to (the Dog object at address 42) that Dog (the one at address 42) is asked to change his name to Max
at line "BBB"
a new Dog is created. Let's say he's at address 74 we assign the parameter someDog to 74
at line "CCC"
someDog is followed to the Dog it points to (the Dog object at address 74) that Dog (the one at address 74) is asked to change his name to Rowlf then, we return.
What happens outside the method:
Did myDog change?
Keeping in mind that myDog is a pointer, and not an actual Dog, the answer is NO. myDog still has the value 42; it's still pointing to the original Dog.
It's perfectly valid to follow an address and change what's at the end of it; that does not change the variable, however.
Monday, March 10, 2014
Who is a developer?
A developer. Not a C programmer, not a Rubyist, or any subset of software development. Know how to program, know how to solve problems, and know how to learn new languages, libraries or frameworks. Sticking to your guns in an industry as ever-changing, for better or worse, as Software engineering, is a bad idea.
Subscribe to:
Posts (Atom)