Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java variables

No matter how simple or complex a program is, the data in that program has to be stored and managed in some way. Variables are here to do it!

This tutorial focuses on:

What is a variable?

A variable is a container that stores a meaningful value that can be used throughout a program. For example, in a program that calculates tax on items you can have a few variables - one variable that stores the regular price of an item and another variable that stores the total price of an item after the tax is calculated on it. Variables store this information in a computer's memory and the value of a variable can change all through out a program.

Declaring variables

One variable in your program can store numeric data while another variable can store text data. Java has special keywords to signify what type of data each variable stores. Use these keywords when declaring your variables to set the data type of the variable.

Java data types
Keyword Type of data the variable will store Size in memory
boolean true/false value 1 bit
byte byte size integer 8 bits
char a single character 16 bits
double double precision floating point decimal number 64 bits
float single precision floating point decimal number 32 bits
int a whole number 32 bits
long a whole number (used for long numbers) 64 bits
short a whole number (used for short numbers) 16 bits
Example:
char aCharacter; int aNumber;

You can assign a value to a variable at the same time that it is declared. This process is known as initialization:

Example:
char aCharacter = 'a'; int aNumber = 10;
Declaring a variable and then giving it a value:
char aCharacter; aCharacter = 'a'; int aNumber; aNumber = 10;

NOTE: A variable must be declared with a data type or an error will be generated! The data type of a variable should be used only once with the variable name - during declaration. After that, you can refer to the variable by its name without the data type.

Naming variables

Rules that must be followed when naming variables or errors will be generated and your program will not work:

Recommended practices (make working with variables easier and help clear up ambiguity in code):

Also keep in mind:

Printing variables

Variables are printed by including the variable name in a System.out.print() or System.out.println() method. When printing the value of a variable, the variable name should NOT be included in double quotes. You can also print variables together with regular text. To do this, use the + symbol to join the text and variable values.

class PrintText{ public static void main(String[] args){ //declare some variables byte aByte = -10; int aNumber = 10; char aChar = 'b'; boolean isBoolean = true; //print variables alone System.out.println(aByte); System.out.println(aNumber); //print variables with text System.out.println("aChar = " + aChar); System.out.println("Is the isBoolean variable a boolean variable? " + isBoolean); } }
Output:
-10 10 aChar = b Is the isBoolean variable a boolean variable? true
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information