What is Java?


Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

 

The Magic Bytecode


Java is a hybrid language. That is it is first compiled to generate .class files (bytecode). This bytecode is then interpreted by Java Virtual Machine to generate machine code. This makes java a platform independent language. You compile a file in any platform you will get the bytecode. You can run it in any system which has a JVM. Of course JVM should be implemented for each platforms separately.Every time a .class file is interpreted, JVM checks the possibility of overflow and pointer arithmetic. If all tests are passed then file is executed successfully. JVM acts as a software layer just above the Operating system and interprets bytecodes into machine codes.

 

JRE, JVM, JDK


The Java Runtime Environment (JRE) is what you get when you download Java software. The JRE consists of the Java Virtual Machine (JVM), Java platform core classes, and supporting Java platform libraries(JDK). The JRE is the runtime portion of Java software, which is all you need to run it in your Web browser.The JVM is only one aspect of Java software that is involved in web interaction. The Java Virtual Machine is built right into your Java software download, and helps run Java applications.

 

Installing Java


Inorder to work with Java programs, You should download and install JAVA.To check whether you have installed JAVA in your system, simply open a terminal and type “java” and press “ENTER”.If installed, then you can see a list given below:java_terminal

else you would get an error message like:

” ‘java’ is not recognized as an internal or external command,operable program or batch file.”.Then go to JAVA’s official download page and download the latest version that suits your platform.Click here. Follow the installation steps .

 

Setting Up Environment Variables


What is an Environment Variable?

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.They are part of the operating environment in which a process runs. If you don’t get the meaning leave it.For the time being, just understand that once they are set properly, you can run a Java program in any directory of your choice. Otherwise you should manually specify the classpath(like “C:\Program Files\Java\jdk1.8.0_05\bin”) of java, everytime you open a terminal. This is because when you compile a .java file, there may be external references to many other java libraries. Since system doesn’t know the path of libraries, it will search the current directory where your java program exists. If current directory does not has library needed at the moment, system looks for  environment variable, which stores the path of java bin files. Alternatively you could create programs inside the java bin directory.But it is not an good way of programming.

Now we shall set up the Environment Variable .

Goto System Properties -> Advanced System Settings ->Environment Variables.You will find a window given below:

env_var

Under user variables ,edit variable ‘PATH'(create if doesn’t exits).It may contain several other paths separated by ‘;’. Just specify the path to your java bin folder, usually it wll be like-“C:\Program Files\Java\jdk1.8.0_05\bin;”. Thats it. We are done with environment variables.

To check whether that environment variable is properly set or not, open a new terminal and type “javac” .If you don’t get a message like ” ‘javac’ is not recognized as an internal or external command,operable program or batch file.” then congrats , you have successfully set up the environment variables.

 

HelloWorld Program in Java


Ok guys, its time to write a simple HelloWorld program in java. Unlike in c++, the main function in Java is inside a class.In a program file, only one class should be made “public” and the name of file should exactly match the class name  and this is compulsory in java. Otherwise the program will not compile successfully.

Open a text editor and create a new file named “Helloworld.java”. All the java files have .java as extension.Now type the following code in it.

public class HelloWorld{
    public static void main(String args[]){
        System.out.println("Helloworld");                                                                     }
    }
}

Open a new terminal and change the terminal directory to the path containing your program.

To compile: javac Helloworld.java                                                                                 To run: java HelloWorld

Please note that to run your file, you don’t need to explicitly specify the .class extension. JVM shall handle that. If your program has no error you should get the output as below:

runjava

And that’s it. You have successfully executed your first java program.