Tuesday, July 15, 2014

Why Java is preferred over C and C++

When it comes on selecting a programming language, we need to consider few parameters like security, robustness, ease in programming and few other parameters, because of below mentioned parameters java is preferred over C++ :

·         Robust:  Java is robust because of two reasons memory management and exception handling. In C,C++ programming 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.

·         No Pointers: Use of pointer is avoided in Java. In C++ programming memory references can be manipulated but in Java, you cannot manipulate references which in turn provide secure memory access.

·         Platform Independence and Portability: Traditional languages C, C++ were designed to compile for specific platform (processors, OS). In Java executable code runs over JVM platform which is independent of OS. So once we have byte code (.class file) we can execute it on any OS. So Java code needs to be compiled only once and we can execute it any number of times across different platforms (processors, OS). So Java provides platform independence and portability.

·         Secure: Java provides sandbox security model for applet execution which creates an environment which provides restricted access to OS resources and allows user to run un-trusted code from unknown source safely. All Java programs run over JVM platform and not on Operating system. So any OS related updates or modifications do not affect execution of Java program.  Java uses byte code verifier module which checks Java code automatically before executing it.


·         Multithreading : its support to multithreading made java enabled for multitasking which allows available resources to be used optimally. Java has built in support for multithreading but in C++, multithreading is entirely dependent on Operating System.

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%