Search This Blog

Friday 4 November 2016

What is JVM, JRE and JDK

Java Virtual Machine (JVM)
The Java Virtual machine (JVM) is the virtual machine that run the Java bytecodes. The JVM doesn't understand Java source code.
You compile your *.java files to obtain *.class files that contain the bytecodes understandable by the JVM.
JVM makes Java to be a "portable language" (write once, run anywhere).
There are specific implementations of the JVM for different systems (Windows, Linux, MacOS) the aim is that with the same bytecodes they all give the same results.

Java Runtime Environment (JRE)
The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language. The JRE does not contain tools and utilities such as compilers or debuggers for developing applets and applications. If we want only to execute the programs written by others, then JRE alone will be sufficient.

Java Development Kit (JDK)
The JDK is a superset of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications.


String literals in switch statements

Prior to Java 7, only integer values were the valid arguments in a switch statement. It is very common to make a decision based on a string value, up till now developers have been using if statements whenever there is a need to make a decision based on String values, use of String in switch statement result in more readable and efficient code.


Code Snippet to display the use of String in Switch Statement

String day;
            day= "";
            switch(day){
            case "Monday" :
                  System.out.println("1st Day");
                  break;
            case "Tuesday" :
                  System.out.println("2nd Day ");
                  break;
                  default:
                        System.out.println("Holiday ");
            }

When using strings, you need to be careful about the following two issues:
ü  Null values for strings : Using a string reference variable that is assigned a null value will result in a java.lang.NullPointerException.
ü  The case of the string : The evaluation of a case expression is case sensitive in a switch statement.

 . . . read more