Java lacks call-by-reference parameter passing, which means that any changes made to method parameters (as opposed to the values referenced by the parameters) from within the method are not propagated back to the caller. Page 140.
The answers, approximately verbatim, in no particular order, and with comments in italic:
Your can’t implement a swap method in Java because objects are pointing1 to memory addresses, not the actual data.
1: Object references are pointing to memory addresses; objects are in memory.
A swap method cannot be implemented in Java because Java uses the pass by value implementation instead of the pass by reference. So when a value is used to swap to another value the initial value2 has not changed it only swaps temporarily.3
2: Which value is this? The value given by the calling method or the value received by the swap method?
3: That is, for the duration of the swap method’s execution.
You cannot implement a swap method because Java does not call by value. It does, however allow call by reference.4 Using the swap will not change the values of the objects5. Once the method is terminated the objects return back to the original values.6
4: Backwards; Java has call-by-value, not call-by-reference.
5: The object references.
6: The original values - that is, the values relative to the calling method - are never changed. The swap method changes local copies of the parameter values; those copies are discarded when the swap method returns.
void swap(int x, int y)
passes the value of x
and
y
when you really need the reference of the object that x
, and
y
are pointing to.7 If inside this method you say int z =
x; int x = y; int y = z;
Java will think that you are talking about a new
variable x and y and not the ones you passed to it.
7: We haven’t come to this yet in the course, but int
s are not
class instances.
You can’t implement a swap method because all methods in Java are8 pass by value. This means that any changes in to the variables passed into the swap method would still have their initial values once the method has completed.
8: Methods in Java use call by value.
Java does not pass parameters in by reference so any work being done9 is not done on the intended objects.10
9: Where? By what?
10: Which are?
#include
statement.
#include
implements textual inclusion, changing the source file containing
the #include
by adding the text in the file referenced by the
include. Import statements define an abbreviation available for use in the rest
of the source file, which has a minimal, but not insignificant, effect.
Include statements are required (or, perhaps more accurately, it would be difficult to write programs of significant size and function without using include statements). Import statements are a convenience, and need not be used. Page 154.
The answers, approximately verbatim, in no particular order, and with comments in italic:
1) #include
adds different libraries into a C++ program so new
functions can be used. You don’t always have to use the import statement
to use a new class.
2) Import allows you to not include the package name for the classes
that are in the imported class.1 #include
only allows you to
use new functions and classes, but you still need to state where they come
from unless you use the using namespace commands well.2
1: Package.
2: This is a good answer; better than mine.
Using the import statement makes it easier to write methods of a class. For example, instead of writing class.method, we could simply write just the method.3 If there is the same method4 for separate classes, we must write class. method where as in C++, we still need to refer to the class.5
3: Ah, so you mean it makes it easier to reference a method in another class. The ease with which you can write a method that doesn’t refer to other methods or to methods outside the class in which the method is being written is independent of include statements.
4: Do you mean the same name for methods in different classes?
5: One of the important words in the question is #include
; I don’t
see much of that in the answer.
Java’s import statement differs from C++’s #include
statement
by:
#include
statement includes the code from the header files,
making the executable file large.6
6: And the import statement differs how?
7: And the include statement differs how?
Java’s import statements are more like C++’s using namespace statements.8
A Java program can compile without a import statement whereas C++ program’s will not compile without an include statement.9
An import statement simply tells javac where to look for a class file,
whereas a #include
tells the C++ compiler to bring that of code into my
program.
8: Import statements are more like namespace statements than what? Presumably imports are more like namespaces than they are like includes, but this is a quiz: you’re supposed to tell me that, not leave it to me to figure out on my own. You probably should have just crossed this sentence out.
9: In all cases? Is it true you can’t write a C++ program that will compile but doesn’t use include statements?
C++ include statements need to be added for which ever library is being used within the program at the beginning. Java import statement are mostly already incorporated10 when a project11 is started. Can also be called12 within each class as needed.
10: How does this happen?
11: Project? That sounds like IDE talk.
12: What can also be called?
To include in C++ is to essentially tell the compiler to take [?] source file and in effect append it to the start of the file that is including.13 The Java compiler already knows about that code.14
It15 also tells that compiler where to look when using elements defined elsewhere. Java will look anyway.
13: Not append to the start; replace the include with the contents of the file referenced by the include.
14: What code? By what mechanism? Whatever it is, I don’t think it’s by the import statement, which is what this question is about.
15: Which it? There’s at least two of them.
This page last modified on 12 February 2009. |