Java programming language

Java programming language 

The Java programming language is a common choice for developing cross-platform applications and systems. Additionally, it has attracted more attention because of the popularity of Android mobile phones in recent years. Therefore, many companies have requirements on their candidates' Java proficiency level.
Java Keywords
Similar to C++ and C#, there are many interview questions on Java keywords or concepts. One of the most frequently met questions is: What are the uses of finalize, finally, and final in Java? The finalize method is related to the garbage collector in Java. Java is a managed programming language, and its runtime periodically reclaims memory occupied by objects that are not referenced by others. If an object is holding some resources besides memory, such as files and network connections, we might want to make sure these resources are released before the object is destroyed. Java provides a mechanism called finalization to handle such situations. We define specific actions in the method of finalize that will occur when an object is about to be reclaimed by the garbage collector.
The keyword finally is related to the exception handling mechanism. The finally block always executes when the corresponding try block exists. This ensures that the finally block is executed even if an unexpected exception occurs. Usually, the finally block contains code to clean up resources.
The keyword final in the Java programming language is used in several different contexts to define an entity that cannot be modified later:
  • No classes can derive from final classes.
  • A final method cannot be overridden by subclasses.
  • A final variable can only be initialized once, either via an assignment statement at the point of declaration or a constructor. Some final variables, named blank final variables, are not initialized at the point of declaration. A blank final variable of a class must be definitely assigned in every constructor of the class in which it is declared. Additionally, final arguments, which are declared in argument lists of methods, are similar to final variables.
    The use for final variables gets more complex when they are references. If a final variable is a reference, it cannot be rebound to reference another object. However, the object it references is still mutable.

    The piece of code in Listing 2-18 uses final variables. Which methods will cause compile-time errors? Listing 2-18. Java Code to Modify Final Fields
    public class WithFinal {
        public final int number = 0;
        public final int[] array;
    
        public WithFinal() {
            array = new int[10];
            for(int i = 0; i < array.length; ++i)
    
    array[i] = i;
    }
        public void ModifyFinal1() {
            ++number;
    
    }
        public void ModifyFinal2() { 
     for(int i = 0; i < array.length; ++i)
    array[i] += i;
      }   
    public void ModifyFinal3() {
        array = new int[10];
    for(int i = 0; i < array.length; ++i)
                array[i] = i;}
    }
    
    In the class WithFinal, there are two data fields. The field number is a primitive while the field array is a reference and it is also a blank final variable.
    Blank final variables can be initialized in constructors, so the constructor method in the class WithFinal has no problems. It tries to modify the constant primitive number inside the method ModifyFinal1, which is not allowed by the Java compiler. The method ModifyFinal2 modifies the object referenced by array, but it does not modify the reference itself. It is allowed to do so. Inside the method ModifyFinal3, a new array is created and it is rebound to the constant reference array, so it raises compiling errors.
    www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

No comments:

 
Top Blogs