Monday, August 22, 2016

JAVA 1 - Do you want to build a program?

So ... do you want to build a program?


Before we start, if you haven't already I need you to download both a version of Eclipse for Java, and the Oracle JDK - I gave instructions for this back here.

When you open Eclipse the first time, you'll have to set a workspace - it can be anywhere, but it helps to remember where it is!



Create a project

Your project space is pretty important, it's your work area on ... well a project.  Where you collect all your objects and files together.  We won't be doing much here on large scale with our programs, because by necessity we'll have to work with relatively small pieces which demonstrate a working idea.

From the menu select File -> New -> Project




Select Java Project, then Next


Set a project name as project helloWorld & select Finish


Within our package explorer, you'll see your project all set up ...


Create a class

Java depends on classes for everything - so we need to add one in order to do anything.

Right click our HelloWorld project and select New -> Class


Now to avoid confusion, I'm going to suggest you go all Yorkshire and call this class "ayeUpWorld".  I also want you to select the tick box for "public static void main".


Now select the Finish button.  You should see your new class added under the Package Explorer ...


Meanwhile under the class, you can see a breakdown of the contents of the class ...


Right now, all this is just default code, but it's worth going through it a line at a time

public class ayUpWorld {

This is a declaration of our class

  • public - this declares that the class can be seen outside of this file area.  Java contains elements called classes, methods and variables - all of which are typically set to either private or public.  Public is easily seen and accessed.  Private, you have to be inside a class to use.  You're probably thinking right now "why not make everything just public - it's easy".  To which I'll say to you right now "shhh ... spoilers!".
  • class - declares that this is a class
  • ayUpWorld - is just the name of the class
  • { ... } these curly brackets are used in Java to show that everything between them belongs to the class we've just defined


public static void main(String[] args) {

This defines our main method.  When we run this program, the main method is immediately run.  Note because of that you can only have one main method per project.

Let's likewise put everything through analysis,

  • public - again we need this public to be accessible.
  • static - this is a complex one for a beginner.  But static just means there's only one instance of this main method.  You could create a whole load of instances of this class, but there'd only be on main method.  [Don't worry if you don't get this right now]
  • void - this position in a method declaration is where the return from the method goes.  We're using void to say that when the main method is finished it's not going to return any form of value.  We'll return to this when we cover methods.
  • main - methods can have any name.  But when we use main (which is a reserved word) it signifies that this method will be called initially when the program is run.
  • (...) - notice methods have normal brackets after the name.  Within here is a list of any parameters passed when a method is called.  We'll return to this when we cover methods.
  • String[] args - you can supply from the command line a whole series of arguments for your system to use - this is defined by this set of Strings.  We won't be using this here.

// TODO Auto-generated method stub

This is a comment - which is used to make code more readable.  The compiler ignored any text after // or between /* and */

Comments allow you to give anyone reading your code some useful background into your project




Let's say "Hello World"

Right now it's really nice ... but it doesn't really do much.  Let's change that.

We're going to make a Hello World program - this is a time honoured program used to learn languages, because it's pretty much a software minimum viable product.  It will output the text "Hello World" to screen.  And that's it.  But along the way we see how the core of the language is structured.

Under the comment line, add this line ...

System.out.println("Hello, World");


  • System.out.println - is a print command with a parameter afterwards in ()
  • "Hello, World" - is the string (defined with the "") that we want to print


REALLY important, and easy to miss is the semicolon ;

All statements in Java end with a semicolon ";".  You'll notice we didn't need them when declaring our class or main method.  But you need them after any line that "does an action".


You're ready to go!

Right let's run this - you'll find a green play button here ...

What happens once selected - your code will compile (right now it's as small as you can make, so this won't seem like long) before running.  Compiling is when your Java text is turned into an executable file.

Once finished, you'll see under the Console ...




Commenting



You might notice throughout the examples in the series I'll sometimes use // or /* ... */ formats, and it'll turn the text green.  These are called comments ...
  • // comments our the rest of a line
  • /* comments out all text until you get to a */
Comments are ignored by your compiler when you build Java, they exist only to help document and explain your code.  I strive throughout the series to make the code readable - using function names and variables which match what I'm doing, but occasionally I'll use a comment to highlight part, or explain something that's new.

1 comment:

  1. Baccarat betting options in a simple way.

    G clubOnline Gambling Betting Sites Win online gambling games that can help you make the most of online gamblers. To make you a gambler, online gambling games that are interested in using the online gambling and investment services with us. You can use the service just to invest here.

    This game is easy to play, easy to play, easy to play all the time. With all the bets. Wherever it is, it will be exposed to gambling that will make good money in all areas. There are easier bets. A gambling destination that gives players the fun and investment they need to play anywhere. Every day gambling is a gamble where every good money. Make a good income every day, there are gambling games that will like to get good deals with mobile. Make this fun. Wherever it is, it can be a good bet all the time. Gclub มือถือ

    ReplyDelete