Java Programming

Only available on StudyMode
  • Download(s) : 101
  • Published : August 8, 2012
Open Document
Text Preview
Java was developed by a team led by James Gosling at Sun Microsystems. Originally called Oak, it was designed in 1991 for use in embedded consumer electronic appliances. In 1995, renamed Java, it was redesigned for developing Internet applications. Java is a full-featured, general-purpose programming language that is capable of developing robust mission-critical applications. Today, it is used not only for Web programming, but also for developing standalone applications across platforms on servers, desktops, and mobile devices. It was used to develop the code to communicate with and control the robotic rover that rolled on Mars. Many companies that once considered Java to be more hype than substance are now using it to create distributed applications accessed by customers and partners across the Internet. For every new project being developed today, companies are asking how they can use Java to make their work easier. The Java programming language is a high-level language that can be characterized by all the following buzzwords: •Simple

Object oriented
Architecture neutral
High performance
A Simple Java Program
A Java program can be written in many ways.
Let us begin with a simple Java program that displays the message "Welcome to Java!" on the console. The program is shown in Listing 1.1.

Every Java program must have at least one class. A class is a construct that defines data and methods. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome. In order to run a class, the class must contain a method named main. The JVM executes the program by invoking the main method. A method is a construct that contains statements. The main method in this program contains the System.out.println statement. This statement prints a message "Welcome to Java!" to the console. Creating, Compiling, and Executing a Java Program

You have to create your program and compile it before it can be executed. This process is repetitive, as shown in Figure 1.11. If your program has compilation errors, you have to fix them by modifying the program, then recompile it. If your program has runtime errors or does not produce the correct result, you have to modify the program, recompile it, and execute it again.

You can use any text editor to create and edit a Java source code file, or you can use an IDE like Eclipse, JBuilder or NetBeans. Figure 1.12 shows how to use NotePad to create and edit the source code file

This file must end with the extension .java and must have the exact same name as the public class name. For example, the file for the source code in Listing 1.1 should be named, since the public class name is Welcome. A Java compiler translates a Java source file into a Java bytecode file. The following command compiles javac

Anatomy of a Java Program
The application program in Listing 1.1 has the following components: •Comments
Reserved words
The main method
To build a program, you need to understand these basic elements. They are explained in the sections that follow. 1.10.1. Comments
The first line in in Listing 1.1 is a comment that documents what the program is and how the program is constructed. Comments help programmers to communicate and understand the program. Comments are not programming statements and thus are ignored by the compiler. In Java, comments are preceded by two slashes (//) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a paragraph comment. When the compiler sees //, it ignores all text after // on the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */. Here are examples of the...
tracking img