Friday, August 17, 2007

Java VS C++ : parameter passing

Parameter Passing : Pass-By-Value & Pass-By-Reference.

Parameter passing is something people who moved from C/C++ to Java (or the other way around) get confused.

Let's just explain it with few examples.

-------------------------------------

void main () {
int myAge = 10;
string myName = getName (myAge);
exit (0);
}

string getName (int age) {
age++;
return "My Name";
}

-------------------------------------


What is the value of myAge after getName(myAge) gets executed?
in C --> 10
in C++ --> 10
in Java --> 10


In all 3 languages above, the parameter that is of a primitive type is always being passed by value. What it means is that if getName() function is defined like the one above, the parameter value outside getName() function will never be changed even if the user manipulates the parameter value (e.g. age++) in getName() function.

Then, how about this case?

-------------------------------------

void main() {
Car myCar = new myCar ("Ferrari");
int price = getPrice (myCar);
exit (0);
}

int getPrice (Car aCar) {
aCar.setName("Hyundai");
return 10000;
}

-------------------------------------

What will be returned if myCar.getName() gets called, after getPrice(myCar) gets executed?
in C --> ERROR! there is no class in C. :-)
in C++ --> still a Ferrari.
in Java --> Hyundai!!!!! (Where the hell is my Ferrari?!?!)


This part is the critical difference between Java and C++ when passing parameters to a function.

In Java, if non-primitive data types are passed as a parameter, reference to the data will be passed, ALWAYS! In other words, if manipulation on the parameter is done inside the function, it will be reflected outside the function as well.

To be more precise, Java never holds a object itself like in C++. Instead, it always holds a reference to an object. Also, Java always passes parameters by value, and the parameter being passed to a function is a reference value. oooh, this is not quite so easy to understand, by the way. :-(

Probably many C/C++ programmers will wonder why the name has been changed to Hyundai, or many Java programmers will wonder why the name has not been changed to Hyundai, if the same code was run in C++.

Java VS C

Differences between Java and C as described in "Java In A Nutshell"

These are something that may well be asked on a university CS exam. (e.g. "List at least 5 differences between Java and C")

1. No preprocessor (e.g. #define, #include, #ifdef)
2. No global varibles -> no possibility of namespace collision
3.Well-defined primitive types :
- Size of short, int, long types are platform (e.g. "What kind of CPU?") dependent in C.
- Not so in Java
4. No pointers
- Java classes and arrays are reference types
- Java references are "opaque" -> no way of converting it to a premitive types or to manipulate it (e.g. ptr++; )
5. Garbage Collection
6. No goto statement
7. Variable declarations anywhere : also true in C++!
8. Forward references : funtions can be used before being defined/declared.
9. Method overloading : multiple methods with same name can be defined as long as parameter lists are different.
10. No struct or union types
11. No bitfields
12. No typedef
13. No method/function pointers

Monday, August 13, 2007

rss test