What email address or phone number would you like to use to sign in to Docs.com?
If you already have an account that you use with Office or other Microsoft services, enter it here.
Or sign in with:
Signing in allows you to download and like content, and it provides the authors analytical data about your interactions with their content.
Embed code for: Lesson 01 - Screen Output
Select a size
Lesson 1…..Console Output - Hello World
The Console Helper Class
To make our first program we are going to use a class that is not from the standard Java API. It’s a helper class that makes it easier to learn Java. It’s called the Console class, and it represents a screen that allows you to put your output and get your input easily. [If you’ve ever used a command-line in Windows or Linux, you’ve used a console]
First Program Overview
The import, class declaration and main method form the structure of your program. All of the programming is done within this structure.
The Console Class Explained.
That API for the Console class can be found on the website. It’s sole function is to create a Console and allow you to get input from it and put output to it. Pretty handy, huh?
You’ve seen how to create a new instance of the Console class.
Console yournamehere = new Console();
When you run the program you’ll see a new window open up.
It’s beautiful, but blank. This lesson will teach 3 ways of putting stuff onto the screen.
The println() Method: When you use this with your Console class,
The print() Method: Does the same as the println method, but doesn’t insert an enter (carriage return) after the line. In other words, the next thing you print will show up on the same line.
The printDate() method: Prints the current date
There are a lot of other methods that you could use to print stuff to the screen. Search the API for information, but there’s printDateTime(), printTime(), printDateTime(style), etc.
Advanced Screen Output
So what sort of things can you print to the screen. Well, lots. You can print text, numbers, or some combination of the two. Look at some examples, and try them yourself:
Console c = new Console(); //
c.println(1); //you can print numbers (notice not quotes)
c.println(3.14159); //you can print numbers with decimals
c.println(25/5) //you can do basic math in the printlns
c.println(“Happy ” + 1 + “st Birthday”); //you can combine things with +, this is called
Steps to Create the Tutorial Program Yourself
Refer to the NetBeans Tutorial if you get lost during any of these steps.
Create a Project in NetBeans called Lesson1.
Create a new Java Class called ConsoleOutput. The template will automatically load up.
Locate the main method, and copy the code (exactly as you see it) from the above code
Remember, all Java Programs create classes. Here we create the class called ConsoleOutput. It’s identified as a class with the words ‘public class’
**REM: This name matches the file name
This defines a “method” called main.
There is only one main method per class, and it is the starting point of the program. The code inside the main method is run by
default when the class is run.
**When you create a main method it must follow the syntax shown precisely.
The import statement brings in Classes / Packages needed in the program. In this case we want the helper package hsa and Console class
This line creates a new instance (object) of a Class called Console.
These lines perform work. They call methods within the c (the Console object created). These methods write some text to the screen (print and println) and then write the date.