71-80 Flashcards
Is JDK required on each machine to run a Java program?
JDK is development Kit of Java and is required for development only and to run a Java program on a machine, JDK isn’t required. Only JRE is required.
What’s the difference between comparison done by .equals method and == operator?
In Java, equals() method is used to compare the contents of two string objects and returns true if the two have same value while == operator compares the references of two string objects.
Is it possible to define a method in Java class but provide it’s implementation in the code of another language like C?
Yes, we can do this by use of native methods. In case of native method based development, we define public static methods in our Java class without its implementation and then implementation is done in another language like C separately.
How are destructors defined in Java?
In Java, there are no destructors defined in the class as there is no need to do so. Java has its own garbage collection mechanism which does the job automatically by destroying the objects when no longer referenced.
Can a variable be local and static at the same time?
No a variable can’t be static as well as local at the same time. Defining a local variable as static gives compilation error.
Can we have static methods in an Interface?
Static methods can’t be overridden in any class while any methods in an interface are by default abstract and are supposed to be implemented in the classes being implementing the interface. So it makes no sense to have static methods in an interface in Java.
In a class implementing an interface, can we change the value of any variable defined in the interface?
No, we can’t change the value of any variable of an interface in the implementing class as all variables defined in the interface are by default public, static and Final and final variables are like constants which can’t be changed later.
s it correct to say that due to garbage collection feature in Java, a java program never goes out of memory?
Even though automatic garbage collection is provided by Java, it doesn’t ensure that a Java program will not go out of memory as there is a possibility that creation of Java objects is being done at a faster pace compared to garbage collection resulting in filling of all the available memory resources.
So, garbage collection helps in reducing the chances of a program going out of memory but it doesn’t ensure that.
I want to re-reach and use an object once it has been garbage collected. How it’s possible?
Once an object has been destroyed by garbage collector, it no longer exists on the heap and it can’t be accessed again. There is no way to reference it again.