A First Simple Program



 Writing and Executing a Java Program

  In high-level programming languages such as C and C++, you write a program in a human-readable format, and a program called compiler, translates it to a binary format called executable code that the computer can understand and execute. The executable code depends upon the computer machine that you use to execute your program; it is machine dependent. In Java, this process from writing to executing a program is very similar but with one important difference that allows you to write Java programs that are machine independent. To understand that difference, pay attention to the term Java virtual machine (JVM).

 Writing a Java Program:

  A Java program that you write contains a set of instructions in text format written according to the rules of the Java programming language. These instructions are called source code. The file that contains these instructions is called the source file and has the file extension .java. You can use a text editor such as Notepad to create and edit Java source files.
  As an exercise, open a new text document (for example, using Notepad on your Windows machine), type the code presented in bellow example into the document, and save the document as Example.java.



 /*
This is a simple Java program.
 Call this file "Example.java".

 */
 class Example
{
 // Your program begins with a call to main().

 public static void main(String args[])
{
  System.out.println("This is a simple Java program.");
 }
}


The Output is: This is a simple Java program.

Entering the Program :

  For most computer languages, the name of the file that holds the source code to a program is not important. However, this is not the case with Java. The first thing that you must learn about Java is that the name you give to a source file is very important. For this example, the name of the source file should be Example.java. Let’s see why.


  In Java, a source file is officially called a compilation unit. It is a text file that contains one or more class definitions. The Java compiler requires that a source file use the .java file name extension.
  As you can see by looking at the program, the name of the class defined by the program is also Example. This is not a coincidence. In Java, all code must reside inside a class. By convention, the name of that class should match the name of the file that holds the program. You should also make sure that the capitalization of the filename matches the class name.
The reason for this is that Java is case-sensitive. At this point, the convention that file names correspond to class names may seem arbitrary. However, this convention makes it easier to maintain and organize your programs.


 Compiling the Program:

  As you know by now, computers cannot understand the source code that you write. Before a CPU of a computer can execute the instructions written by you, the instructions need to be translated into a machine language—that is, into a binary format. In most of the programming languages, such as C and C++, a compiler compiles the source code into the machine language, and this compiled code is called executable code. However, in Java, the compiler compiles the code into bytecode, which is not executable code. Bytecode is somewhere in the middle of source code and executable code. For example, to create the bytecode files from the source file Example.java, you run the Java compiler by issuing the following command:


C:\:javac Example.java

  The javac compiler creates a file called Example.class that contains the bytecode version of the program. As discussed earlier, the Java bytecode is the intermediate representation of your program that contains instructions the Java Virtual Machine will execute. Thus, the output of javac is not code that can be directly executed.

Caution
  The command to compile a program in the source code file is javac, and you must include the file Example.java when specifying the file name.The bytecode in the class files is interpreted by the JVM when you execute (or run) the program.


  To actually run the program, you must use the Java application launcher, called java. To do so, pass the class name Example as a command-line argument, as shown here:

  When you issue the java command, the JVM reads your bytecode file and translates the instructions to the executable format that your computer can understand. The executable form of a program is specific to a particular machine. This is why you need a specific JVM for a specific kind of platform such as Windows, Solaris, or Linux. you need to write the program only once, but you can execute it on a number of different machines with different operating systems by using different JVMs.

    C:\:java Example

  When the program is run, the following output is displayed:

    This is a simple Java program.

  When Java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension. This is why it is a good idea to give your Java source files the same name as the class they contain—the name of the source file will match the name of the .class file. When you execute java as just shown, you are actually specifying the name of the class that you want to execute. It will automatically search for a file by that name that has the .class extension. If it finds the file, it will execute the code contained in the specified class.


 A Closer Look at the First Sample Program:

  Although Example.java is quite short, it includes several key features that are common to all Java programs. Let’s closely examine each part of the program.
The program begins with the following lines:

  /*
  This is a simple Java program.
  Call this file "Example.java".
  */

  This is a comment. Like most other programming languages, Java lets you enter a remark into a program’s source file. The contents of a comment are ignored by the compiler. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code. In this case, the comment describes the program and reminds you that the source file should be called Example.java. Of course, in real applications, comments generally explain how some part of the program works or what a specific feature does.
  Java supports three styles of comments. The one shown at the top of the program is called a multiline comment. This type of comment must begin with /* and end with */. Anything between these two comment symbols is ignored by the compiler. As the name suggests, a multiline comment may be several lines long.
  The next line of code in the program is shown here:

 class Example
{

  This line uses the keyword class to declare that a new class is being defined. Example is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace ({) and the closing curly brace (}). For the moment, don’t worry too much about the details of a class except to note that in Java, all program activity occurs within one. This is one reason why all Java programs are (at least a little bit) object-oriented.

  The next line in the program is the single-line comment, shown here:

  // Your program begins with a call to main().

  This is the second type of comment supported by Java. A single-line comment begins with a // and ends at the end of the line. As a general rule, programmers use multiline comments for longer remarks and single-line comments for brief, line-by-line descriptions. The third type of comment, a documentation comment, will be discussed in the “Comments” section later in this chapter.

  The next line of code is shown here:

public static void main(String args[])
{

  This line begins the main( ) method. As the comment preceding it suggests, this is the line at which the program will begin executing. All Java applications begin execution by calling main( ). The full meaning of each part of this line cannot be given now, since it involves a detailed understanding of Java’s approach to encapsulation. However, since most of the examples in the first part of this book will use this line of code, let’s take a brief look at each
part now.

  The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. (The opposite of public is private, which prevents a member from being used by code defined outside of its class.) In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java Virtual Machine before any objects are made. The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values. If all this seems a bit confusing, don’t worry. All of these concepts will be discussed in detail in subsequent tutorials.


  System.out.println("This is a simple Java program.");

  System: It is the name of standard class that contains objects that encapsulates the standard I/O devices of your system.
  It is contained in the package java.lang. Since java.lag package is imported in every java program by default,therefore java.lang package is the only package in Java API which does not require an import declaration.

  out:The object out represents output stream(i.e Command window)and is the static data member of the class system.
  So note here System.out (System -Class & out- static object i.e why its simply refered by classname and we need not to create any object).

  println:The println() is method of out object that takes the text string as an argument and displays it to the standard output i.e on monitor screen.

Note:
  System -Class
  out -static Object
  println() -method




0 comments:

Post a Comment

Powered by Blogger.

Stats