Java Questions 11 - 20  «Prev Next»

Java Garbage Collection Questions

  1. What role does the finalize() method play in garbage collection?
    Answer:

    The finalize method is about memory and its de-allocation. 1) every class inherits the finalize() method from java.lang.Object, 2) the method is called by the garbage collector when it determines no more references to the object exist 3) the Object finalize method performs no actions but it may be overridden by any class normally it should be overridden to clean-up non-Java resources for example, closing a file 4) when overridding finalize() it is good programming practice to use a try-catch-finally statement and to always call super.finalize() , 5) This is a safety measure to ensure you do not inadvertently miss closing a resource used by the objects calling class
  2. What is the purpose of the finalize method?

    Answer:

    It is limited to special cases in which your object can allocate some storage in some way other than creating an object.
  3. Does Java allow you to create local objects?

    Answer:

    No. You must always use the new keyword.
  4. How can one think of an object?
    Answer:
    As an instance of a class to which attribute values have been assigned.
  5. Why does one declare reference variables?
    Answer:
    To refer to objects symbolically within a program. In Java, objects are passed by reference and primitives are passed by value.

  6. When designing OO software, which part do you focus on designing first?
    Answer:
    With OO Software development, we focus on designing the applications data structure first. (Map, HashMap, TreeMap)
  7. What is an object from a software perspective?
    Answer:
    An object is a software construct that bundles together state (data) and behavior (functions) which, taken together represents an abstraction of a real-world (physical or conceptual) object.
  8. What is the difference between the following maps?
    HashMap<String, Object> map = new HashMap<String, Object>();
    Map<String, Object> map = new HashMap<String, Object>();
    

    Answer:
    There is no difference between the objects. There is a difference in the interface you have to the object. In the first case, the interface is
    HashMap<String, Object>, 
    

    in the second it is Map <String, Object>. The underlying object, though, is the same. The advantage to using Map<String, Object> is that you can change the underlying object to be a different kind of map without breaking your contract with any code that is using it. If you declare it as HashMap<String, Object>, you have to change your contract if you want to change the underlying implementation.
  9. What are reference variables in Java?
    Answer:
    The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types are the boolean type and the numeric types. The numeric types are the integral types byte, short, int, long, and char, and the floating-point types a) float and b) double. The reference types are 1) class types, 2) interface types, and 3) array types. There is also a special null type. The values of a reference type are references to objects. All objects, including arrays, support the methods of class Object. String literals are represented by String objects.
  10. What is the purpose of the this keyword in Java?
    Answer:
    The most common reason for using the "this" keyword is because a field is shadowed by a method or constructor parameter. The Point class was written like this
    public class Point {
        public int x = 0;     
        public int y = 0;	
        //constructor
        public Point(int a, int b) {
    	x = a;
    	y = b;
        }
    }
    

    But it could have been written like this:
    public class Point {
        public int x = 0;     public int y = 0;
        //constructor
        public Point(int x, int y) {
    	this.x = x;
    	this.y = y;
        }
    }
    

    Each argument to the constructor shadows one of the object's fields. Inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use "this.x".