• $10 Game Dev' Courses - Use code "BREAKTHRU"

    Video courses

The Complete Java Developer Course. Learn Step by Step

The Complete Android & Java Developer Course – Build 21 Apps

Master Android N App Development With Java

The main way to change our game’s variables using Java is through a concept called operators.Operators in Java are just like mathematical operators and many of them are the same symbol. So if you coped with junior school math you will have no problem with this tutorial. We will start by looking at a list of some of the most commonly used Java operators.

About this tutorial

Skill level 1
Time to read: 10 minutes

New Concepts:

  1. Operators
  2. assignment
  3. increment
  4. decrement

Recommended preparation tutorials

Projects related to or that demonstrate these concepts

Java operators

Here is a list of the most useful Java operators that allow us to do things with our game’s variables. One of which we already know about.
The assignment operator which we have already seen ( =): This makes the variable to the left of the operator the same as the value to the right. For example:

hiScore = score;

or

score = 100;

The addition operator +: This adds the values on either side of the operator. It is usually used in conjunction with the assignment operator, such as:

score = aliensShot + wavesCleared;

or

score = score + 100;

Notice that it is perfectly acceptable to use the same variable on both sides of an operator.
The subtraction operator -: This subtracts the value on the right side of the operator from the value on the left. It is usually used in conjunction with the assignment operator, perhaps:

lives = lives - 1;

or

aliensRemaining = aliensRemaining - numberAliensDestroyed;
  • Learning java by building Android games

    Learning Java by Building Android Games

    Want to learn Java for Android? Even if you are completely new to either Java, Android, or game programming but are aiming to publish Android games, then this book is for you. This book also acts as a refresher for those who already have experience in Java on other platforms or other object-oriented languages.
    • Setup your own Android game programming environment using Android Studio
    • Control logic, branch your code, and add real decision-making depth to your games
    • Design and use 2d sprite animations, smooth pixel graphics, sound FX, simple collision detection and artificial intelligence (AI)
    • Build around a dozen sample test apps and 4 complete working games!
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)
    small_book

    Android Game Programming by Example

    Learn to build exciting Android game projects. Build a simple flappy-bird like game to a multi-environment, tough, retro platformer then an OpenGL ES 2 Asteroids clone, running at hundreds of frames per second .
    • Animate your characters with sprite sheets, add multiple scrolling parallax backgrounds, and implement genuinely tough, playable levels in your games
    • Every single line of code is printed in the book! No need to refer to the code files while trying to follow along. All the code files are also supplied separately so you can refer to them in their completed form and copy/paste them into your project if you like.
    • Implement a multitude of other game features such as pickups, firing weapons, HUD’s, generating and playing sound FX, scenery, level transition, high scores, and more
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US Only)

The division operator /: This divides the number on the left by the number on the right. Again, it is usually used with the assignment operator, like this:

hitPoints = hitPoints / swordLevel;

or

recycledValueOfBlock = originalValue / .9;

Obviously, in the previous example, the variable recycledValueOfBlock will need to be of the type float to be multiplied by the value of .9.
The multiplication operator *: Perhaps unsurprisingly, multiplies variables and numbers, such as:

answer = 10 * 10;

or

biggerAnswer = 10 * 10 * 10;

The increment operator ++: This is a really neat way to add 1 to the value of one of our game’s variables.

This:

myVariable = myVariable + 1;

is the same as this:

myVariable ++;

The decrement operator --: You guessed it, a really neat way to subtract 1 from something.

playerHealth = playerHealth -1;

is the same as:

playerHealth --;
If you would like to practice what you have learnt in this and the previous tutorial about game variables try the mini-project Game variables demo.
In the next tutorial, Checking for conditions in our game we will try using some declarations, assignments and operators. When we bundle these elements together into some meaningful syntax, we call it an expression.