Beginners World
A Blog for the beginners to the world of programming.
April 26, 2021
System class of the java
March 16, 2021
Garbage Collection
As we all know that java programming language is robust and had so many features available and also adding features versions by versions. Like garbage collection, it supports this feature from the very early versions of the java.
What garbage collection do is it destroys the unnecessary resources from the memory. This is the thing which java does automatically, which we can not see with the help of programming.
But still this feature is very much helpful when we are creating very large scale applications or some scientific applications. We can not control the way it destroys the resources from the memory, but what we can do is simply force the compiler to free the resources. First we need to understand that this process is completely automatic, by forcing the compiler to free the resources means that we are giving instructions to the system that this the resource which we don't want to use it can free the resource from the system.
This is not necessary to implement garbage collection in each and every program or application of the java , as it is part of the automatic process, but in the large scale applications it is in constant need to free memory at some point so at that time we can force the system to free the unnecessary resources for the smoothness of the application.
To implement the garbage collection we need to call a built in method "System.gc()", and implement finalize() method in your class. Here below is the simple example of Garbage collection.
class MyTest{
static int cnt = 0;
MyTest() {
System.out.println(cnt + " Object is Created");
}
{//Simple block for increment of the counter when object is created in memory
cnt++;
}
static void showCnt(){
System.out.println("Total objects are created : " + cnt);
}
protected void finalize() {
System.out.println("Object Destroyed : " + cnt);
cnt--;
}
}
class MyClass{
public static void main(String[] a) {
MyTest t = new MyTest();
for(int i =0 ;i < 5 ; i++)
new MyTest();
MyTest.showCnt();
System.gc();
MyTest.showCnt();
}
protected void finalize() {
System.out.println("MyClass Destroyed");
}
}
Type the above example save it as any name you want and run the MyClass when you run the above program you every time you will see different output .Below is the screenshot of the output I had when I had run the same program.
March 08, 2016
What is Multi threading in Java?
Take an example of Microsoft word, where it is doing the spell checking at the same time we writing the document. The writing of document, and checking spells are two different threads which is running simultaneously.
Do not get confused multi threading by multiprocessing. This difference is clearly visible in the below image.
Advantages of Threads
- Light weight process
- Thread creation is 10-100 times faster than the process creation (because when each and every process is creating it need to be occupied a separate memory slots where as in threads uses the current memory slots)
- Lower context switching overhead
- All threads within a process shares same global memory
- It is the thread from where other child thread will be created
- Often it must be the last thread to finish the execution because it performs various shutdown actions.
December 20, 2015
Unrechable catch block error in exeception handling
When we are using multiple catch blocks at that time the order of the catches are matter most. It means that all catch blocks must be from most specific one to most general ones. which is sub classes of Exception must come first and then the super classes. If we change the order of the catch block the compiler will give the error of unreachable block. Take a look at the following code snippet.
public class ExceptionTest
{
public static void main(String[] args)
{
try
{
int i = Integer.parseInt("abc"); //This statement throws NumberFormatException
}
catch(Exception ex)
{
System.out.println("This block handles all exception types");
}
catch(NumberFormatException ex)
{
//Compile time error
//This block becomes unreachable as
//exception is already caught by above catch block
}
}
}
December 03, 2015
What is an Exception ?
- An Exception is an abnormal condition that arises during the execution of any program.
- An exception is a run-time error. In computer languages that do not support exception handling, errors of the program must be checked and handled manually.
- Java's exception handling avoids these problems by providing a wide range of exception classes.
- A java exception is an object that describes the exceptional (i.e., error) condition that has occurred in a program.
- When an exceptional condition arises, an object representing that exception is created and thrown to method that caused the error.
- try
- catch
- throw
- throws
- finally
- The code / program statement to be monitored for exception are to be written in try block.
- If any exception occurs within try block, it is thrown and our code can catch that exception using catch block and handle it in some of the very user friendly manner.
- System generated exceptions are automatically thrown by the Java run-time system.
- To manually throw an exception, we can use the throw keyword.
- Any exception that is thrown out of a method must be specified using throws keyword.
- The finally block is a block is executed compulsory. A code which we want to be executed for sure whether exception occurs or not, like releasing resources, closing database connections, closing stream connections, etc, we can write that code in finally block.