Java Blogs are millions in number. I wanted to write this blog, just to make it easy for myself during interview preparations and also a name tag of technical blog I would get out of it.
Java is a very easy language compared to C , C++. That is a wide opinion. Others feel Java is COBOL of modern age, used by all, but not so efficient. Opinions apart, most machines in the world Java byte-codes. So we learn Java and eventually end up as a developer primarily use Java.
Lets talk about initial phone interview questions.
Easy phone interview questions:
1. What is a Final class for ? Final method ? Final variable ?
1.1. What is Finally ?
1.2. Explain Finalize?
In other words, this question is famously know as, Final,Finally,Finalize
(I don't know how they are related !!!)
I am writing a class that contains lot of confidential information. No other person should be able to access the class without an object for it, which means, no other class should be able to just extend this class and use its methods or variables. I want to end the chain of this class, so I make it Final. No other class can extend it. Final class gives security to the program.
What if I have just a block of code that should be secured ? So we create a method and mark it as Final. So other classes can extend the class, but they cannot override (change/re-write) the Final method in their classes.
I don't even have a block of code to be secured, just a variable. So make it a Final variable. In this case, the variable cannot be changed once initialized.
Finally - This is a part of try-catch block. Finally is executed irrespective of the execution of try or catch block. If we feel like some part of code might throw some error, we put it in try block. If the block is not executing properly, or there is some error, we will catch that exception in catch block. So either try block is executed or catch block is executed. If we want something to be executed after both of them, we place it in finally block.
eg. We can give system shutdown there. We can give some killing jobs there. Clean up code can be there.
It is like switching off your stove after cooking anything.
One common question, if there is a break in try block, will finally get executed ? Yes. finally gets executed. Only System.exit() prevents finally from getting executed.
Finalize is garbage collection mechanism in Java. Java garbage collection can be done manually by using finalize method.
Java is a very easy language compared to C , C++. That is a wide opinion. Others feel Java is COBOL of modern age, used by all, but not so efficient. Opinions apart, most machines in the world Java byte-codes. So we learn Java and eventually end up as a developer primarily use Java.
Lets talk about initial phone interview questions.
Easy phone interview questions:
1. What is a Final class for ? Final method ? Final variable ?
1.1. What is Finally ?
1.2. Explain Finalize?
In other words, this question is famously know as, Final,Finally,Finalize
(I don't know how they are related !!!)
I am writing a class that contains lot of confidential information. No other person should be able to access the class without an object for it, which means, no other class should be able to just extend this class and use its methods or variables. I want to end the chain of this class, so I make it Final. No other class can extend it. Final class gives security to the program.
What if I have just a block of code that should be secured ? So we create a method and mark it as Final. So other classes can extend the class, but they cannot override (change/re-write) the Final method in their classes.
I don't even have a block of code to be secured, just a variable. So make it a Final variable. In this case, the variable cannot be changed once initialized.
Finally - This is a part of try-catch block. Finally is executed irrespective of the execution of try or catch block. If we feel like some part of code might throw some error, we put it in try block. If the block is not executing properly, or there is some error, we will catch that exception in catch block. So either try block is executed or catch block is executed. If we want something to be executed after both of them, we place it in finally block.
eg. We can give system shutdown there. We can give some killing jobs there. Clean up code can be there.
It is like switching off your stove after cooking anything.
One common question, if there is a break in try block, will finally get executed ? Yes. finally gets executed. Only System.exit() prevents finally from getting executed.
Finalize is garbage collection mechanism in Java. Java garbage collection can be done manually by using finalize method.