Java
Java is an object-oriented programming language developed by Sun Microsystems. It is very popular because of its cross-platform nature & ease of use. With java you can create programs that can run on desktop as well as web application. So if you’re new to java programming this article is helpful to you in understanding the concepts of it in short. In this article you’ll learn about how to set up the JDK environment & how to compile, execute java programs. This article assumes you’ve experience in programming language C++.
Why Java?
- Java is object oriented programming language; it is easy to learn & use
- Java programs can run various operating systems supported by Java Virtual machine.
- It supports multi-threading, socket communication, memory management (automatic garbage collection)
- Supports Web based application (Servlet, JSP & Applet), Distributed application (Sockets, RMI, EJB etc)
- Many Protocols supported includes HTTP, JRMP and more to list.
Difference between C++ and Java
Here are some of the differences in C++ & Java.
- Java does not support Pointers. Pointers are considered as tricky & troublesome to use.
- Java does not support multiple inheritances; instead it supports multiple interface inheritance.
- Java does not support Structures & Unions, as Collections API in java does that job
- Automatic garbage collection is used in java, while in C++ you have to use Memory management explicitly
- Code in java is encapsulated in Class and that’s why there are no Global variables & functions
- Java does not support operator overloading
- Java does not have destructors, but it adds use of final () function
- Java does not perform any automatic type conversion that results in loss of precision
Installation
Depending upon the available release, you can download the java development kit from Sun’s website at java.sun.com Download the binary or source for your operating system then execute the binary to allow the java platform development kit & runtime environment to install on your operating system.
Setting path
Classpath is important environment variable that needs to be set after the installation of java environment. Classpath tells the java compiler about where to find compiled bytecode files are – this includes the files of your own programs as well as the ones required by the system that came with SDK itself.
Follow the instructions to set the path:
In Windows XP/2000:
- From start menu >Select “Settings” > “Control Panel” > Click on “System Icon”.
- In the system properties dialog box, click the “Advanced” tab > “Environment Variables”.
- Click the PATH variable.
- Enter the path of JDK’s bin folder here for example for JDK 1.6 installed on your system: c:\jdk1.6\bin; (Don’t forget to enter semicolon at the end)
- If you’ve done this process successfully then close the dialog box.
Now if you’ve made correct setting then you can easily execute java commands on command prompt of windows. Try the following command on command prompt:
Javac
This command will invoke the java compiler. If you’re not seeing the message of the java compiler then you’ve made mistake in setting path of java bin directory in environment variables. Make sure that the path is correct, once that path is properly set then you can see the java commands running properly on command prompt.
Programming in Java
After successfully installing java platform on your operating system, you are all set to program in java. You can create java source file using these software’s:
Text-Editors:
- Notepad
- Notepad++
- Pspad
- Ultraedit
And many other text-editors that supports syntax highlighting.
Integrated development environments
- Eclipse
- Netbeans
- Visual-Age
- Jcreator Pro
- Jedit
- SkyIDE
If you don’t have any Advanced Notepad, IDE or Editor then you can download later. In mean time you can open notepad if you’re using windows operating system. Type the following code in notepad or editor.
public class myhello { public static void main (String[] args) { System.out.println(“Coffee or Tea? “); } }
Now save the file with the name of the class name which in this case is myhello.java, the file extension should end with .java& not *.text. Whenever you save your java program then save it with the name of classname or else “classnotfound” error will catch you.
Some things to remember while migrating from C++:
- Functions are called as methods in java
- Like #include, here in java import is used whenever you import classes from packages (which you can see later when you import methods from system, lang or io classes.)
Now open up the command prompt, and then locate your folder with java file. Then change the directory in your command prompt to that folder. Once you’re in the folder then execute the following commands:
Javac myhello.javaNow if the program you’ve written is correct and you’ve not made any typos or syntax mistake then compiler will not return any error flags, it will simply return blank with the prompt. If you’re seeing the classnotfound exception then it means either you had not written the class name same as that of file or you’ve problem with setting classpath. If you’re having no errors then execute the following steps.
java myhello
This will execute our program called myhello and then it will print “coffee or tea?” On command prompt, you’ve successfully executed your first program in java. Now you’ve whole new world of java to explore.
Resource
There are many resources related to java on the internet .Take a look at the list of compiled resource of java.
Java home page: http://java.sun.com (Look for the Tutorial Trail & Javadoc)
David Ecks Javanotes: http://math.hws.edu/javanotes/
You can check out other articles realted to java:
RMI (Remote Method Invocation)
Books:
Beginners Guide to Java 2:JDK5 edition by Peter Norton (Wrox Press)
There are many others to list here but again choice depends on the learning strategy of student.
Don’t forget to download the Javadoc & official java trail tutorial from the homepage of the java.
Newsgroups
comp.lang.java
comp.lang.java.help
comp.lang.java.programmer
comp.lang.java.databases
comp.lang.java.gui
There are many forums like daniweb, dreamincode; techiwarehouse and many others forums have separate section for java programmers, where many professionals solve the java related problems.
I hope the information above helped. I have tried to cover as much as beginner related information in this article. If you’ve any suggestion or feedback to this article then please don’t hesitate to comment it below.
Comments
Leave a Reply
