Friday 26 February 2016

HELLO JAVA(YOUR FIRST JAVA PROGRAM)

We’ll end up taking four passes at this example before we’re done (HelloJava, Hello Java2, etc.), adding features and introducing new concepts along the way. But let’s start with the minimalist version:

 public class HelloJava {  
 public static void main( String[] args ) {  
 System.out.println("Hello, Java!");  
 }  
 }  
This five-line program declares a class called HelloJava and a method called main() . It uses a predefined method called println() to write some text as output. This is acommand-line program, which means that it runs in a shell or DOS window and prints its output there. That’s a bit old-school for our taste, so before we go any further, we’re going to give HelloJava a graphical user interface (GUI). Don’t worry about the code yet; just follow along with the progression here, and we’ll come back for explanations in a moment. In place of the line containing the println() method, we’re going to use a JFrame object to put a window on the screen. We can start by replacing the println line with the following three lines:
 JFrame frame = new JFrame( "Hello, Java!" );  
 frame.setSize( 300, 300 );  
 frame.setVisible( true );  
This snippet creates a JFrame object with the title “Hello, Java!” The JFrame is a graphical window. To display it, we simply configure its size on the screen using the setSize() method and make it visible by calling the setVisible() method. If we stopped here, we would see an empty window on the screen with our “Hello, Java!” banner as its title. We’d like our message inside the window, not just scrawled at the top of it. To put something in the window, we need a couple more lines. The following complete example adds a JLabel object to display the text centered in our window. The additional import line at the top is necessary to tell Java where to find the JFrame and JLabel classes (the definitions of the JFrame and JLabel objects that we’re using).
 import javax.swing.*;  
 public class HelloJava {  
 public static void main( String[] args ) {  
 JFrame frame = new JFrame( "Hello, Java!" );  
 JLabel label = new JLabel("Hello, Java!", JLabel.CENTER );  
 frame.add(label);  
 frame.setSize( 300, 300 );  
 frame.setVisible( true );  
 }  
 }  
After compilation and running. You should get something similar to the following;
Be aware that when you click on the window’s close box, the window goes away, but your program is still running. (We’ll fix this shutdown behavior in a later version of the example.) To stop the Java application in Eclipse, click the big red button in the console window(We'll be learning how to Create, Compile, Run Projects and set up NetBeans and Eclipse IDEs Shortly). If you are running the example on the command line, type Ctrl-C. Note that nothing stops you from running more than one instance (copy) of the application at a time. HelloJava may be a small program, but there is quite a bit going on behind the scenes. Those few lines represent the tip of an iceberg. What lies under the surface are the layers of functionality provided by the Java language and its foundation class libraries. We’re going to cover a lot of ground quickly in an effort to show you the big picture. We’ll try to offer enough detail for a good understanding of what is happening in each example, but will defer detailed explanations until the appropriate moments. This holds for both elements of the Java language and the object oriented concepts that apply to them. Next, let’s take a look now at what’s going on in our first example.

0 comments:

Post a Comment