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!”);








Run your program and notice the output in the console.

Leave a Comment