quick learn java

Hour I. Hello Program, Orientation, and Input
Example I.
class Hello {
  public static void main (String[] args) {
    System.out.println("Welcome to the world of Java Programming.");
  } // method main
} // class Hello
To compile and run this program, you need to have installed JDK and added a line to your path statement referring to the directory of where it was install + \bin. (e.g. path %path%;c:\jdk\bin;)
•    type this file into notepad or something
•    save it as Hello.java(class name + .java)
•    drop to a command prompt
•    type javac Hello.java (e.g. "javac C:\work\Hello.java")
•    type java Hello (e.g. "java C:\work\Hello")
•    then watch the magic
You have now written and completed your first Java program. Looking back to the above program, you should notice the following. First, Java is case-sensitive. The commands have to be written like they are above. Java also denotes the end of statement with a semi-colon like C & Pascal. Brackets signify either to "{" begin a group of statements, or "}" end a group of statements. The // designates a comment. Anything after two slashes the compiler ignores. Now we're ready to read input from the keyboard. Pay attention to the comments, they help explain the programs.
Example II.
import java.io.*; //include Java's standard Input and Output routines

class Echo {
  public static void main (String[] args) throws IOException {

    // Defines the standard input stream
    BufferedReader stdin = new BufferedReader
      (new InputStreamReader(System.in));
    String message; // Creates a varible called message for input

    System.out.print ("Enter the message : ");
    System.out.flush(); // empties buffer, before you input text
    message = stdin.readLine();

    System.out.print("You ");
    System.out.println("entered : " + message);

  } // method main
} // end of class Echo
You have just learned the standard way of getting text. First you create a reader, and then you input the text with readLine() , and finally you use the print command to output it. Notice the difference in the print commands : println prints, then goes to the next line, while print does not. The throws IOException in main lets Java know what to do with errors, when it encounters them.

No comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More