For the absolute beginners

By B zlu R hm R k

Java 7 for novice

1

Java 7 for novice

Contents
WRITING YOUR FIRST JAVA PROGRAM ......................................................................................... 3
What is Java ........................................................................................................................................................... 3 Byte Code .............................................................................................................................................................. 3 Java Virtual Machine ............................................................................................................................................. 3 Java Runtime Environment .................................................................................................................................... 4 Java Developer Kit ................................................................................................................................................. 4 Installing Java Development Kit ............................................................................................................................. 5 Installing IDE .......................................................................................................................................................... 5 Writing First Program ............................................................................................................................................ 5 Summary ............................................................................................................................................................. 13

2

Java 7 for novice

Writing Your First Java Program
What is Java
We are going to learn java. It’s a programming language like C, C++, C# etc. As you open this book, you are already convinced that you need to learn java, so I’m not going too far about why you need to learn java. To put it simple, java is unlike any other languages but it has some major advantages. Very first one is, it is platform independent language. Here, platform means Operating System. There are different types of Operating System like Windows, Linux, and Mac OS etc. out there. Java is not dependent on Operating System which means you can write software for any of these operating systems in Java without knowing much about a specific operating system. This one is the main advantage of java but there are many. Java is simple, secure, portable, object oriented, robust, multithreaded, interpreted, distributed, and dynamic and so on. It was invented by Sun Microsystems1, by James Gosling2. Today Oracle3 owns Sun and therefore Java too. Java is currently in version 7 (Java 7), but Java 8 is expected to be released soon. In this book I will use Java 7.

Byte Code
A computer program is nothing but a set of instruction which we call Source Code. Byte code is another kind of special code which is compiled from java Source Code. And this byte code is executed by java virtual machine.

Java Virtual Machine
Java virtual machine is like a computer. We know about computers and computers need operating systems. Virtual machine is a kind of software which emulates a physical computing environment. So java virtual machine is a kind of software components which emulate physical computer. Typically when we write instruction (for example in C) it is compiled into an executable and our physical computer runs it. Here when we write java source code, it is compiled into java bytecode and then it is run by java virtual machine. So java virtual machine basically executes java bytecode.

1

Sun Microsystems, Inc. was a company that sold computers, computer components, computer software, and information technology services. Sun was founded on February 24, 1982. [Wiki] Dr. James A. Gosling, OC (born May 19, 1955 near Calgary, Alberta, Canada) is a computer scientist, best known as the father of the Java programming language.

2

3 Oracle Corporation is an American multinational computer technology corporation that specializes in developing and marketing computer hardware systems and enterprise software products – particularly database management systems.

3

Java 7 for novice
This java virtual machine is implemented in several different operating systems like Windows, Mac OS, Linux, IMB Mainframes, and Solaris etc. So if your application written in java and runs on windows, surely it will run on any of other operating systems.

Java Runtime Environment
Java Runtime Environment (JRE) also known as Java Runtime is a set of tools including java virtual machine with minimum requirements for executing a Java application. So if you install java runtime in your machine, which means you install Java Virtual Machine also.

Java Developer Kit
Java Developer Kit (JDK) is also a set of programming tools that enables you to write programs in Java. It’s a tool box. It consists of Java Runtime, Java Compiler and some other components which are required to develop an application.

Figure 1-1: Java Development Kit 7

So if you install JDK, you will get all.

4

Java 7 for novice

Installing Java Development Kit
Installing Java Development kit is fairly easy. Well, before you can install it, you have to download it, of course. To get the latest version of java development kit, follow these following steps: • • • • Go to your web browser and open http://www.oracle.com/technetwork/java/javase/downloads/index.html Click the Download button which is mentioned Java Platform (JDK) 7u5 Follow the instructions provided by the web site. Run the installer and accept any defaults.

Installing IDE
IDE stands of Integrated Development Environments. It’s another kind of software where we write our programs. Writing a program in a notepad is a bit uncomfortable. IDE provides a convenient tool for writing and testing our program easily. They are designed to maximize programmer’s productivity by suggesting code segments, finding and correcting errors as we make them, structuring source code and so on. There are lots of IDEs out there, but in this book, we are going to use Eclipse as our IDE. It’s totally free. Again, before you can install Eclipse, you have to download it. To do so, follow these steps: • • • • Open http://www.eclipse.org/downloads/ in a web browser. Find the Eclipse IDE for Java Developers choice and select the 32-bit version. Follow the instructions provided by the web site. Download it and extract it if is zipped form. That’s it.

Note: if you have 64 bit operating system, choice the 32 version of eclipse anyway. 64 bit version of eclipse has some issues. So 32 version eclipse is safer and at this time we are going to avoid any type of issues

Writing First Program
We suppose to write our first java program. As we always start with hello world program, in fact as it’s a tradition, now we’ll write our first java program which will print “hello, world!”. Let’s follow these following steps: Start your eclipse.

5

Java 7 for novice

Figure 1-2: Eclipse Splash screen You’ll start up with a splash screen. Wait a bit; you’ll get a dialog to choose your workspace.

Set your workspace. Workspace is directory where you want to keep your source code.

Figure 1-3: Workspace Launcher If you want to keep default, just go ahead, click Ok. Now you have your workspace.

6

Java 7 for novice

Figure 1-4: Eclipse workspace This is Eclipse Juno, Java IDE, our workspace where we’ll write our code.

Now let’s start a new project. When you use Eclipse, you have to create a separate project for each program. In this way, Eclipse can keep the details of one program away from the other. Each project consists of files of source code you write for your program and potentially a number of other resources that can be attached to the program. So let’s create a project. o From the File menu, select New, and then select Project. The New Project window appears, as shown below:

7

Java 7 for novice

Figure 1-5: Project Window o In the New Project window, select Java Project and click Next button. The New Java Project window appears, as shown below:

8

Java 7 for novice

Figure 1-6: New Java Project Window o o Pick a name and write on the textbox and click Finish button. I named it “Hello”. Now we are about create to a class. What is class, I will explain you later, for now, just follow these instructions. See the left pane of your eclipse, you’ll find your project. Expand it, you’ll find a folder named ‘src’. Keep your mouse point there, and right click. Keep your mouse over New and then click on Class. See the figure below:

9

Java 7 for novice

Figure 1-7: New Class Creation o After clicking on class, New Java Class window appears. In the Package field, type whatever you like for the package. I will explain what is package and why it is needed, we'll cover in the next chapter. In the Name field, type Helloworld. This is the name of your class. Check the checkbox that gives you a main method (public static void main (String args [])). See the figure below:

10

Java 7 for novice

Figure 1-8: New Java Class window I put the package name ‘com.example.hello’. Now click on Finish button and you will get code like Listing 1-1.

11

Java 7 for novice

Listing 1-1: Preliminary Hello class

package com.example.hello; public class Helloworld { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }

o

In the code, after the ‘//TODO Auto-generated method stub’ text, insert the line given below:
System.out.println("Hello, world!");

o Now your class should now look similar to Listing 1-2 Listing 1-2 : Basic Hello program
package com.example.hello; public class Helloworld { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello, world!"); } }

o Now you have a complete Java program. You can now run your program by clicking the Run button in the toolbar or by choosing Run from the Run menu. Figure given bellow shows where to find the Run button.

12

Java 7 for novice

Figure 1-9: Run Button

o Click on the run button. Eclipse then displays a console panel under the code area that shows the output of your program. In this case, it says, “Hello, World!” as below:

Figure 1-10: Eclipse Console

This is all about our first java program.

Summary
In this chapter, we learned: What is java What is Byte Code What is Java Virtual Machine, Java Runtime and Java Development Kit Installing Java Development Kit Installing Eclipse IDE Writing first java program 13

Java 7 for novice If you like my tutorial, don’t forget to share and you’re welcome at my Facebook page to get instant update. Facebook: facebook.com/codexplo Twitter: @bazlur_rahman

14

Sign up to vote on this title
UsefulNot useful