- Equality
In Java, what is the difference between the comparision operator, ==, and the comparision method, equals,
when comparing objects?
- Object References
Consider the following two innocent looking methods:
StringBuffer getText ()
{
return myText;
}
String getName ()
{
return myName;
}
Because every object variable in Java is a reference, explain why the first method has the
potential to break the encapsulation of the object, while the second method does not.
- Swapping two values
In C++, the following method swaps any two values passed:
template<class T>
void swap (T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
In Java, however, the following version of the method has no effect on the two values passed:
void swap (Object a, Object b)
{
Object tmp = a;
a = b;
b = tmp;
}
- Even though all object variables in Java are actually references, explain why the Java version of swap does not work.
- Describe a way to write a generic swap method in Java. You do not need write your solution in Java code, just explain what is needed to make a swap method possible.
- What is the main difference between running programs in C++ and Java?
What impact does that have in terms of compile-time versus run-time errors?