Friday, March 11, 2016

Intro to Java: Section 1.3 - Hello World (part two)

Printing To The Console

Now it's finally time to write your first line of code. This is the line we will use to print "hello, world" to the screen.
System.out.println("hello, world");
This line of code should be placed within the main method. We will be using the built in Eclipse console to view all of our output rather than the command windows provided by the operating system such as the Command Prompt in Microsoft Windows or the Terminal window in Mac or Linux.
The Java API includes all the objects you can use by default with Java. API stands for Application Programming Interface. You may view the full documentation on the Java API here.
System is an example of one such object provided by the Java API. This object contains the field out which is used to access the standard output PrintStream. PrintStreams contain the method println which enables us to print a line of text to a stream, so System.out.println("hello, world") can be interpreted as meaning: print the message "hello, world" to the standard output stream.
The information you provide to the method within the parenthesis is known as an argument. The text "hello, world" is wrapped in quotation marks, indicating that this is a string of text. This means that the argument being passed to the println method of the System.out object is a string containing the words "hello, world."
In this case, the line of code is known as a statement. Statements specify the actions Java will perform. Simple statements may only require one line of code while more complex statements may require multiple lines. Most statements in Java will end with a semicolon. If you are missing a semicolon where one is required, your program will not compile.
our HelloWorld.java file should now look as follows:
package helloworld;

public class HelloWorld {

 public static void main(String[] args) {
  System.out.println("hello, world");

 }

}

Running The Program

It's now time to run the program. Save the program (Ctrl+S) or save button. Then click the play button on the toolbar to run the program. You should see the words "hello, world" displayed in the console. If you see this, then your program has executed successfully and you have completed this exercise.

Making Modifications

You may modify the program to output whatever text you would like simply by changing the string in line 6. You can also print out multiple lines of text by adding another System.out.println("") statement to the code.
package helloworld;

public class HelloWorld {

 public static void main(String[] args) {
  System.out.println("Hello people.");
  System.out.println("My name is Bob.");

 }

}
The above line of code would produce the following output:
Hello people.
My name is Bob.
In addition to the println method, System.out also contains other methods you may use to output strings of text to the console. The print method is an example of this. It works in the same manner as println except a new line will not be automatically created.
System.out.print("Hello people.");
System.out.print("My name is Bob.");
If you were to change the method println to print in the above example, it would produce the following output instead. Note that a space will not automatically be added.
Hello people.My name is Bob.

0 comments:

Post a Comment