Title: Basics Hello World Comments
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Basics Hello World Comments

One of the tough things about getting started in Java is that even my simple Hello program has lots of things in it that will take some time to fully comprehend. I'm going to give a quick description of what is in it, but you may need to give it about 8 weeks before all of the pieces make perfect sense. In fact I'm going to recommend that you listen to the following statements and not worry if they don't make sense now. In 8 weeks come back and go over these comments and let me know if it doesn't then make perfect sense. After this we will start with the "nuts and bolts" of Java and build everything from the ground up.


import java.util.Scanner;

class Hello
{
   public static void main(String[] args)
   {
       System.out.println("hello world ... Enter your name");

       Scanner keyboard = new Scanner(System.in);
       String input = keyboard.nextLine();
       System.out.println("Hello: " + input);

   }
}


Points on the above program (we will try to show variations and mistakes on the following points to clarify):

- All Java code will live in a class that has a name (in this case Hello). I would recommend that for the time being, you always put a class Hello into a file named Hello.java(pay attention to case ... Java is case sensitive). This will avoid some problems for the time being.
- If you want to execute your program the way we executed the Hello program, you will need a method that has the format:
public static void main (String[] args)
For a few chapters we will put all of our code into this method until we have had a chance to talk more fully about methods.
- To send output to the screen, there is a Java object called System.out which is associated with your output monitor device. This object has 2 methods:
println - for output with a new line following it
print - for output with no new line following it
- The methods print and println (we will discuss methods in great detail later), are very good at converting anything to an output string.
- A constant String can be created just by enclosing some text with quotes:
"This is a string"
- We will find a need to create objects to fully utilize Java. The creation of an object can always be identified with the keyword: new . So the following statement creates an object that is associated with the Keyboard:
Scanner keyboard = new Scanner(System.in);
- It turns out that our new keyboard object has a method called nextLine() which allows you to read the next line from the keyboard. This method returns an object of type String. We have created a variable named input to receive this keyboard input:
String input = keyboard.nextLine();
- The method println will see 2 strings separated with a "+" operator. The println will concatenate these 2 strings when forming the output to the screen.
- Java makes uses of code libraries sometimes called packages. Note the statement:
import java.util.Scanner
Without this statement, the Java compiler doesn't know where Scanner is defined. By adding the import statement you are telling the compiler that the Scanner class can be found in the java.util package. Note that the compiler knows about somethings without any imports (i.e. System, String). That's because there is an automatic assumption that you need the package java.language and it's implied to be present even if not explicitly stated. In otherwords, its as if you always had a statement like:
import java.language.*

Last Modified: July 17, 2013 2:15 AM