At some point, I imagine you have had the need for functions to communicate with one another. It usually takes the form of function parameters since global variables are not good programming practice. A basic version looks like:
There are two ways to pass a parameter and the distinguishing between them is very important; it can create bugs and make or break your program in some instances. First let's consider C++. C++, by default has parameters passed as shallow copies of whatever they are supposed to be. consider the following snippet:
What do you expect the output to be? 6? Nope, a shallow copy of the 'a' integer was passed to the function. This copy's value was modified to 6, but as soon as the function returns it goes out of scope and 'a' remains the same. This is where references and pointers come in handy. Consider this snippet:
When we pass 'a' from main to the function, a copy is not passed, we pass the reference to 'a' to the function. This allows us to modify the value of a directly. This is both powerful and dangerous since (unless we are careful) we can accidentally modify a value and have a tough bug to find. The output of this snippet is 5, 6, indicating we successfully modified a's value. We can achieve the same effect with pointers (which are more often used then references since they have more abilities, but that goes out of the scope of what I'm talking about in this blog post). Consider this:
We pass a reference to a as the param in main, this is assigned to the pointer "theNum" in function and then we assign the value of theNum (its derefernece location to 6). The output is the same as the previous snippet: 5, 6. Effectively two ways to reach the same destination. The ability to expressly use pointers and references is an advantage (and both a curse?) C++ has over java. Consider a typical swap function:
We create two integers, pass their references and "swap" their values using a temp holder. This is only possibly by using pointers. If we tried the exact same concept with java it would fail. Consider the same snippet in java:
The output is 5,6 5,6. Why? Because we did not pass the reference to the numbers (in this instance it was a shallow copy). This can get confusing since java handles objects by reference, but method parameters by value. "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." The scope of the parameters end with the death of the function.
Clarification: If the argument is a primitive type, then it is passed by value. If the argument is an object (or an array), then it is considered pass-by-reference. However, java does not pass by reference--at all.
Java does not allow programmers to directly access and play around with object pointers. To protect us I suppose, but it requires a little extra effort on our part to solve the problem. If the numbers are a part of a data structure and we pass the structure as well, then any assignments will remain in effect, even after the death of the function:
The array is passed by reference (remember that an array has an implicit pointer to the first element in C++ which we can access and it also exists in java, but we cannot manipulate it the same way, i.e. directly). Sidenote: the subscript operator [] is the same as derefencing the pointer + the element you want to access in C++:
For a final look-see, I'll show you two string permutation algorithms, one in C++ and the other in Java. Both work, however, see if you can notice the difference needed to make it work in java:
VS.
Found it? The answer is, in java I had to pass the array so the swapping would be permanent, otherwise it wouldn't display the permutations correctly; it would be as if the swap never happened. Specifically here:
As with most programming endeavors, there are many routes to solve a problem. It is knowing how best to utilize your tools at your disposal that makes you a good programmer. In the above work, one way is not necessarily better then the other, as it would be comparing apples to oranges. Each has a different manner in which things must be handled. Hopefully you found this both useful and interesting. Happy coding!
--KYA
edited to be more accurate
void function (int theNum)
{
//do stuff with the num
}
int main()
{
int a = 5;
function(a);
return 0;
}
There are two ways to pass a parameter and the distinguishing between them is very important; it can create bugs and make or break your program in some instances. First let's consider C++. C++, by default has parameters passed as shallow copies of whatever they are supposed to be. consider the following snippet:
void function (int theNum)
{
theNum = 6;
}
int main()
{
int a = 5;
function(a);
cout << a << endl;
return 0;
}
What do you expect the output to be? 6? Nope, a shallow copy of the 'a' integer was passed to the function. This copy's value was modified to 6, but as soon as the function returns it goes out of scope and 'a' remains the same. This is where references and pointers come in handy. Consider this snippet:
void function(int& theNum)
{
theNum = 6;
}
int main()
{
int a = 5;
cout << a << endl;
function(a);
cout << a << endl;
return 0;
}
When we pass 'a' from main to the function, a copy is not passed, we pass the reference to 'a' to the function. This allows us to modify the value of a directly. This is both powerful and dangerous since (unless we are careful) we can accidentally modify a value and have a tough bug to find. The output of this snippet is 5, 6, indicating we successfully modified a's value. We can achieve the same effect with pointers (which are more often used then references since they have more abilities, but that goes out of the scope of what I'm talking about in this blog post). Consider this:
void function(int* theNum)
{
*theNum = 6;
}
int main()
{
int a = 5;
cout << a << endl;
function(&a);
cout << a << endl;
return 0;
}
We pass a reference to a as the param in main, this is assigned to the pointer "theNum" in function and then we assign the value of theNum (its derefernece location to 6). The output is the same as the previous snippet: 5, 6. Effectively two ways to reach the same destination. The ability to expressly use pointers and references is an advantage (and both a curse?) C++ has over java. Consider a typical swap function:
void swap(int* first, int* second)
{
int temp = *first;
*first = *second;
*second = temp;
}
int main()
{
int a = 5, b = 6;
cout << a << " " << b << endl;
swap(&a, &b);
cout << a << " " << b << endl;
return 0;
}
We create two integers, pass their references and "swap" their values using a temp holder. This is only possibly by using pointers. If we tried the exact same concept with java it would fail. Consider the same snippet in java:
public static void swap (int first, int second)
{
int temp = first;
first = second;
second = temp;
}
public static void main (String args[])
{
int a = 5, b = 6;
System.out.println(a + " " + b);
swap (a,b);
System.out.println(a + " " + b);
}
The output is 5,6 5,6. Why? Because we did not pass the reference to the numbers (in this instance it was a shallow copy). This can get confusing since java handles objects by reference, but method parameters by value. "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." The scope of the parameters end with the death of the function.
Clarification: If the argument is a primitive type, then it is passed by value. If the argument is an object (or an array), then it is considered pass-by-reference. However, java does not pass by reference--at all.
Java does not allow programmers to directly access and play around with object pointers. To protect us I suppose, but it requires a little extra effort on our part to solve the problem. If the numbers are a part of a data structure and we pass the structure as well, then any assignments will remain in effect, even after the death of the function:
public static void swap (int[] arr)
{
int temp = arr[0];
arr[0] = arr[1];
arr[1]= temp;
}
public static void main (String args[])
{
int[] nums = {5,6};
System.out.println(nums[0] + " " + nums[1]);
swap (nums);
System.out.println(nums[0] + " " + nums[1]);
}
The array is passed by reference (remember that an array has an implicit pointer to the first element in C++ which we can access and it also exists in java, but we cannot manipulate it the same way, i.e. directly). Sidenote: the subscript operator [] is the same as derefencing the pointer + the element you want to access in C++:
//Neat little thing to know arr[3] == *(arr + 3);
For a final look-see, I'll show you two string permutation algorithms, one in C++ and the other in Java. Both work, however, see if you can notice the difference needed to make it work in java:
//C++
#include <iostream>
#include <string>
using namespace std;
void swap(char* first, char* second)
{
char ch = *second;
*second = *first;
*first = ch;
}
int permute(char* set, int begin, int end)
{
int i;
int range = end - begin;
if (range == 1) {
cout << set << endl;
} else {
for(i=0; i<range; i++) {
swap(&set[begin], &set[begin+i]); //initial swap
permute(set, begin+1, end); //recursion
swap(&set[begin], &set[begin+i]); //swap back
}
}
return 0;
}
//Example Implementation -- Up to you on how to use
int main()
{
char str[255]; //string
cout << "Please enter a string: ";
cin.getline(str, 255); //take string
permute(str, 0, strlen(str)); //permutate the string
return 0;
}
VS.
//java
import java.io.*;
public class Permute {
public static void swap(char[] set, int first, int second)
{
char ch = set[second];
set[second] = set[first]; //swap the values
set[first] = ch;
}
public static int permute(char[] set, int begin, int end)
{
int i;
int range = end - begin;
if (range == 1) {
System.out.println(set); //print out each permutation
} else {
for(i=0; i<range; i++) {
swap(set, begin, begin+i); //initial swap
permute(set, begin+1, end); //recursion
swap(set, begin, begin+i); //swap back
}
}
return 0;
}
//***************TEST EXAMPLE********************
public static void main(String[] args) {
char[] test = {'a','b','c','d'};;
permute(test, 0, 4);
}
}
Found it? The answer is, in java I had to pass the array so the swapping would be permanent, otherwise it wouldn't display the permutations correctly; it would be as if the swap never happened. Specifically here:
public static void swap(char[] set, int first, int second) {}
//versus
void swap(char* first, char* second)
As with most programming endeavors, there are many routes to solve a problem. It is knowing how best to utilize your tools at your disposal that makes you a good programmer. In the above work, one way is not necessarily better then the other, as it would be comparing apples to oranges. Each has a different manner in which things must be handled. Hopefully you found this both useful and interesting. Happy coding!
--KYA
edited to be more accurate
5 Comments On This Entry
Page 1 of 1
LaFayette
03 March 2009 - 12:19 PM
Actually, everything in Java is passed by value! If you pass a primitive type, the value it self is copied (hard copy), and if you pass an object type, the address to where it's pointing is copied (shallow copy). This might look like pass by reference since you can do stuff to what the reference is refering to and have the effect permanent, but you can't change the reference it self and make it stick.
Have a look at this:
This will print 2 and then 3. arr outside won't be effected since the reference was copied when we called foo!
Have a look at this:
public class ByValue extends JPanel {
public static void foo(int[] arr) {
int[] arr2 = {2};
arr = arr2;
System.out.println(arr[0]);
}
public static void main(String[] args) {
int[] arr = {3};
ByValue.foo(arr);
System.out.println(arr[0]);
}
}
This will print 2 and then 3. arr outside won't be effected since the reference was copied when we called foo!
LaFayette
03 March 2009 - 01:44 PM
thumbs up, KYA!
edit: removed my example, because the examples in polymath's link are better!
edit: removed my example, because the examples in polymath's link are better!
Page 1 of 1
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
My Blog Links
Recent Entries
Recent Comments
Search My Blog
20 user(s) viewing
20 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



5 Comments









|