Java Programming Tutorial Redirection

I made these tutorials because I wasn’t aware of any good (i.e. simple, clear, and concise) Java programming tutorials online. But recently I’ve been introduced to a couple of good resources, so I’m not going to make any more tutorials.

Good luck with your programming!

Java Programming Tutorial 4: Variables

Variables store information in memory (such as a person’s name, the position of the mouse cursor on the screen, and whether a window is minimized or not)
  • CREATING – Variables are always made by specifying the type of variable and then the variable name (ex. “int age” creates an integer variable called “age”).   Variable names can’t contain spaces our strange characters. And, of course, if all you’re doing is creating the variable on one line by itself, then you need to end your command with a semi-colon (ex. “int age;”)
  • ASSIGNING – Set a variable’s value with the “=” operator.  This can be done at the same time that the variable is created (ex. “int age = 25;”) and/or at any other time in the program (ex. “age = 20;”)
  • USING – Simply type the name of the variable, by itself, to use it (ex. “age”).

These are the most important variables:

  • int – An integer stores whole numbers, using 32-bits of memory (less common are the byte, short, and long variables which are just like the int, but use 8-bits, 16-bits, and 64-bits, respectively).  The more memory a variable uses, the bigger the number the variable can hold.
  • double – Stores a decimal number, using 64-bits of memory (less common is the float which is just like the double, but uses 32-bits). More memory, in this case, means better precision.
  • char – Just one letter or character (ex. ‘a‘ or ‘*‘) which must be surrounded by two apostrophe marks.
  • boolean – Is either true or false.

Give it a try!

  1. Open your file from the previous tutorials
  2. Before the line that starts with “System.out.println”, create several lines of variables of different types.  Assign some of these variables when you create them and some of them later in the program. For example: “int age = 5;” and “bool hungry = true;” and “age = 20;”
  3. Duplicate the “System.out.println” line for each variable that you have, deleting the “Hello World” message (and its quotation marks) and replacing it with the name of the variable. For example: “System.out.println(age);”

Your program might look something like this:

public class Program {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int age = 5;

double cost = 2.89;  // Gas prices in northern Utah

char middleInitial = ‘Z';

boolean hungry = false;

boolean male;

age = 20;

hungry = true;

male = hungry;

System.out.println(“Hello World!”);

System.out.println(age);

System.out.println(cost);

System.out.println(middleInitial);

System.out.println(hungry);

System.out.println(male);

}

}

Run your program and notice the output in the console.

Java Programming Tutorial 3: Writing Your First Program

The file that you created in the previous tutorial should look like this:

public class Program {

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

 

}

 

}

 

  • COMMENTS – Anything on a line that is to the right of “//” is ignored by Eclipse.  Also, anything in the program after a “/*” and before a “*/” is ignored.  These are tools for you to write comments in your program that help other programmers (and yourself) to remember what’s going on in your program – to take notes, you might say.
  • FUNCTIONS – Functions are small parts of your program.  The line that says “public static void main(String[] args)” starts a function called “main”.  The following “{” marks the beginning of the function, and the next “}” marks the end of the function.  As you can see, nothing actually happens in the function right now.  When you run a Java program, it starts by looking for a function called “main” and runs it.  We’ll talk more about the words that come before the function name (“public static void”), and the stuff between the parenthesis, later.
  • CLASSES – Classes are groupings of functions and data.  They always have the word “class” before the name of the class.  They also start and end with “{” and “}”.

THE BIG PICTURE – As you can see, we have a class (called “Program”) containing one function (called “main”) that does absolutely nothing.  Our first task will be to make the program display text to the user.  To do that, write the following somewhere between the main function’s “{” and “}” markers:

System.out.println(“Hello World!”);

This command runs another function called “System.out.println”.  When running another function, everything inside of the parenthesis is sent as information to that function.  All text messages (called “Strings”) must be surrounded in quotation marks.  Finally, all commands end with a semi-colon.

You’re final program should look like this:

 public class Program {

/**

 * @param args

 */

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(“Hello World!”);

}

 

}

Run your program by clicking on the “Run” menu and selecting “Run” or “Run Last Launched.”  Notice, in the “Console” tab, that the message “Hello World!” was displayed!

Java Programming Tutorial 2: Creating a New Project

Every program that you make needs to be in a new “Project.”

  1. Open eclipse.exe
  2. From the “File” menu, select “New\Java Project”
  3. Enter the name of your program into the “Project name” box (ex. “Hello World”)
  4. Click “Finish”

Congratulations, you made a project for your first program!  Now you need to create a file to write your Java code in.

  1. Find your project In the “Package Explorer”
  2. Click on the “+” next to your project to expand it
  3. Right-click on the “src” item, and then select “New\Class” from the drop-down menu
  4. Enter the name of the file that you want to make (ex. “Program”) in the “Name” box
  5. If this is the main file for your program, check the box next to “public static void main(String[] args)”
  6. Click “Finish”

Now you’re ready to start programming!

Java Programming Tutorial 1: Installing Tools

These instructions will lead you through the process of installing tools needed in order to program in Java on a Windows computer.

  1. You need a JDK (Java Development Kit) installed on your computer. You don’t need to download any package with additional tools, such as JavaFX or Java EE, just the JDK by itself. You can get it from the following website:
  2. http://www.oracle.com/technetwork/java/javase/downloads/index.html

  3. You should have Eclipse installed on your computer (we will use this as the Java code editor). You need the “Eclipse IDE for Java Developers.” You can get it from the following website:
  4. http://www.eclipse.org/downloads/

    Then unzip the file to some directory (say, “C:\Program Files” – it should create a subdirectory called “eclipse”)