Tuesday, July 15, 2014

Why java is Secure

When it comes across the security issue of programming languages, Java is surplus over traditional programming languages like C and C++. Java is more secure than C and C++ for the following reasons:

·         Its JVM and not OS: All Java programs run over JVM platform and not on Operating system. So any OS related updates or modification does not affect execution of Java program.

·         Sandbox Security Model: Java applets are executed inside sandbox which creates an environment, which provides restricted access to OS resources and allows user to run un-trusted code from unknown source safely.

·         No Pointer Manipulation: In traditional programming memory references can be manipulated but in Java we cannot manipulate references. Thus you cannot cause an object reference to point to an arbitrary memory location, which in turn provides secure memory access.

·         Byte Code Verifier:  Byte code verifier checks correctness of class files and API libraries. Java uses this byte code verifier module which checks Java code automatically before executing it.

·         Array Boundary Check: Java provides run time array boundary check which is not provided in traditional programming.

·         Packages and Access Modifiers: This combination of packages and access modifiers allows your class to have detailed knowledge of each other, but not expose that knowledge outside that package.

·         Run Time Exception Handling: Exceptional conditions may arise in traditional programming which may lead to abnormal termination of programs but Java provides exception handling which helps to handle and eliminate this abnormal termination.

·         Dynamic Memory Allocation and De-allocation: In traditional programming C,C++ dynamic memory management is very difficult as memory allocation and de-allocation must be done by programmer but Java does this dynamic memory allocation and de-allocation without any extra efforts. Java uses new operator to dynamically allocate memory and Garbage Collection technique to handle automatic memory de-allocation.


·         Two Level of Code Checking: Code is checked two times, first at compile time and then at run time which can be considered as more secure than single level of code checking.

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