Tag: Java

This article is a continuation of articles in the series of Java 101. You will need to read lesson 1 and lesson 2 to completely understand this article (or blog post, or whatever you want to call it). In lesson 2, we executed a piece of code, which printed “Hello World” on the command prompt (also called the console). The code looked something like this :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<span style="color: #808080;">/**

* This is a package. You need to import this to print anything to the command prompt,

* get information from the user on the command prompt, to read/write to a file, etc.

* For now, just keep in mind that you need to have this line in almost all java applications

* you write

*/</span>

import java.io.*;

<span style="color: #808080;">

/**

* This is a class which will contain the method you will run, to run your program.

* As we move along in these tutorials, you will see that this is a common structure

* in all Java applications. Everything is placed in a class. I will explain this more

* when we learn about classes.

*

* Also this is a public class, which means it has to be put in a file whose name is

* the same as its name (which will be HelloWorld.java). If you name this class

* HelloJoe, then you will have to save this whole code in HelloJoe.java

*/</span>

publicclassHelloWorld{<span style="color: #808080;">

<span style="color: #808080;">/**

* Forgive the cliche but this is a method which you will use in all Java applications

* you will write. This method is run is the one that the Java Virtual Machine will

* look for, when it is asked to run your application. Think of this as a starting point.

*/</span></span>

publicstaticvoidmain(String[]args){

<span style="color: #808080;">/**

* This is the place where you write your code. Give Java instructions on what you

* want it to do, etc. In this case, we are just printing out a statement - "Hello World".

* Just for fun, try changing the text "Hello World" to "I have ice cream" and run the

* application. The statement "I have ice cream" will be printed on the command prompt

*/</span>

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

}

}

I added some extra text in there to make it easy to understand what each line is doing. Notice that the extra text I added is in between /* and */ . Any text placed in between /* and */ is called a comment. Comments are pieces of information that are ignored by the compiler when it compiles and runs your code. They are meant to explain what your code is supposed to do, which is very helpful when the code is 3000 lines. As we move through the lessons, I will place comments in my example code so you should have an idea of how they are generally used.

The comments in the above code should explain pretty much everything about the Hello World application (or program). What you can do is copy this same structure for any program you write. And, just to be clear this is the structure :

1

2

3

4

5

6

7

8

import java.io.*;

publicclassAnyClassName{

publicstaticvoidmain(String[]args){

<span style="color: #808080;">// This is where you will write your code. Everything else will remain the same

// You can change the class name though (which is AnyClassName here)</span>

}

}

Now that we know the basic structure of a Java program we will start learning some actual programming in the next lesson.

Since this is the first class, I guess I should talk about what Java is. Java is a language that can be used to write platform-independent desktop applications or web applications. The term “Platform independent” means that you can write the code for an application on Windows and run it in Linux or a Mac (as long as you have a JVM installed). In fact, you can run your program on any device – not just computers – if that device has a JVM available for it.

The story of how Java came into existence is very interesting. The first release of Java was in 1995. In the last 14 years, Java has come a long way from being something that just integrated into Netscape to being one of the most widely used languages in the computer industry. I don’t want to bore everyone with the details of the history of Java’s birth so those who are interested can go to : http://java.sun.com/features/1998/05/birthday.html

How does Java work, you ask? When you write code in Java, you need to compile it using the javac command. When you compile your code, it is converted to “byte-code”. This byte-code is later read by JVM when you run your application using the java command. So, it’s a 2 step process.

To be able to make program in Java, you need to install the JDK (Java Development Kit). JDK comes with quite a few things but the ones we are interested in are the compiler, and the JVM. You may also have heard the term JRE (if you’ve ever had to install the JRE plugin for Internet Explorer). JRE stands for Java Runtime Environment. You can run Java applications with it but you can’t compile them.

Double click the file you just downloaded and it will start the installation process. Follow the instructions that come up and after it’s done you should be ready to write code Java code – assuming nothing went wrong.

J2SE download

For Windows :

You have java installed, but now you need to add it to your path. Path is an environment variable in windows. All the folders in this variable are searched for, when you try to run a program without providing its location.

Click start then right click on “My Computer”. Select “Properties” from the menu that pops up. Go to the “Advanced” tab and press the “Environment variables” button in it.

Click to enlargeClick to enlarge

In the dialog that opens up, you will find a variable called path. Click on it and click on the edit button. If you have installed the JDK in the default path, then your JDK will be installed in C:\Program Files\Java. You need to get the location for the bin folder in your JDK. For, JDK6 Update12 it is : C:\Program Files\Java\jdk1.6.0_12\bin . Go to the end in the “Variable value” textbox and type ; then type in the location to the bin folder for your JDK. In the end you will have written something like this :