Sie sind auf Seite 1von 15

For the absolute beginners

By B zlu R hm R k

Java 7 for novice

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

Java 7 for novice

Writing Your First Java Program


What is Java
We are going to learn java. Its a programming language like C, C++, C# etc. As you open this book, you are already convinced that you need to learn java, so Im 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.

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.

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.

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. Its 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.

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. Its 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 programmers 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. Its 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. Thats 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 its a tradition, now well write our first java program which will print hello, world!. Lets follow these following steps: Start your eclipse.

Java 7 for novice

Figure 1-2: Eclipse Splash screen Youll start up with a splash screen. Wait a bit; youll 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.

Java 7 for novice

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

Now lets 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 lets create a project. o From the File menu, select New, and then select Project. The New Project window appears, as shown below:

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:

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, youll find your project. Expand it, youll 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:

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 } }

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, dont forget to share and youre welcome at my Facebook page to get instant update. Facebook: facebook.com/codexplo Twitter: @bazlur_rahman

14

Das könnte Ihnen auch gefallen