Friday, November 13, 2015

WRITING AND RUNNING YOUR FIRST JAVA PROGRAM

Usually, when learning a new programming language, we start out
with a Hello, World program that simply displays the message, “Hello, World!”
So, we will now write a simple Java program that displays a greeting at the NetBeans output console. Click on this Link to see how to write and run the Hello, World program using NetBeans. 

Programming in Java is divided into three parts:
  • Writing the Source code.
  • Compiling the source code.
  • Running the program
Usually, NetBeans takes care of the second part for you and it compiles as you type in the code and reports errors on-the-go. (Another advantage of IDEs over Regular Text editors)
Java is a highly structured and organized language. One of the rules that must be followed is that the name of a source code file must match the name of the public class defined in that file, and the file extension must be .java.
So we will go straight ahead and write a program called MyFirstJavaProgram.java. This means that our code will contain a public class (Will be discussed in details later) named MyFirstJavaProgram.java.

This program will display some text on the output console and will serve as a great starting point for you.
The source code is listed below.
package theartofcoding;
public class MyFirstJavaProgram {
   public static void main(String[] args) {
       System.out.println("Hello, World!");
       System.out.println("This is Chux Mykel From theartofcoding.tumblr.com");
       System.out.println("You are in for a thrill because Java is thrilling");
       System.out.println("This IDE makes life easy for me");
       System.out.println(It highlights my mistakes LIKE THIS);
       System.out.println("Notice how the previous Line has no Quotes around the text");
   }
}

The program contains a single class named MyFirstJavaProgram. Within MyFirstJavaProgram is one method: main(). The main() method is declared as public, static, and void, which are Java keywords that are discussed later in the book. The main() method is unique because it is the method invoked by the JVM when this program is executed.
The main() method must look like:
public static void main(String [] args). “A java program without a main method wont run“.
Within main() are some statements:
System.out.println(“Hello, World”);                                                                  This statement is what causes “Hello, World” to appear at the console—or in this case, the standard output. System.out represents the standard
output of the device where this program is running, and the println()
(short for print line) method displays the given string along with a line feed.
Notice the semicolon at the end of the System.out.println() statement. The
Java compiler ignores all whitespace and indenting, so it is necessary to use a
semicolon to denote the end of all statements in Java.
The other statements have similar functions. Notice how the 5th statement is underlined by the IDE. It contains an error. All strings in java must be enclosed in quotation marks. And the IDE will notify you if any error should be found.
Run the program as you ran the Hello, World! program and study the output. You can change the string contents and view the resulting output.

No doubt, this was one lousy post. Should you get stuck at any point, feel free to drop a comment.
Next post in a few days.
Don’t forget to follow @theartofcoding
Until next time, Be Well