Tuesday, July 15, 2014

Why main method is public static void in java

HelloWorld.java is a sample program which contains main method.
To Compile: javac HelloWorld.java
To Execute: java HelloWorld

Lets see what is meaning of public static and void keyword in main() declaration

·         public: The main method is called by JVM when we execute “Java HelloWorld”. But since the JVM is out of scope of main method of the class HelloWorld. to make it accessible by JVM, so main method is public in java.

·         static: static keyword allows us to access members without creating object of that class with the help of class name. When JVM calls main method, it does not have any instance of the class having main method. JVM calls main method internally using class name like HelloWorld.main().So main method is static in java.

·       void: when any method does not returns any value its return type must be void. As main method does not return any value, so main method is void in java.

Saturday, July 12, 2014

Does Java support Multiple Inheritance?

Answer to this question is yes, java does not support multiple inheritance but by introducing the concept of interface it just gives the feeling of multiple inheritance but still it is not multiple inheritance.

Multiple inheritance means inheriting more than one class into single class. A class may implement more than one interface but it can inherit only one class. Interface does not contain method implementation but it contains abstract methods which need to be implemented by the class which uses that interface. So it just gives feeling of multiple inheritance but it is not multiple inheritance as the class which uses those interfaces must implement the methods present in those interfaces.

Multiple inheritance gives rise to diamond problem. Let us see that this problem is.

In the above diagram, two classes B and C are derived from same class A and class D is derived from two classes B and C using multiple inheritance. The problem with above type of inheritance is that when an instance of D is calling any method of class A, it is not clear whether to call version of method derived from class B or class C. You can see in the figure above that the classes essentially form the shape of a diamond, this problem is called the diamond problem.

In the designers' opinion, multiple inheritance causes more problems and confusions than it solves. The use of multiple inheritance results into diamond problem so for the sake of simplicity, multiple inheritance is not supported in java. 

Java is Compiled and Interpreted Both

It seems little confusing for java beginner to understand the exact difference between compilation and interpretation. Java is compiled language because java code is compiled to get byte code. Java is interpreted language because this byte code is interpreted into native code.

Actually getting output from java program is two steps process compilation and interpretation. The executable used for these steps are java compiler (javac) and java interpreter (java). First step is compilation in which the java compiler compiles the source code to generate byte code in the form of .class file. Actually this byte code is not machine code but it is just an intermediate code. In the second step java interpreter uses this byte code to generate native code for under lying operating system. The JIT (Just In Time) compiler comes into action in second interpretation step. The JIT reads the byte code to generate native code dynamically so that program runs fast.


Finally in short, java is compiled to byte code which is then used by JVM where it gets interpreted to native code. So java is compiled and interpreted both.

What is JVM JRE and JDK

JVM, JDK and JRE are most commonly used in java programming. Many people think that these are same or gets confused about their differences. JVM, JDK and JRE are interlinked with each other and works together in cooperative manner.
  •  JVM (Java Virtual Machine): Interpreter + JIT
JVM is code executing component of Java that executes byte code (.class file). The output of Java compilation is byte code and only JVM can understand the byte code. JVM interprets this byte code to native code depending upon underlying processor and architecture. JVM has two main components interpreter and Just In Time (JIT) compiler. JIT is compiler which helps for optimizing byte code to machine code conversion and hence results into reduced execution time.  Note that Java is platform independent but JVM is not platform independent. Each JVM has its own structure of executable which differs from platform to platform. For each OS there is different JVM available. So JVM is highly platform dependent.
  • JRE (Java Runtime Environment): JVM + byte code verifier + class loader
JRE contains an environment for execution of Java program. JRE consist of JVM, class loader and byte code verifier. Class loader module loads all the .class files (byte code) and required libraries which are then verified by byte code verifier. Then this verified code is interpreted by JVM which creates native code.
  •  JDK(Java Development Kit): JRE + Development tool
JDK contains tools like compiler and debugger to develop Java program. It provides predefined set of libraries, API classes and other files which is used by JVM at runtime. It also includes JRE, interpreter (java), compiler (javac), document generator (javadoc).


Setting Path Environment Variable in Java


For the beginner one of the difficult and confusing task is to set path and classpath environment variables.
Path is nothing but setting up an environment for operating system. Path is a system variable used to tell to Operating System all locations of executable files. Operating System will look in this path variable for executable files.
Classpath is nothing but setting up an environment for java. Classpath is pure java variable, which is used to tell the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages.

For example,assume that Java is installed in C:\Program Files\Java\jdk1.7.0_25\ and your code is in C:\Sample\example.java, then path and classpath should be set to like this:
classpath=C:\Sample
path= C:\Program Files\Java\jdk1.7.0_25\bin;
Or
path=%JAVA_HOME%\bin;
where JAVA_HOME is pointing to Home directory “C:\Program Files\Java\jdk1.7.0_25”, where jdk is installed

Let us see what exactly means by setting path variable
Whenever you try to compile a program using javac, JVM will search for javac in current directory where your program is residing, If it does not find javac and other executables in current directory then it will search in the directories where the path variable is set to. So setting path variable allows you to execute your program from any directory as location of executables java and javac is retrieved through path variable.
If you have not set path variable then it will show error like “javac is not a recognized command” . hence to remove this error set path environment variable as follows:
  • Command to set path variable from command prompt

·         Set path=”C:\Program Files\Java\jdk1.7.0_25\bin”
·         Set path=%path%;”C:\Program Files\Java\jdk1.7.0_25\bin”
Here in second command we are just adding our path value to existing value of path variable.
  • Command to remove value from path variable

·         Set path=
This will remove directory path from environment variable.
  • Check whether environment variables are set correctly or not

·         If your environment variables are set correctly, then windows should recognize these commands.
C:\> java OR
C:\> javac
·         you can see current value of path variable by following command
C:\> echo %path%

Sunday, July 7, 2013

Ubuntu: How to sort file contents using command ?


In this article, I will share my understanding of sort command with some examples. One important point to notice is that the file contents must be on separate line. If file contains only single line then we are not able to observe the effect of sort command. Before jumping directly to examples, here are some of its variations

To sort file contents                             : sort filename.txt
To sort according to column contents   : sort –k (column number) filename
To sort in reverse order                       : sort –r filename
To remove duplicates while sorting      : sort –u filename
  •  Sorting numeric contents
           Suppose file contains only numeric data then file contents are sorted in ascending order of integer values as shown below.
$ cat file
015
004
016
009
003
018
002
020

$ sort file
002
003
004
009
016
018
020
  •   Sorting alphabetic contents
            Suppose file contains only alphabetic data then file contents are sorted alphabetically as shown below.
$ cat file
Ashish Gaigole
Siyaram Vishwakarma
Deepak Jaiswal
Dashrath Sharma

$ sort file
Ashish Gaigole
Dashrath Sharma
Deepak Jaiswal
Siyaram Vishwakarma
  •  Sorting alpha-numeric contents
           In case file has both numeric and alphabetic data then first all numeric data is sorted in ascending order followed by alphabetic data.
$ cat file
Ashish Gaigole
Siyaram Vishwakarma
Deepak Jaiswal
Dashrath Sharma
015
016
004
009

$ sort file
004
009
015
016
Ashish Gaigole
Dashrath Sharma
Deepak Jaiswal
Siyaram Vishwakarma
  •  Sorting file contents according to particular column values
$ cat file
Ashish Gaigole 016 F-42
Siyaram Vishwakarma 015 F-43
Deepak Jaiswal 009 F-44
Dashrath Sharma 004 F-41

$ sort –k 3 file  : file is sorted according to third column in file.
Dashrath Sharma 004 F-44
Deepak Jaiswal 009 F-41
Siyaram Vishwakarma 015 F-43
Ashish Gaigole 016 F-42

$ sort –k 4 file  : file is sorted according to fourth column in file.
Deepak Jaiswal 009 F-41
Ashish Gaigole 016 F-42
Siyaram Vishwakarma 015 F-43
Dashrath Sharma 004 F-44                         

Saturday, May 18, 2013

Ubuntu: ”How to recover root password ?”


Situation may arise that you want to login as root user and you don’t  know or you forgot the root password. In this case we need to boot in recovery mode. In some cases recovery mode option will not prompt automatically so need to press left shift key while booting. This will prompt option to boot in recovery mode. Once you boot in recovery mode, various options will be available. 

Selecting root option will login you as root in terminal without any password. Now you can create new root user, you can change the password of existing root user. 






After you have successfully changed the password, you need to reboot the system and now you can login with your new password.






For this just refer following commands.

You can get the list of current user                                 cat  /etc/passwd              
                                                  
You can add new user to the system                              useradd username  

 You can set the password of the user                             passwd username