EE408 Java Questions and Answers (Fall 2008)
Questions are answered according to
the order I received them. Some editing may have been applied according
to my understanding of the questions. Requests for further
clarification are welcome.
1. What is the main difference between a class and an interface?
A class may implement its methods by
providing a body for them. An interface declares only method headers
without a body. A class may be used to create an object ("new
C(...)"). An interface cannot be used
to create an object. Both can be used as the declared type of a
variable.
2. References in Java are similar to pointers in C++. Is there
anything like pointer arithmetic in Java?
No pointer arithmetic in Java.
Pointer arithmetic is important for systems programming, a domain
C++/C, but not Java, was targeted at.
3. Can functions be created outside of a class like they can be in C++
or must they be a method of a class?
In Java, all operations must be
declared inside a class as methods.
4. What is the purpose of an object being immutable (or mutable)?
It depends on the meaning of the
objects to make them mutable or not. For some objects, like a Point in
a plane, it just makes sense for them to be immutable. On the other
hand, some objects like a Stack or a Queue, are inherently mutable. A
benefit for having immutable objects is that they help compilers to
improve performance.
5. What is a supertype?
Simply put it, in Java, an interface
I that is implemented by a
class C is a supertype of C.
A class (interface) S that is extended by another class (interface) C is also a supertype of C. C is called the subtype of I and S, respectively. Objects of a
subtype are expected to be able to be substituted for an object of a
supertype while maintaining the behavioral correctness of a program.
6. What is the purpose in overloading a class?
Only methods, not a class, are
overloaded. Overloading may help improve code readability by using the
same name for a group of related, similar methods.
7. Can you make custom templates because I noticed Java has function
overloading?
No. (I assume by "custom
templates" you are referring to function
templates.)
8. What is the difference between vectors and strings?
In Java, Vector can grow dynamically
and is mutable, and String is immutable.
9. Can you overload operator functions like (=,-,+,<,>)?
No.
10. Is there any way to avoid Object Sharing when assigning one Object
another?
This must be taken care of by the
programmers rather than by the compiler. If they know what other
variables are pointing to an object of interest, they could assign null
to those variables to avoid sharing. But object sharing is necessary
for implementing some features. Thus the challenge to programmers is to
learn to use object sharing only when it is necessary and avoid it
otherwise.
11. Is there any special way to declare an Object
immutable? Is there any easy way to tell if an object is
immutable or mutable?
No automated, machine-checkable way
other than through comments. Similarly, concluding the mutability of an
object is still done by reasoning about the code manually. There are
some research tools trying to automate this step, but they are not
widely used in practice.
12. Why would someone use a Vector instead of an Array and vice
versa?
A vector is good for situations where
the maximum number of elements in a sequence is unknown. When the
number of elements is known, an array can be used. Array is built-in to
Java, and Vector is an ordinary class.
13. If object is an supertype could you create something of type Object
and then set any different object equal to it?
Because the class Object is an abstract class, it cannot be used to create an object of
type Object. But Object can be used as the declared type of a
variable.
14. Do things like the Integer object act like a primitive int if you
try to use it like one?
Yes, since Java 1.5. This is called
unboxing.
15. If a function has static before it but passes an array can it
change elements because the arrays address is being passed?
Yes.
16. Why are strings immutable?
One, with immutable strings, one can
do all that a mutable string can offer. Two, immutable types make
certain compiler optimizations possible.
17. How do we do operator overloading?
No operator overloading in Java.
18. Are Java Vectors, Lists, etc. the same in implementation as those
in C++?
The ideas underlying these data types are the same regardless
of programming languages, but they are implemented in two different
languages, C++ and Java.
19. How does Java know when to garbage collect variables/objects not in
use?
When a heap object is not referenced
any more, the Java virtual machine will reclaim its memory. Garbage
collection is an active research area in compiler construction.
20. Is it possible to have standalone functions like in C++ or do they
have to be class methods?
Yes, you can have standalone
functions in Java by adding a static keyword before a method.
21. What are some programming etiquette tips specific to Java?
Nothing really specific to Java.
Technically, you may want to know about how to write JavaDoc.
22. Will Java automatically deconstruct dynamic objects or variables?
Yes. When a heap object is not
referenced by any variable any more, the Java virtual machine will
reclaim its memory.
23. What are some other key conversions Java will automatically do like
how it converted int to an Integer object?
The reverse called unboxing. And
boxing and unboxing apply to all primitive types. See here for further details about autoboxing.
24. I think i still need some clarification of when to use == and
.equals() and what the differences are.
For two variables l and r, l==r
compares their values, which are
references, and l.equals(r)
compares the heap objects that l and r refer/point to.
25. How do you make a graphical GUI in Java is it implementation of
another class, or is it another type of project? Are we also going to
make visual GUI's in this course?
In Eclipse, a Java GUI application is
a Java project.
26. Dispatching is it accessing variables from another class, method or
both?
Dispatching is about accessing
methods only.
27. I am confused as to the exact definition of a subtype and supertype
of hierarchy, What are they?
See item 5 above.
28. I don’t really understand what it means when the word “Boolean” is
written in the code. What does it mean?
Every variable is declared with a
type. Java pre-defines several primitive types (int, long,
boolean, char, byte, float, and double).
The boolean type includes two values, true and false. "Boolean" is the object counterpart of primitive
type "boolean" (notice the
different capitalization of the first letters). Boxing and unboxing
apply to Boolean and boolean.
29. What does the word “static” mean when written in code?
A static method is roughly equivalent
to global functions in C++. A static field a global variable in C++.
30. What is a “try-catch” construct?
These are constructs for exception
handling. The discussion of exception handling will be deferred.
31. Is the apparent type just what the object is declared as and the
real type just whatever subtype is assigned to that object?
Yes. But to be more precise, in the
question, "object" should be replaced by "variable".
32. I am having a hard time understanding the explanation of
dispatching in the handout. Could you explain it a different way?
Yes, I will discuss dispatching
sometime.
33. If you declare a supertype object and then assign it an object of a
subtype, by what method does this assignment take place? is it just
element by element assignment of the fields?
None of the above. In Java,
assignments pass around only references (pointers in C++'s terms), not
the actual objects that reside on heap.
34. Does similar compiled C++ code run faster than compiled Java code?
Yes when everything else is the same.
35. Is Java more widely used in the professional world than C++?
I do not have accurate data to answer
this question. A fair answer would be that both are widely used in the
professional world. C++ would be favored if performance or memory is a
critical requirement to a project.
36. Are there additional libraries to "include" when using Java or is
everything included?
The basic library, a.k.a JDK, is
automatically included as a whole. Java also allows for additional,
application-specific libraries. Libraries are made of compiled classes
and packaged in a format called "jar".
37. In what way is Java more similar to SmallTalk, Lisp, Simula67 and
CLU?
This is an advanced topic in
programming languages. In short, Java and most of these languages are
garbage collected, meaning that developers are free from memory
management issues. These languages are also similar in their support
for abstract data types. They are all type-safe languages. Further
details about these languages can be found here.
38. Is operator overloading possible in Java?
User-defined operator overloading is
Not supported. But there are built-in operator overloading. For
example, + is used for both arithmetic addition and string
concatenation.
39. Do Java programs with a GUI need to have a main class?
Yes. All Java programs need to have a
main class. A GUI program written in Java is a Java program.
40. What is the difference between the stack and the heap?
I know that this was explained in the packet but I would like a more
detailed explanation from a different source.
This is not a question that can be
briefly answered here. Please see me sometime to discuss.
41. When we create our own custom class, is it automatically a subclass
of Object??
Yes, all objects inherit from the Object class in the java.lang package.
42. How does an int or float get “widened” into a long?
int, but not float, can be widened
into long. More details about the possible conversion can be found here.
And most likely we will not need to use/know these details in EE408.