Saturday 21 November 2015

Writing JAVA

Writing and Running Your First Java Program

Java source code can be written easily without the need of any special software for instance it can be written in notepad or notepad++. I personally like notepad++ as it looks good and its free, so we'll be doing an example in notepad++(Download notepad++

Make a test folder in your C: drive. Open notepad++ and save the file as world.java in test folder.



























Both the .java extension the location are very much essential as the Java compiler can only run files that are .java and also it should know the location of the document it is suppose to run.

Here we're taking the example of a very famous program called "Hello World".


class world{
public static void main(String args[]){

        System.out.println(""Hello World"");

        }
}

Don't worry about the syntax of the program we'll be covering that part later, just copy paste the given program.

After writing the program save the file. Next step is to give the file to Java compiler and get an output.

For this the path of the document should be known by the javac. So open command prompt and write cd C:\ which stand for change directory to C:\ and basically goes back all the way to C:\ folder. After that write cd test to change the directory from C:\ to C:\test. For checking the files present in C:\test write dir as your next command in command prompt to see all the files and directories in C: drive. One of these files is world.java.


Compile the world.java file in the test folder for which write javac world.java in command prompt. This step will result into generation of a file called world.class which can be run to get the output of the java source code.






























Somethings which might be confusing that world.class is generated as our file name was world.java is incorrect. The reason why world.class has been generated is because the class of source code is mentioned as world. Experiment with this change the file name and class, do some permutation combination and you will have a better understanding of this concept.




















To get the output of written code write java world as the next command in command prompt and you will get the output as Hello World. And now we've finally run the java source code and got an output.



Now one of the main function of compiler is to check the source code for errors. Here we're deliberately making an error in second line where we're passing args String as an argument but should be in small braces but at one end we've used curly braces. All these concepts as String and arguments will be discussed later in the series in great details. Save this file and give this to java compiler to check how compiler catches an error in the source code.

class world{
public static void main(String args[]}{

        System.out.println(""Hello World"");

        }
}


Every now and then no matter how good programmer you're these small mistakes occurs although with continuous practice these errors can be minimized but if you're writing a very long program say a source code having 20-30 java classes all having more than 100 lines each and this happens it becomes next to impossible to find the error. But thanks to the java compiler which will tell us the exact location and type of error that has happened which then can be easily rectified.
























Here the java compiler has exactly given that the error in line 2 as world.java:2:  and the error as error: ')' expected at the place of '}' in public static void main(String args[]}{ . Thus java compiler saves a lot of amount of time at troubleshooting and as soon as the problem is taken care of one can proceed to run the java source code.