JBay Solutions Development Blog on Java, Android, Play2 and others

Java Tutorial 3 - Variables - First Look

  1. Introduction
  2. My First Variable
  3. Primitive Variables
  4. Some operator action and Strings


On the previous tutorial we made a really simple program, with just one Class and one Method. There was no data stored, no calculations done, etc.

In this tutorial we will look into storing data and doing something with that data. We'll talk about variables, primitives and Objects, and we'll make a really simple calculator to add 2 numbers! Yes, exciting!

My first Variable

First, we open our favorite text editor and create a new class called Lesson2 with an empty main method. If you have been following the tutorials, try and do it by yourself, or just simply copy the following code onto the editor and save the file as Lesson2.java (remember, java is Case Sensitive).

public class Lesson2 {
    public static void main(String[] args) {


You can now compile it and run it. It should give you no errors and print absolutely nothing.

Make the main method now print the "Hello World" phrase, like it was done on the previous tutorial, and we should end up with something like this:

public class Lesson2 {
    public static void main(String[] args) {
        System.out.println("Hello World");

Compile and run it again. It should be printing "Hello World"! Remember, every time you modify the Source Code you must compile the code again, and only then will you be able to run it with the new changes.

Lets modify this code now to have a variable and have that code print the value of the variable:

public class Lesson2 {

    public static void main(String[] args) {
        int firstVariable = 5;
        System.out.println("Value is: " + firstVariable);


The first change you notice is the int firstVariable = 5; . This is a variable named firstVariable , it is of type int (integer) and has a value of 5.

This is how you declare a variable in Java: You explicitly define the Type of the variable and give it a name. Optionally you can also define a value, like we did there, but this is not mandatory.

The second change is the print that we are now doing: System.out.println("Value is: " + firstVariable); . The program will now print "Value is: " and concatenate the value of the firstValue using the operator +.

Compile and run it. It should have printed : Value is: 5

So, the first variable, which is named firstVariable is of type int (which stands for integer), but what is an int and what other types are there?

Primitive Variables

int is what is called a Primitive type and there are 7 other primitive types. Here is a complete list, with a very simple description of what they are:

  • short : a type of whole number, that can go from -32768 to 32767. (it is 16bit signed)
  • int : a type of whole number, that can go from -2^31 to (2^31)-1. (it is 32bit signed)
  • long : a type of whole number, that can go from -2^63 to (2^63)-1. (it is 64bit signed)
  • float : a type of floating point number, therefore can have decimals (it is 32bit single precision)
  • double : a type of floating point number, therefore can have decimals (it is 64bit double precision)
  • boolean : a type that only allows 2 values : true or false.
  • char : a single character (16 bit)
  • byte : a type of whole number, that can go from -128 to 127. (it is 8bit signed)

Before you get too nervous about all this jibber jabber, lets start by saying that only about 3 or 4 of these are used really regularly and these are : int, long, double and boolean . That is not to mean that you don't need to know about the other ones, you do, and you will use some of them, but for learning purposes we'll focus on these 4 for now.

What is a Primitive Variable? There are two types of Variables, those that are of a Primitive type (therefore of one of these 8 types) or those that are Reference Variable. We'll see Reference Variables later, lets write some code first!

Lets modify the Lesson2 class and create variables for these types:

public static void main(String[] args) {
    short shortVariable = 1;
    System.out.println("shortVariable is: " + shortVariable);

    int intVariable = 2;
    System.out.println("intVariable is: " + intVariable);

    long longVariable = 3;
    System.out.println("longVariable is: " + longVariable);

    float floatVariable = 4.1f;
    System.out.println("floatVariable is: " + floatVariable);

    double doubleVariable = 5.1;
    System.out.println("doubleVariable is: " + doubleVariable);

    boolean booleanVariable = true;
    System.out.println("booleanVariable is: " + booleanVariable);

    char charVariable = 'x';
    System.out.println("charVariable is: " + charVariable);    

    byte byteVariable = 8;    
    System.out.println("byteVariable is: " + byteVariable);    

Now compile and run it. You should be getting an output like:

shortVariable is: 1
intVariable is: 2
longVariable is: 3
floatVariable is: 4.1
doubleVariable is: 5.1
booleanVariable is: true
charVariable is: x
byteVariable is: 8

Notice the notation used to define these variables:

  • float floatVariable = 4.1f ;

We placed an "f" after the number. When you define a value of a float typed variable, you must write the letter "f" after the number, to let the compiler know (when you decide to compile the code) that you understand that you are using a small floating-point type, and therefore know that you will not have a lot of precision on that number.

  • boolean booleanVariable = true ;

When defining a value for a boolean typed variable, there are only 2 choices: true or false .

  • char charVariable = 'x' ;

When defining a value for a char typed variable, the character must be enclosed inside ' and only one character. Char is a bit special, it is 16 bits, exactly the same as the short type... and are interchangeable. We'll get a chance to see it later.

Now that we have a basic knowledge of the primitive variable types, lets do some operations on those.

Some Operator Action and Strings!

Now that we know how to define and assign values to primitive variables, lets modify the code again, and have only two variables of type int defined inside the main method. We'll call them number1 and number2 and will assign them the values 50 and 2. Try doing it yourself!

It should end up looking like this:

public static void main(String[] args) {

    // Variable of type int called number1 and number2
    int number1 = 50;
    int number2 = 2;


If you compile and run it now, it should produce no output. Also, notice that now we have a comment on the source code, before the definition of the variables. This is one of the two ways of writing comments, by placing // everything thing written to the right of this will be considered a comment and ignored by the compiler.

Lets now create another variable, named sumOfNumbers, which will contain the sum of number1 and number2, and lets print the result to the console:

public static void main(String[] args) {

    // Variables of type int called number1 and number2
    int number1 = 50;
    int number2 = 2;

    int sumOfNumbers = number1 + number2;

    System.out.println( sumOfNumbers );


Compile it and give it a run. It should have printed the result : 52

There are many other operators, like - for subtraction, / for division, * for multiplication, % for modulus. There are also Compound Operators and Equality Operator, which we'll see very soon.

Experiment with those operators to see what happens, create a few more variables with the result of these operators applied to variables number1 and number2 and print those values out. Check the following code:

public static void main(String[] args) {

    // Variables of type int called number1 and number2
    int number1 = 50;
    int number2 = 2;

    int sumOfNumbers = number1 + number2;
    System.out.println( "Sum: " + sumOfNumbers );

    int subtractionOfNumbers = number1 - number2;
    System.out.println( "Subtraction: " + subtractionOfNumbers );

    int divisionOfNumbers = number1 / number2;
    System.out.println( "Division: " +divisionOfNumbers );

        For the Multiplication, we'll do something different

    int multiplicationOfNumbers;
    multiplicationOfNumbers = number1 * number2;
    System.out.println( "Multiplication: " + multiplicationOfNumbers );


Compile and run!

In this piece of code we can see a few new things:

  • We have a new type of comment : /* comment */ . This type of comment can span several lines, it begins with /* and ends with */ . Everything in between those is ignored by the compiler

  • We create an int typed variable called multiplicationOfNumbers but we do not assign any value to it. Then, on the next statement, we assign the value with the result of the multiplication of number1 and number2.

  • We modified every "print" statement, to now print a String (like "Multiplication: ") with the value of specific variables.

Notice that we have been using the + operator since the beginning of the tutorials to concatenate a String ( something inside " ) and values of primitive variables. Keep in mind that the Add operator, when applied to primitives calculates the addition of the values of the two variables. When applied to one or more Strings, it performs concatenation.

On the next Tutorial we'll talk about Constructors and Methods.

When you feel ready: Tutorial 4 - Classes, Constructors and Methods

comments powered by Disqus