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.
- Writing the Source code.
- Compiling the source code.
- Running the program
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.
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 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.
Next post in a few days.
Don’t forget to follow @theartofcoding
Until next time, Be Well