In this blog i will be explaining you about the ways in which you can start off programming in Java...Very soon I will be embedding a video more like a screencast teaching you about using Java.
Well generally whenever you start off with some programming language, the main thought that comes to your mind is "How do i compile the code and where do I deploy????" right....This post will teach you just the basics of Java and all you need to get started with some serious coding.....
I assume that the audience or the readers of this post knows something about programming languages and for a clear understanding of all the concepts I suggest you to read some books on C++ which are present in my books page...
Clarifying all these, let me start off with some history...Java was created by James Gosling and his team in around 1990 in a view to create a platform independent code i.e he was focusing mainly to develop codes for simple toasters and washing machines and ensure that the code need not be changed every time it is targeted to machines of different companies.....What i mean to say is this. Assume a scenario wherein you need to write a washing machine code for both Siemens and LG..In case of primitive languages like C and C++, you would have to write platform or machine specific codes for both Siemens and LG washing machines as the deployment platform are totally different...In case of Java this problem is eliminated....Programmers code a single piece of software and just deploy it on any machine....Confused????? Well don't be......I told that a single Java code can run on any platform....But for the code to run, there is a requirement of a "platform specific layer" or an environment for the Java code to run called JVM or the Java Virtual Machine...Some of you might be thinking what difference it would make if you need a platform specific software again to run......But here is the fact.....In case of C and C++, you will have to recode the entire software as per the specifications of the target platform, compile it wholly and then deploy...In case of Java, this is replaced by a simpler technique. All java code written is compiled by the Java compiler into a Machine independent byte code file called as the .class file. This file an run on any JVM's and hence it is only the JVM's that has to change from platform to platform and not the written code.....This actually reduced the programming burden of platform dependancies...
Now, to start off......
You must have a Java Developer Kit (JDK) installed in your system in order to compile your program. Let me just brief out some steps you will have to follow to program a Java code to say " Hello Viewers ".. Simple enough to make you learn a lot of new concepts...There are two ways by which you can do this. If you are a GUI fan, you can use any IDE's like Eclipse or Netbeans which are free softwares available on the internet...The other is the all-powerful command line execution and compilation which actually is good for many starters as it gives you the core fundamentals of the working of Java codes.
JDK is a freely available software of SUN microsystems. Just download the kit and install. You have an option to chose the JDK package that you wish to download. Generally JDK packages come as Java 2 SE, Java 2 EE, Java 2 SE with Netbeans and many more. If you wish to stick on with just the command line compilation as I told earlier, just download the J2SE package. The most recent at the time of writing this post is the J2SE 1.6 update 21. I would suggest you to download the J2SE package with netbeans as it will be helpful to ease your work when you start developing complex software structures.
After successfully installing the software you have the JDK ready to use with some specifications and also the GUI Netbeans IDE which is pretty decent to operate.
First let me go with the usual command line coding of Java.
Let me show you the code first and then brief you with some other details about compiling. The code is pretty simple.
-------------------------------------------------
class First
{
public static void main(String s[])
{
System.out.println("Hello whoever you are");
}
}
-----------------------------------------------
Thats it...you have the code ready....Just save the code in a file named First.java only....no other name.....I will tell you why.....In java, there is a rule wherein you have to name the file with the same name as that of a top level public access class which it has or can be any one of the top level class if there are no top level public class. What i mean by top level class is this
class TopLevel { class InnerClass {} } This is just a dummy class and the class names are self explanatory of what i mean. So for the file containing this class, I should name the file as TopLevel.java only. Also note the case of the name. toplevel.java would compile but will never run...So take care....Never change the case of the file name. To make it more clear,
class First { class Second {} }
class Third { class Fourth {} }
for the above code, i can name the file as either First.java or Third.java.
class First { class Second {} }
class Third { public class Fourth {} }
public class Fifth {}
for this, the only file name can be Fifth.java
public class First{}
public class Second{} will generate an error as there cannot be 2 top level public classes in the same file.
Now that the naming problem is solved, it is upto the compilation process. Here, you will need to focus a bit. If you want the easy method, just skip this bullet and skip to bullet 2.
1) This is the tough way which will(and definitely) make your life henceforth.
I assume that you know how to use command prompt of Windows. If not type cmd in Run of your start menu. If you want to open an executable file in a folder, say you want to open speed.exe which is the application starter of NFS MostWanted game...(My favourite racing game)..you would have to go to the specific folder where you have installed NFS and then run the appplication using the command speed.exe right???? In another scenario, you can start notepad from any random folder just by typing notepad.exe in command prompt right?????How is it???? Well there is something called as environment variables which actually control this behavior. There is a variable called "path" which you will have to set as you have chosen the tougher path i.e bullet 1 is the tough path.....lol.....OK....Now right click on your My Computer icon and select properties.
goto advanced -> Environment Variables
Now i will not bother you too much with the details....Just contact me if you want any further details regarding this..I will surely get back to you with satisfying answers.
Now just look into the user variables part which happens to be the first window block on the screen.. Browse through if you can find a "path" variable. If found, click edit.....else create a new path by selecting new tab. Type the variable's name as "path"
Now if you already have a path variable defined, " ; " is used as a delimiter...So goto the end of the variable's definition which happens to be the second text field and add a " ; " and then type your JDK's bin directory's entire path into the text field. If you are creating a new path variable, just type the bin directory's path.
Click ok and exit from the window...
Now if you type javac in any directory in command prompt, it must display a list of switches which you can use with javac. If it doesn't work contact me via email....ill help you out with that......
2) This is the less troubling method but you will have to keep doing this over and over whenever you want to compile a java code. Just goto the directory in which you have installed java's JDK. Generally it will be the \Program Files\Java\(JDK Version)\ Now goto the bin directory inside this folder. All this is to be done using cd of command prompt. A hectic way but less thoughtful.
Now that you have direct access to javac and java which are the java compilers and interpreters, the compilation is pretty simple.
to compile, javac First.java or javac
to run the code, java First or java
----------------------------------------------------------------------------------------------------------------
Now let me describe this process by using a GUI like Netbeans which generally comes in the package which i mentioned earlier. Just follow these steps which I will be explaining...
1) Start Netbeans ( I assume that you have downloaded the exact package which i said earlier as it contains Netbeans 6.9.1 integrated with JDK 6 Update 21....Moreover the general process of execution should not change....)
2) goto file -> New Project -> Select Java and chose Java Application -> Click Next -> Type a project name and set the location where to save the project -> Ensure Create Main Class checkbox is ticked -> Click finish
3) A Main.java is preloaded. Just type the following code in the public static void main(String args[]) { } block only.....
System.out.println("Hello");
Thatz it....you are done with the coding....just ensure that the statement is inside the main() block itself...If you cannot do this, please quit java......
To run this, goto Run -> Run or just press F6
This does it....My job of introducing you to Java is done fairly...Happy coding...Please leave you feedback via comments or please contact me via my mail nagaraja.r@live.in for any queries regardin Java for an authorised answer.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.