Welcome to the second part of
JAVA tutorial series. In this post you will be learning how to setup your own
JAVA environment and start with coding in JAVA.
Should I really install a software on to the system to learn coding in
JAVA ?
The answer to this question is
both YES as well as NO. Are you surprised about 'NO' ? Well yes, you can
actually start coding in JAVA. Thanks to the online IDEs (Integrated
Development Environments), where in which some websites help us to code,
compile and execute the JAVA code or any other programming languages code
online Example: http://ideone.com/. But,
installing the IDE offline i.e. installation of the IDE on our system will be a
great option. In order to work with JAVA, we need something called JAVA
environment installed in our system. You can get and install JAVA SE from the
following link: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
The JDK and JRE will be dependent upon the operating system, architecture and a
few other parameters. Download from the link and then install the file by
following the instructions shown during installation.
Sometimes the JAVA environment variable will be installed by the JAVA software itself, if not then please do check whether the JAVA environment variables are set right. There are some situations where you won't get JAVA working in your system. In order to check if JAVA is working in your system, open command prompt/terminal and type the command java -version. If everything is alright, the JAVA version installed will be displayed.
Note: If you get any error message or warnings, then please do
refer this link: https://docs.oracle.com/javase/tutorial/essential/environment/paths.html,
where you'll be shown how to set environment path and variables.
Everything is setup ! Now where to write the code ?
If you have reached this step,
congratulations for you ! Now you are ready to write you first code in JAVA. There
are many IDEs or editors for writing the JAVA code. There are many sophisticated
IDEs too, but as of now you can consider the following editors:
- Text Editor (Notepad): You can use any of the text editors to write the code.
- Eclipse: It is a JAVA IDE developed by the eclipse open-source community and it can be downloaded from http://www.eclipse.org/.
- NetBeans: Even it is a JAVA IDE which is open-source and free. It can be downloaded from http://www.netbeans.org/index.html.
Hurray ! Finally you are ready to
write the first JAVA code. Open Notepad or any text editor, write the following
code:
1 2 3 4 5 6 7 | class first { public static void main(String[] args) { System.out.println("Welcome to JAVA 2 Digest"); } } |
Now save the file as first.java (Note: Not first.txt, it is first.java). Open the command prompt and navigate to the directory
where you have saved your file (first.java). Notice that the file name and the
class name are same. It is a convention to follow, where we are notifying the
compiler that it is the class which is the entry point of the program. The
entry point here means the containment of main(String[] args) method. Assuming
that you have opened the command prompt and navigated to the folder where you
have saved the file. Run the command
> javac first.java
If everything is fine then the prompt will successfully go to the next line and the first.class file will be created in the same folder where first.java file is present. If you get any statement like 'javac' is not recognized as an internal or external command, operable program or batch file. It is the problem of not setting the environment variable or path.
This a common problem which
occurs if you have not installed the JSE/JDK or if you have not set the environment
variables. The solution for this is copy the path C:\Program
Files\Java\jdk1.7.0_71\bin (Assuming that JSE/JDK is installed and OS
installation directory is assumed to be C: ). After you have copied the path,
open Control panel and click advanced system settings.
Once you have opened the edit
window, click the variable value and click End key on your keyboard, it will
navigate towards end of the line. Don't make any changes for the existing path
variables. After the last semi-colon, paste the copied path (C:\Program
Files\Java\jdk1.7.0_71\bin).
Click ok->ok->ok. Now
assuming that you have followed the steps, close the command prompt which was
open. Open the command prompt again, navigate to the java file and type the
commands
> javac first.java
> java first
Congratulations you have executed
your first JAVA program. If you get any errors while executing, then please do
check the code for spelling mistakes and note that JAVA is case sensitive.
Explanation of the code:
1 2 3 4 5 6 7 | class first { public static void main(String[] args) { System.out.println("Welcome to JAVA 2 Digest"); } } |
In JAVA a class is the collection of data members and member functions/methods. In the given code, 'class' is the keyword and 'first' is the class name. The JAVA code is written in blocks, so each block of code is enclosed within { } - Flower brackets. In the class named first, we have a method called 'main'. The 'main' method is the entry point for the program. 'public' is the access modifier, 'static' means the method belongs to the class and to all objects formed but not accessible by the objects (Don't get confused :P for now consider static as a reserved word). 'void' is the return type of the method 'main', which means 'main' method won't return anything, so it is 'void'. 'String' is a class in JAVA and whatever which is written to the console and taken as input from the console is considered as of String type and it is array of String. 'System' is a class in the java.lang package, out is a static member of the System class, and is an instance of java.io.PrintStream. 'println' is a method of java.io.PrintStream. This method is overloaded to print message to output destination, which is typically a console or file.
That's it for now ! Hope this
article was informative and knowledgeable. Your comments, queries and
suggestions are most welcome. See you soon in the next tutorial :)
No comments:
Post a Comment