Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 28 May 2013

JAVA Program : Hello World!

Posted on 20:25 by Unknown
I want to share with you guys a simple program to demonstrate what software programming is about; this is a "Hello World!" example that many people see when learning on how to write computer programs using a specific programming language.

In this case, I'm going to show you on how to create the example program in JAVA. If you do not have JAVA installed, be sure to go on to http://www.oracle.com and download the latest version of the language plugin on your computer. You should also take the time to download a developer's toolkit and an integrated development environment for you to write programs at a more convenient level.

The advantage of using an IDE over a text file is, every time you save your source code file, it is compiled automatically. You can also see the moment you make a mistake, a compiler error seen on the scroll bar where if you hover your mouse over, you'll see why you get an error like this.

Read the on-screen instructions and check to see if you have enough space on your computer or hard drive to install the software needed. By the time you have it installed, open up the IDE and follow the introduction instructions and how to go to the workspace and write a new source code file.

When you get there, you should see a large white text area with the cursor on the top left corner, with no characters inserted. That's where you will start writing your source code.

Let's get started, shall we?



The first thing you need to do is declare and open up a class definition. This is where your program is going to lie; the other purpose of a class definition is to prepare "a recipe where you can make exact copies of it," which is essentially the principle of object-oriented programming. But let's worry about making a simple program for now.

Type in "public class," all in lower case, and then the class name. You want to make sure that the class name is the same as the file name you're typing in, without the .java extension, because otherwise you will receive a compiler error. You cannot declare a class name that is other than the name of the source code file itself.

Since we have created a file called "HelloWorld.java," the header of the definition will look like this:
public class HelloWorld

Next, press Enter, and type in the left brace, press Enter, and the right brace. This is where you should be so far:

public class HelloWorld
{
}

Now, you want to add an empty bar between the braces, because you're now going to add a method called "main." This is what the code should look like by now:

public class HelloWorld
{

}

The "main" method is always the first method the program goes to upon execution. This is where your starting statements or your method calls will be done. The header is usually constant, but you'll eventually see that the parentheses can be set up in different ways as long as you can declare it of the same type.

Anyway, the header of the main method looks like this:

public static void main(String[] args)

If you are curious about what each part of the header is, let me tell you what they are.

"public" is an access specifier. It tells the compiler how the method can be accessed. There are two other specifiers you can use, with an extra one being no specifier at all; each one of them has a different visiblity, so you may want to stick with the specifier "public" before learning on how the other specifiers set the access to a method or a class. The other specifiers you can use for a method or class are "private" and "protected"; do not use them in your class declaration, however. You can only use them in what are called inner classes, which you shall see later if you decide to keep learning.

"static" simply means it is part of the entire class and not to every specific object. Let's say you instantiate 10 objects out of the Car class you've programmed yourself beforehand, and you declared a counter in the class with the word "static" that every time you instantiate the class for an object, it increases by one. Instead of returning 1 when you want to display that variable, because it is common to all objects, it returns 10.

You cannot reference a static variable or method with a non-static variable or method; otherwise you will get a compiler error. All Java classes use main as part of the entire class instead of every single object instantiated, so that's why the method is declared as a static method.

"void" is a return type. All method headers you write need to have their return types declared, but since the main is not designed to return anything, we use the key word "void." For any primitive data type or object type, you have to specifically return a reference or an actual value, as dependent on the type. If you attempt to return something in a void method, you will get a compiler error.

"main" is simply the name of the method. All methods and classes have names associated with their declaration; it's the norm for programming applications or experiments.

And what's inside the brackets are simply the arguments of the method. In this case, we are declaring one argument to be an array of String objects. You'll see more information about this by still attending your programming course or even reading a book on doing programming like this,

Anyway, moving on to the programming, what you want to do next is the same thing you did as the step after declaring your class: add two opposing braces, with an empty line, over three different lines. It will look like this:

public static void main(String[] args)
{

}

This program consists of displaying a simple message, so what we want to do is use console output. In Java, the way to do this is to use a method called println, referenced by System.out. The statement looks like this:

System.out.println();

Any object reference, variable, or a constant string message, can go between the parentheses and be displayed on the console upon execution. Here, we want to display a message, so we write the statement like this: 

System.out.println("Hello world!");

Put the statement inside the braces of the main method. The final program will look like this:

public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}

Compile your program using the IDE compiler or the command way, run your program on the command prompt, or IDE, and voila: you will see "Hello world!" on your screen!

That's how you write the most simplest Java program; if you keep paying attention to every specific detail of the program, you'll eventually build your steps to becoming a programmer.

Anyway, I'll see you around!
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • The Black Glove [Short Poem by me]
    This is a poem written by me back in April 2010. I'm not sure if this was for an assignment, but this is what I got. It may not be good ...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • Princess Peach In India [Story Idea]
    Here's a really fancy story I have made, although it is more of an initial idea and comes from a visual I had more than two years ago. E...
  • Super Mario Script 1 (v 2.3) [Super Mario Galaxy font, with a few photos]
    Hello there. Do you recognize the font of this text? I'll give you two clues. The one below is the a sample sentence of different sizes....

Blog Archive

  • ▼  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ►  June (12)
    • ▼  May (12)
      • JAVA Program: Number Loop (Experimental Program of...
      • JAVA Program: Displaying Unlimited Strings on a GU...
      • JAVA Program: Command-based String Remover in Text...
      • Arcade Games to check out
      • JAVA Program: Random 0s and 1s Table with Refresh ...
      • JAVA Program: Object-Oriented Programming Class, t...
      • Announcement on "The Asperger Computer"
      • JAVA Program : Hello World!
      • Introducing myself (original blog on Autism Suppor...
      • Demonstration of JAVA Classes
      • Non-fiction e-book, The Asperger Computer, reveals...
      • The Asperger Computer - Facebook Edition
    • ►  April (2)
    • ►  January (1)
  • ►  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile