Beruflich Dokumente
Kultur Dokumente
By B zlu R hm R k
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
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.
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.
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
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.
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:
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:
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:
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
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
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
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:
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