Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Java variables

This lesson focuses on:

What is a variable?

A variable is a container which holds information in a computer's memory. The value of a variable can change all throughout a program.

Declaring variables

In Java, different variables store different types of data. The type of data that is stored by a variable is signified with a data type.

Java data types:

Data type Type of data it stores 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

A variable is declared with the data type of the data it will store.

Syntax:

dataType varName;

Example:

char aCharacter;
int aNumber;

In the above example, two variables are declared. The first variable is used to store a single character and is therefore of data type char. The second variable is used to store a whole number and is therefore of data type int. You can assign a value to a variable at the same time that it is declared. This process is known as initialization.

Example of initializing a variable:

char aCharacter = 'a';
int aNumber = 10;

In the above example a character variable named aCharacter is initialized with the value 'a' and a numeric variable is initialized with the value 10.

Example of declaring a variable and then giving it a value:

char aCharacter 
aCharacter = 'a';

int aNumber 
aNumber = 10;

In the above example a character variable named aCharacter is declared. On the next line this variable is assigned the value 'a' and a numeric variable named aNumber is declared and on the next line it is assigned the value 10.

NOTE: A variable must be declared with a data type. If you do not specify a data type for a variable, an error will be generated! Furthermore, The data type of a variable should be used only once with the variable name - during declaration. After a variable has been declared or initialized, you can refer to it by its name without the data type.

Naming variables

When naming variables, several rules should be considered. These rules will make variable declaration easier, as well as clear up ambiguity and possible errors in code.

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.

Example:

class PrintText{

public static void main(String[] args){

	//declare some variables
	byte aByte = -10;
	int aNumber = 10;
	char aChar = 'b';
	boolean isBoolean = true;
	
	//print the variables
 	System.out.println(aByte);
	System.out.println(aNumber);
	System.out.println(aChar);
	System.out.println(isBoolean);

   }

}

Output:

-10
10
b
true

You can also print variables together with regular text. To do this, use the + symbol to join the text and variable values together.

Example:

class PrintText{

public static void main(String[] args){

	//declare some variables
	byte aByte = -10;
	int aNumber = 10;
	char aChar = 'b';
	boolean isBoolean = true;

	//print the variables	
 	System.out.println("aByte = " + aByte);
	System.out.println("aNumber = " + aNumber);
	System.out.println("aChar = " + aChar);
	System.out.println("Is the isBoolean 
        variable a boolean variable? " + isBoolean);

   }

}

Output:

aByte = -10
aNumber = 10
aChar = b
Is the isBoolean variable a boolean variable? true

Output:

Here is some text
Here is some more text

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments