Pages

A Simple Java Program

A Java program is executed from the main method in the class. 

Let’s begin with a simple Java program that displays the message Welcome to Java! on the console. (The word console is an old computer term that refers to the text entry and display device of a computer. Console input means to receive input from the keyboard, and console output means to display output on the monitor.)  

Example- 1 //Welcome.java 
 public class Welcome 
{
   public static void main(String[] args) 
  { 
     // Display message Welcome to Java! on the console  
      System.out.println("Welcome to Java!");
   }
}

Output: Welcome to Java!

Note: That the line numbers are for reference purposes only; they are not part of the program. So, don’t type line numbers in your program. 

Line 1 defines a class. Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome. 

Line 2 defines the main method. The program is executed from the main method. A class may contain several methods. The main method is the entry point where the program begins execution. A method is a construct that contains statements. The main method in this program contains the System.out.println statement. 
This statement displays the string Welcome to Java! on the console (line 4). 
String is a programming term meaning a sequence of characters. A string must be enclosed in double quotation marks. Every statement in Java ends with a semicolon (;), known as the statement terminator. Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program. 
For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in this program are public, static, and void. 

Line 3 is a comment that documents what the program is and how it is constructed. Comments help programmers to communicate and understand the program. They 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 block comment or 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 comments:
 

// This application program displays Welcome to Java! 
/* This application program displays Welcome to Java! */
 /* This application program displays Welcome to Java! */

A pair of curly braces in a program forms a block that groups the program’s components. In Java, each block begins with an opening brace ({) and ends with a closing brace (}). Every class has a class block that groups the data and methods of the class. Similarly, every method has a method block that groups the statements in the method. Blocks can be nested, meaning that one block can be placed within another, as shown in the following code.
public class Welcome 
{
   public static void main(String[] args) 
  { 
     // Display message Welcome to Java! on the console  
      System.out.println("Welcome to Java!");
   }End of the method
}End of the Class

Example- 2
The program in Example1 displays one message. Once you understand the program, it is easy to extend it to display more messages. For example, you can rewrite the program to display three messages, as shown in Example2.  

 //WelcomeWithThreeMessages.java 
 public class WelcomeWithThreeMessages 
{
 public static void main(String[] args) 
 {
    System.out.println("Programming is fun!");
    System.out.println("Fundamentals First");
    System.out.println("Problem Driven"); 
  }
}

Output:
Programming is fun! 
Fundamentals First 
Problem Driven
 

Example- 3 
Gives an example of evaluating
10.5 + 2 * 3 45 - 3.5 


//ComputeExpression.java  
public class ComputeExpression 
{
   public static void main(String[] args) 
  { 
    System.out.println((10.5 + 2 * 3) / (45 -3.5)); 
  }
}

Output: 0.39759036144578314
 

The multiplication operator in Java is *. As you can see, it is a straightforward process to translate an arithmetic expression to a Java expression. We will discuss Java expressions further in Chapter.


Example- 4
//ComputeExpression.java 
public class Test 

 public static void main(String[] args) 
 {     
   System.out.println(3.5 * 4 / 2 - 2.5);  
  } 
}
Output: 4.5

How to compile Java program?
First approach: 
>> Open the Notepad
>> Write the Java Code
  >> Save the Sample.java
  >> Compile the Program
  >> Go to Command line
>> Successfully run this process

Second Approach:
>> Open the NetBeans  
>> Click the File --> Open New Project --> Java Application -- Sample

>> Java application --> Click the Next button 


  >> Project Name: Sample --> Click the Finish button



>> Output : Hello Java
>> This Approach is Successfully Running.. 

Creating, Compiling, and Executing a Java Program:
             You save a Java program in a .java file and compile it into a .class file. The .class file is executed by the Java Virtual Machine. 


                                        
The Java program-development process consists of repeatedly creating/modifying source code, compiling, and executing programs. 

public class Welcome 
{
   public static void main(String[] args) 
  { 
     // Display message Welcome to Java! on the console  
      System.out.println("Welcome to Java!");
   }
}

The Java language is a high-level language, but Java bytecode is a low-level language. The bytecode is similar to machine instructions but is architecture neutral and can run on any platform that has a Java Virtual Machine (JVM), as shown in Figure 1.8b. Rather than a physical machine, the virtual machine is a program that interprets Java bytecode. This is one of Java’s primary advantages: Java bytecode can run on a variety of hardware platforms and operating systems. Java source code is compiled into Java bytecode and Java bytecode is interpreted by the JVM. Your Java code may use the code in the Java library. The JVM executes your code along with the code in the library. 

To execute a Java program is to run the program’s bytecode. You can execute the bytecode on any platform with a JVM, which is an interpreter. It translates the individual instructions in the bytecode into the target machine language code one at a time rather than the whole program as a single unit. Each step is executed immediately after it is translated.  


(a) Java source code is translated into bytecode. (b) Java bytecode can be executed on any computer with a Java Virtual Machine.


JDK :
   Java development toolkit is called JDK.
       
    JDK consists of a set of separate programs for developing and     testing java program, each of which is invoked from a commend line.
   Java development tools ( Net Beans, Eclipse, Text Pad, Jbuilder ).                            JDK = JRE + Development Tools






No comments:

Post a Comment