Saturday, March 28, 2015

How to run java program | Java jdk | Java runtime

To compile and run java program you need JDK that is java development kit installed in your computer. You can download JDK from oracle website or from here by clicking the following link


After installing jdk you need to set variable path. In order to do that please follow the following steps:
  1. Goto C:\ drive and open program files and in that open java folder. In that you will see jdk1.8.0_20 folder. Open it. In this you will see bin folder open it up and copy its path.
  2. Now right click on my computer folder on desktop and go to properties. In that in the left hand side you will see Advanced System Settings. open it and a pop up will come. On advance tab click on environment variables. Click on new. On variable name you have to write "path" and in path copy that address of bin folder which we had copied in step one.
  3. Now select that new variable and click OK. Now the environment variable is set.

The following are the steps to write and run our first java program using notepad and cmd:-
  1. Open notepad and write the same code snippet and save it with name First.java
  2. Open command prompt(cmd) and write cd.. This command is used to change directory. Change it till you get it into that folder in which your First.java resides.
  3. Now write javac First.java. This will compile our program. Javac is for java compiler.
  4. Now write java First. It will run our program and you will see the output which is Hello World! in our case.
The following are  the steps to run program in eclipse software:First you need eclipse software. You can download it by clicking on the link below:

  1. Open up this file  and eclipse will be launched but if you are getting trouble then you have to download it from internet.
  2. Go to the file menu and point to new and then click on other . A pop up will appear. Write dynamic web project there and then specify the name of the project then click next and simply your project will be created.
  3. If it is asking for Java EE then click no.
  4. Now click on the project that you just created and on src folder right click and then point to new and then click on package.
  5. Then write package name generally we write like this package name:"com.company_name.project_name".
  6. Click on finish.
  7. Now a package is created in your src folder of project.
  8. Now right click on package and point to new then click on class.
  9. Now write class name of your wish which is First in our case and then click OK.
  10. Now write the whole code in this class and run it from top and you will get output below the program in console.
From now onwards we will be writing our programs mostly in eclipse.

 
read more

Java compiler | How Java Program Runs | Java Virtual Machine


The Java program runs in two steps:-
  1. First the source code is compiled using java compiler known as "JAVAC". It converts the source code(human-readable) into Byte Code which is not human-readable.This byte code has .class file extension.
  1. Next this byte code id handled by JVM which is specific for a system. JVM converts this byte code into the output which is shown on standard output device that is screen.
read more

Simple Java Program | Java Hello World Program | How to Program | Learn to program

Here we are starting up with our first java program. The following code snippet is our first simple java program. It just prints "Hello World!".


//call this program First.java
class First
{
 public static void main(String args[])
{
  System.out.println("Hello World!");
}
} //ends here

Description of the Program:
  • The first line of the program is the comment. It just gives the important information about our program. It is not an executable(runnable) statement. There are other ways also by which we can enter comments in the program which we will discuss later.
  • The second line of the code is the starting statement. It defines a class named First(it can be any name). Class is the container in which whole program resides. The rule says the first letter of class name should be capital letter. We will dealing with more sophisticated classes later.
  • The third line is the curly bracket({). It tells the starting of the program.
  • Next line is the main method. From here the program execution starts. It has been declared public so that it is accessible from everywhere in the program. Public is an access specifier. It is declared static so that is does not need to be instantiated(need not to make object). Next is void that means this method is not returning anything. In the bracket we have declared an array named args which is made string type. It saves whatever the program doing.
  • Next line is again curly bracket which marks the beginning of main method.
  • Next line is the output statement. It has many things. System is telling  the output should be on the standard output screen. Out is for output. Println is printing , here "ln" means it will send pointer to next screen after printing the line.
  • Next is the curly brackets. If we are opening two brackets then we have to  close two brackets.
read more

Java Buzzwords

  • Simple:-As java derives its syntax from C and C++ so the programmer who knows C and C++ will have to put little effort in learning java . Even if you are not an experienced programmer then also you can master java will hard work.
  • Secure:-As you know when you are downloading a program from internet your computer is at a risk because the program you are downloading may contain virus like malware or Trojan horse.This virus may gain access to the local files of your computer and may get private information like passwords or CREDIT CARD number etc. When java applets were downloaded this security issue had to be considered so java achieved this protection very effectively by keeping the control of applet to java execution environment only ,it can not access the local files on your computer. This was the major achievement of java . Thus java is secure.
  • Portable:-There is a famous phrase of java that is "Write Once Run Anywhere". It literally means that you just need a java virtual machine on the computer . On that you can run your already compiled program (Byte Code). I know i am confusing you but hold on you will get to know this concept in more detail when when learn about the class file that is  how the java program compiles and runs. Portability was important concept because suppose there is an applet you want to download then this should be able to be downloaded on any operating system because different operating systems have different configurations and having different OS(Operating System) for every applet would not be possible.
  • Object-Oriented:-As java is derived from C++ which is an Object-Oriented Programming Language, so java is also object-oriented. It considered that everything is an "object". You will gradually learn about the power of objects and classes later in this tutorial.
  • Robust:- Robust actually means Strong. Java is a strictly typed language . It prevents you from making common errors as it checks them during compile time and hard-to-track errors are tracked during run-time. One more reason why java is robust is that it has its automatic garbage collection system unlike in C or C++. In C or C++ you have to free up the memory manually (by use of destructors). But in java all the memories taken by variables are deallocated automatically when there is no reference to that memory.
  • Multithreaded:- Java was designed to meet the real world requirements of multitasking. Java allows you to write programs that do multi-task simultaneously.
  • Architectural-neutral:- Earlier when you wrote a program and if you upgrade the OS then the program use to malfunction. This problem was also solved by introducing Java Virtual Machine(JVM). We will learning about JVM shortly.
read more

Evolution Of Java | What is java | Basic Programming

Java is a Programming language. Java enjoys legacy from two languages that is C and C++. From C it inherits syntax and OOPs (Object-Oriented Programming language) concepts come from C++. C++ is not a new language it is extended version of C language only thats why it was earliar known as "C with classes".

Gradually Internet came then the need was felt for more secure language and Java was the solution to this situation.The success of Java is too important to ignore. Java's importance can only be  understood  by understanding java buzzwords. Following is a list of  java buzzwords.We will be shortly learning each of them.

The Java Buzzwords are:

  •  Simple
  •  Secure
  •  Portable
  •  Object-oriented
  •  Robust
  •  Multithreaded
  •  Architecture-neutral
read more