April 26, 2021

System class of the java

       In many applications we want to get some of the system properties as some features of java are not being able to be used on older versions of either Java or the operating system. So here I have made an simple command line program just to display all the basic and necessary properties of System class. 

        There is only one method we have to use of the System class is "getProperty()". The method System.getProperty() holds the one argument which is of type String. So if any one who is beginner to this and using notepad, then I suggest to read the argument carefully and type it as it is in the given example. Because if there will any spelling error in it, there are chances of giving it a runtime error. So be very careful about spelling while typing the progrm.


class SystemClass
{
public static void main(String[] ar)
{
System.out.println("1 : " + System.getProperty("java.version"));//Current Version Name
System.out.println("2 : " + System.getProperty("user.name"));//Current Username
System.out.println("3 : " + System.getProperty("user.dir"));//Current user driectory
System.out.println("3 : " + System.getProperty("user.home"));//Home directory of the user
System.out.println("4 : " + System.getProperty("os.name"));//Operating System name
System.out.println("5 : " + System.getProperty("os.arch"));//Operating System Architecture Ex : x86= Microprocessor 8086 architecture
System.out.println("6 : " + System.getProperty("os.version"));//Operating System version
System.out.println("7 : " + System.getProperty("java.io.tmpdir"));//Temporary directory of user
System.out.println("8 : " + System.getProperty("java.ext.dirs"));
System.out.println("9 : " + System.getProperty("java.specification.version"));//Version of java
System.out.println("10 : " + System.getProperty("java.specification.vendor"));//Vendor of java
System.out.println("11 : " + System.getProperty("java.specification.name"));//name of java Specification
System.out.println("12 : " + System.getProperty("java.class.path")); //Classpath of JDK
System.out.println("13 : " +System.getProperty("java.class.version"));//Version of java class
System.out.println("14 : " +System.getProperty("path.separator"));//path separator of classpath
System.out.println("15 : " +System.getProperty("line.separator"));//it will show two blank line
System.out.println("16 : " +System.getProperty("java.vm.name"));//name of JavaVirtualMachine
System.out.println("17 : " +System.getProperty("java.vm.vendor"));//Vendor of java Virtual Machine
System.out.println("18 : " +System.getProperty("java.vm.version"));// version of java virtual machine
System.out.println("19 : " +System.getProperty("java.vm.specification.name"));//specification name of java virtual machine
System.out.println("20 : " +System.getProperty("java.vm.specification.version"));//specification version of java virtual machine
System.out.println("21 : " +System.getProperty("java.vm.specification.vendor"));//vendor of specification of java virtual machine
System.out.println("22 : " +System.getProperty("java.vendor.url"));//main url of java 
System.out.println("23 : " +System.getProperty("java.vendor"));
System.out.println("24 : " +System.nanoTime());
//System.load(System.getProperty("user.dir") + "\\" + "MyClass.java");
}
}

    Type above example and save it as the name you want and run with the name of class. The output of the above code will give something like below.





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?

Simply we can say multi threading is a kind of specialized form of multi tasking. In a thread based multitasking environment, the thread is a smallest unit of dispatchable code. In a single program we do two or more tasks at a time (simultaneously).

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.

Multiprocessing Vs Multithreading


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

Main Thread

When a Java Program starts up, one thread begins running immediately, usually called the Main Thread. This main thread is important for two reasons:
  1. It is the thread from where other child thread will be created
  2. 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.
Java exception handling is managed by the following five keywords:
  • try
  • catch
  • throw
  • throws
  • finally
The following is the basic block of simple exception handling in java:

try{
   //Block of code to be monitored
}
catch(ExceptionType1Object ob){
   //Exception handling code for ExceptionType1
}
catch(ExceptionType2Object ob){
   //Exception handling code for ExceptionType2
}
.
.
.
finally{
   //Block of code to be executed before try block ends
}

Basic description of the above block:
  • 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.